<style>
.Contacts .add-contacts{
  text-align: right;
  padding: 0 0 20px 0;
}
.Contacts td{
  font-size: 12px;
}
</style>
<template>
    <div class="Contacts">
      <div class="add-contacts">
        <el-button icon="el-icon-plus" class="crm-btn query-btn crm-btn-mini margin-right0" @click="isPass=false,contactID=0,addContactsShow=true">新建联系人</el-button>
      </div>
      <el-table
        v-loading="loading"
        stripe
        ref="multipleTable"
        :data="ContactList"
        tooltip-effect="dark"
        style="width: 100%"
        row-class-name="font-size-12">
        <el-table-column 
            prop="ContactName"
            label="姓名"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column 
            prop="ContactPhone"
            label="手机"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column 
            prop="ContactDutyStr"
            label="职务"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column 
            prop="IsFirstContact"
            label="操作"
            show-overflow-tooltip>
            <template slot-scope="scope">
              <div style="display: flex;">
                <p v-if="scope.row.IsFirstContact === 1" class="font-color-warning">首要联系人</p>
                <p v-else-if="IsRead === 0 &&　scope.row.IsFirstContact !== 1" class="font-color-link cp" @click="SetFirstContact(scope)">设为首要联系人</p>
                <span v-if="userInfo.EmployeeId==scope.row.CreateBy" class="font-color-link cp" style="margin-left: 10px;" @click="editContacts(scope.row)">编辑</span>
                <span v-if="userInfo.EmployeeId==scope.row.CreateBy" class="font-color-link cp" style="margin-left: 10px;" @click="EditPassword(scope.row)">重置密码</span>
              </div>
            </template>
        </el-table-column>
      </el-table>
      <div v-if="addContactsShow">
        <addContacts :ID="CustomerId" :contactID="contactID" @addContactsOk="addContactsOk"/>
      </div>

      <el-dialog :modal="false" :visible.sync="isPass" :close-on-click-modal="false" class="addContacts"
      width="400px">
        <div class="add-tit" slot="title">
            <p><span></span>重置密码</p>
            <span icon="el-icon-close"></span>
        </div>
        <div class="form-box" >
            <el-form :model="PasswordData" ref="PasswordData" class="MyEditForm" :rules="rules">
                <el-row :gutter="30">
                    <el-col :span="24">
                        <div class="MyEditForm-item">
                            <el-form-item label="新密码" prop="Password">
                              <el-input type="text" show-Password placeholder="请输入新密码" v-model="PasswordData.Password"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" class="add-box-btn" @click="submitForm('PasswordData')">确 定</el-button>
            <el-button class="add-box-btn add-box-cancel" @click="isPass = false, resetForm('PasswordData')">取 消</el-button>
        </div>
    </el-dialog>
    </div>
</template>
<script>
import addContacts from "../dialogModel/addContacts";
export default {
  components: {
    addContacts
  },
  props:{
    CustomerId: {
      type: Number,
      default: 0
    },
    IsRead: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      loading: false,
      addContactsShow: false,
      ContactList: [],
      userInfo: {},
      contactID: 0,
      isPass: false,
      PasswordData:{
        ContactId: 0,
        ContactName:"",
        Password:""
      },
      rules: {
        Password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          // {
          //   pattern: /^(?=.*[a-zA-Z])(?=.*\d).{6,20}$/,
          //   message: '密码由6-20位字母、数字组成'
          // }
        ],
      },
    };
  },watch:{
      CustomerId: {
          handler(val, oldVal){
            this.GetContactByCustomerID()
          },
          deep:true
      }
  },
  mounted() {
    let userInfo = this.getLocalStorage();
    this.userInfo = userInfo;
    let $this = this
    this.MsgBus.$on('closeaddContacts', function (){
      $this.addContactsShow = false
    })
    this.GetContactByCustomerID()
  },
  methods: {
    submitPassword(){
        this.apipost('/api/Customer/SetCustomerContactPW', this.PasswordData, res=>{
          if (res.data.resultCode == 1) {
            this.isPass = false
            this.$message.success("操作成功");
          }
        })
    },
    submitForm(addMsg) {
      //提交创建、修改表单
      this.$refs[addMsg].validate((valid) => {
        if (valid) {
          this.submitPassword()
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      // 重置表单
      this.PasswordData = {
        ContactId: 0,
        ContactName:"",
        Password:""
      };
      this.$refs[formName].resetFields();
    },
    EditPassword(item){
      this.PasswordData = {
        ContactId: item.ID,
        ContactName: item.ContactName,
        Password:""
      },
      this.isPass = true
    },
    editContacts(item){
      this.contactID=item.ID
      this.addContactsShow=true
      this.isPass = false
    },
    addContactsOk(){
      this.GetContactByCustomerID()
    },
    SetFirstContact(scope){
      this.apipost('/api/Customer/SetFirstContact', {CustomerId: this.CustomerId, ID: scope.row.ID}, res=>{
        if (res.data.resultCode == 1) {
          this.$message.success('操作成功');
          this.GetContactByCustomerID();
        }
      })
    },
    GetContactByCustomerID(){
      if(this.CustomerId === 0) return
      this.loading = true
      this.apipost('/api/Customer/GetContactByCustomerID', {CustomerId: this.CustomerId}, res=>{
        if (res.data.resultCode == 1) {
          this.loading = false
          this.ContactList = res.data.data
        }
      })
    },
  }
}
</script>