<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="this.source == 'BoDetails' ? this.bo.winningState == 0 ? true : false : true">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="this.boAuthority.boAuthority.contactAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="contactList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gander" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gander"/>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="所属客户" align="center" prop="customerName" />
      <el-table-column label="部门名称" align="center" prop="departmentName" />
      <el-table-column label="职务" align="center" prop="position" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_position" :value="scope.row.position"/>
        </template>
      </el-table-column>
      <el-table-column label="决策力" align="center" prop="power" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_power" :value="scope.row.power"/>
        </template>
      </el-table-column>
      <el-table-column label="兴趣爱好" align="center" prop="hobby" />
      <el-table-column label="家庭地址" align="center" prop="address" />
      <el-table-column label="状态" align="center" prop="state" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_contact_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" v-if="this.source == 'BoDetails' ? this.bo.winningState == 0 ? true : false : true">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="!boAuthority.boAuthority.contactEdit"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleBrowse(scope.row)"
            :disabled="!boAuthority.boAuthority.contactView"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            :disabled="!boAuthority.boAuthority.contactDel"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改联系人管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="this.operatingState == 'Browse'">
        <el-divider content-position="left">
          <dev style="width: 50px; height: 40px; font-size: 18px">基本信息</dev>
        </el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="商机id" prop="boId" v-if="source == 'Contact'">
              <el-input v-model="form.boId" placeholder="请输入商机id" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" placeholder="系统自动生成编码" :disabled = "true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="gander">
              <el-select v-model="form.gander" placeholder="请输入性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="所属客户" prop="customerName">
              <el-input v-model="form.customerName" placeholder="请输入所属客户" :disabled="this.source == 'BoDetails'">
                <el-button slot="append" icon="el-icon-more" @click="refereCustomer" :disabled="this.source == 'BoDetails'"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人分类" prop="contactClassification">
              <el-select v-model="form.contactClassification" placeholder="请输入联系人分类">
                <el-option
                  v-for="dict in dict.type.mk_bo_contact_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生日" prop="birthday">
              <el-date-picker clearable
                v-model="form.birthday"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择生日">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="籍贯" prop="birthplace">
              <el-input v-model="form.birthplace" placeholder="请输入籍贯" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="兴趣爱好" prop="hobby">
              <el-input v-model="form.hobby" placeholder="请输入业务爱好" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="state">
              <el-select v-model="form.state" placeholder="请输入状态">
                <el-option
                  v-for="dict in dict.type.mk_bo_contact_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">
          <dev style="width: 50px; height: 40px; font-size: 18px">工作信息</dev>
        </el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="上级联系人" prop="superiorContactName">
              <el-input v-model="form.superiorContactName" >
                <el-button slot="append" icon="el-icon-more" @click="refereContact"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任职科室" prop="section">
              <el-select v-model="form.section" placeholder="请输入任职科室">
                <el-option
                  v-for="dict in dict.type.mk_bo_section"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="职务" prop="position">
              <el-select v-model="form.position" placeholder="请输入职务">
                <el-option
                  v-for="dict in dict.type.mk_bo_position"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="职称" prop="jobTitle">
              <el-select v-model="form.jobTitle" placeholder="请输入职称">
                <el-option
                  v-for="dict in dict.type.mk_bo_job_title"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="决策力" prop="power">
              <el-select v-model="form.power" placeholder="请输入决策力">
                <el-option
                  v-for="dict in dict.type.mk_bo_power"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="支持度" prop="support">
              <el-select v-model="form.support" placeholder="请输入支持度">
                <el-option
                  v-for="dict in dict.type.mk_bo_support"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="擅长领域" prop="fieldExpertise">
              <el-select v-model="form.fieldExpertise" placeholder="请输入擅长领域">
                <el-option
                  v-for="dict in dict.type.mk_bo_field_expertise"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关键决策人" prop="decisionMaker">
              <el-select v-model="form.decisionMaker" placeholder="请输入关键决策人">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
          </el-col>
        </el-row>
        <el-divider content-position="left">
          <dev style="width: 50px; height: 40px; font-size: 18px">联系信息</dev>
        </el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮件" prop="mail">
              <el-input v-model="form.mail" placeholder="请输入邮件" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家庭地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入家庭地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="最佳拜访时间" prop="visitTime">
              <el-input v-model="form.visitTime" placeholder="请选择最佳拜访时间" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最佳拜访地点" prop="visitPlace">
              <el-input v-model="form.visitPlace" placeholder="请输入最佳拜访地点" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
          </el-col>
        </el-row>
        <el-tabs v-model="activeName" @tab-click="handleClick" v-if="this.operatingState != 'Insert'">
          <el-tab-pane label="学历信息" name="first">
            <EducationList  :key="timer" :source="'ContactList'" :supForm="this.form" />
          </el-tab-pane>
          <el-tab-pane label="社会关系" name="second">
            <RelationshipList :key="timer" :source="'ContactList'" :supForm="this.form" />
          </el-tab-pane>
        </el-tabs>
        <div class="md-auditInfo">
          <el-divider content-position="left">
            <dev style="width: 50px; height: 40px; font-size: 18px">其它信息</dev>
          </el-divider>
          <el-form :inline="true" label-position="right" :model="form">
            <el-form-item label="创建人">
              <el-input v-model="form.createByName" size="small" readonly></el-input>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-input v-model="form.createTime" size="small" readonly></el-input>
            </el-form-item>
            <el-form-item label="修改人">
              <el-input v-model="form.updateByName" size="small" readonly></el-input>
            </el-form-item>
            <el-form-item label="修改时间">
              <el-input v-model="form.updateTime" size="small" readonly></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="this.operatingState != 'Browse'" :disabled="submitButtonEditStatus">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户参照 -->
    <CustomerRef
      ref="customerSelect"
      @doSubmit="customerSelectionsToInput"
      :single="true"
    />
    <!-- 联系人参照 -->
    <ContactRef
      ref="contactSelect"
      @doSubmit="contactSelectionsToInput"
      :single="true"
      :outerQueryParams="this.bo"
    />
  </div>
</template>

<script>
import { listContact, getContact, delContact, addContact, updateContact } from "@/api/business/spd/bo/contact";
import CustomerRef from '@/views/business/spd/bo/refer/customer/index.vue';
import EducationList from '../education/educationList.vue';
import RelationshipList from '../relationship/relationshipList.vue';
import ContactRef from '@/views/business/spd/bo/refer/contact/index.vue';

export default {
  name: "contactList",
  props:["source","bo","boAuthority"],
  dicts: ['sys_user_sex','mk_bo_contact_state','mk_bo_section','mk_bo_position','mk_bo_job_title','mk_bo_power','mk_bo_support','mk_bo_field_expertise','sys_yes_no','mk_bo_contact_type'],
  components: {CustomerRef,ContactRef,EducationList,RelationshipList},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联系人管理表格数据
      contactList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boId: null,
        name: null,
        code: null,
        customer: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        gander: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "所属客户名称不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        section: [
          { required: true, message: "任职科室不能为空", trigger: "blur" }
        ],
        position: [
          { required: true, message: "职务不能为空", trigger: "blur" }
        ],
        jobTitle: [
          { required: true, message: "职称不能为空", trigger: "blur" }
        ],
        power: [
          { required: true, message: "决策力不能为空", trigger: "blur" }
        ],
        decisionMaker: [
          { required: true, message: "关键决策人不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          {
            validator: function(rule, value, callback) {
              if (/^1[34578]\d{9}$/.test(value) == false) {
                callback(new Error("手机号格式错误"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ],
      },
      //重新加载子组件参数
      timer: '',
      //当前操作状态
      operatingState: '',
      //
      activeName: 'first',
      //确定按钮是否可点
      submitButtonEditStatus:false,
    };
  },
  created() {
    if(this.source == 'BoDetails'){
      this.queryParams.customer = this.bo.customer;
      let params = {"post":this.boAuthority.post};
      this.queryParams.params = params;
    }
    if(this.source == 'Contact'){
      this.queryParams = this.bo;
    }
    console.log('this.boAuthority',this.boAuthority);
    this.getList();
  },
  methods: {
    /** 查询联系人管理列表 */
    getList() {
      this.loading = true;
      listContact(this.queryParams).then(response => {
        this.contactList = response.rows;
        if(this.source == 'BoDetails'){
          console.log('this.contactList',this.contactList);
          for (var i = 0; i < this.contactList.length; i++) {
            this.contactList[i].telephone = this.contactList[i].telephone.substring(0,3) + '******' + this.contactList[i].telephone.substring(this.contactList[i].telephone.length - 4,this.contactList[i].telephone.length);
            this.contactList[i].customerName = this.contactList[i].customerName.substring(0,2) + '******' + this.contactList[i].customerName.substring(this.contactList[i].customerName.length - 2,this.contactList[i].customerName.length);
            const start = new Array(this.contactList[i].name.length).join('*');
            this.contactList[i].name = start + this.contactList[i].name.slice(-1);
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        boId: null,
        delFlag: null,
        updateTime: null,
        updateBy: null,
        createDate: null,
        createBy: null,
        departmentName: null,
        departmentCode: null,
        area: null,
        organization: null,
        visitPlace: null,
        visitTime: null,
        address: null,
        mail: null,
        telephone: null,
        decisionMaker: null,
        fieldExpertise: null,
        support: null,
        power: null,
        jobTitle: null,
        position: null,
        section: null,
        superiorContact: null,
        superiorContactName: null,
        state: null,
        hobby: null,
        birthplace: null,
        birthday: null,
        contactClassification: null,
        customerName: null,
        customerCode: null,
        gander: null,
        name: null,
        code: null,
        id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.operatingState = "Insert";
      this.reset();
      if(this.source == 'BoDetails'){
        this.form.boId = this.bo.id;
        this.form.customer = this.bo.customer;
        this.form.customerName = this.bo.customerName;
      }
      this.form.state = '1';
      this.open = true;
      this.title = "添加联系人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.operatingState = "Update";
      this.reset();
      const id = row.id || this.ids
      getContact(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改联系人";
        this.timer = new Date().getTime();
      });
    },
    /** 查看按钮操作 */
    handleBrowse(row) {
      this.reset();
      const id = row.id || this.ids
      getContact(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.operatingState = "Browse";
        this.title = "基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.submitButtonEditStatus = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContact(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.submitButtonEditStatus = false;
            });
          } else {
            addContact(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.submitButtonEditStatus = false;
            });
          }
        }else{
          this.submitButtonEditStatus = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除联系人管理编号为"' + ids + '"的数据项？').then(function() {
        return delContact(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/contact/export', {
        ...this.queryParams
      }, `contact_${new Date().getTime()}.xlsx`)
    },
    // 触发客户参照列表
    refereCustomer() {
      this.$refs.customerSelect.init()
    },
    //客户参照列表选择后
    customerSelectionsToInput (selections) {
      this.form.customer = selections[0].id;
      this.form.customerName = selections[0].name;
    },
    // 触发联系人参照列表
    refereContact() {
      this.$refs.contactSelect.init()
    },
    //联系人参照列表选择后
    contactSelectionsToInput (selections) {
      this.form.superiorContact = selections[0].id;
      this.form.superiorContactName = selections[0].name;
    },
  }
};
</script>
