<style>
.Contacts .add-contacts{
  text-align: right;
  padding: 0 0 20px 0;
}
.Contacts td{
  font-size: 12px;
}
.Contacts .el-tag.el-tag--info{
  background-color: rgba(0,0,0,0);
  border-color: rgba(0,0,0,0);
  display: inline-block;
  white-space: nowrap; 
  overflow: hidden;
  text-overflow:ellipsis;
}
</style>
<template>
    <div class="Contacts">
      <div class="add-contacts">
        <el-button class="crm-btn query-btn crm-btn-mini easy-btn" @click="guanlian">关联</el-button>
        <el-button class="crm-btn query-btn crm-btn-mini easy-btn" @click="jiechuguanlian" v-if="IsRead === 0">解除关联</el-button>
        <el-button icon="el-icon-plus" class="crm-btn query-btn crm-btn-mini margin-right0" @click="addContactsShow=true">新建联系人</el-button>
      </div>
      <el-table
        v-loading="loading"
        stripe
        ref="multipleTable"
        :data="ContactList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        row-class-name="font-size-12">
        <el-table-column fixed type="selection" width="40"></el-table-column>
        <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">
              <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>
            </template>
        </el-table-column>
      </el-table>
      <div v-if="addContactsShow">
        <addContacts :ID="BusinessIdCustomerId" @addContactsOk="addContactsOk"/>
      </div>
      <el-dialog :modal="false" width="400px"  :visible.sync="dialogTableVisible" :close-on-click-modal="false" class="editTeamPower">
        <div class="add-tit" slot="title">
            <p><span></span>关联商机联系人</p>
            <span icon="el-icon-close"></span>
        </div>
        <div class="form-box" >
            <el-form :model="form" ref="form" class="MyEditForm">
                <div class="MyEditForm-item">
                    <el-form-item label="选择联系人" class="label-pad-left">
                      <el-select
                        filterable
                        multiple
                        v-model="ContactIDsArr" placeholder="请选择">
                        <el-option
                          v-for="item in CustomerContactList"
                          :label='item.ContactName'
                          :value='item.ID'
                          :key='item.ID'>
                        </el-option>
                      </el-select>
                    </el-form-item>
                </div>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" class="add-box-btn" @click="addData()">确 定</el-button>
            <el-button class="add-box-btn add-box-cancel" @click="dialogTableVisible = false">取 消</el-button>
        </div>
    </el-dialog>
    </div>
</template>
<script>
import addContacts from "../dialogModel/addContacts";
export default {
  components: {
    addContacts
  },
  props:{
    BusinessId: {
      type: Number,
      default: 0
    },
    BusinessIdCustomerId: {
      type: Number,
      default: 0
    },
    IsRead: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      loading: false,
      addContactsShow: false,
      ContactList: [],
      ContactIDsArr: [],
      CustomerContactList: [],
      multipleSelection: [],
      form: {
        BusinessID: 0,
        ContactIDs: ''
      }
    };
  },watch:{
      BusinessId: {
          handler(val, oldVal){
            this.GetContactByBusinessID()
          },
          deep:true
      }
  },
  mounted() {
    let $this = this
    this.MsgBus.$on('closeaddContacts', function (){
      $this.addContactsShow = false
    })
    this.GetContactByBusinessID()
    this.getGLcustomerList()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val.map(x=>
        x.ID
      )
    },
    getGLcustomerList(){
      this.apipost('/api/Customer/GetContactByCustomerID', {CustomerId: this.BusinessIdCustomerId}, res=>{
        if (res.data.resultCode == 1) {
          this.loading = false
          this.CustomerContactList = res.data.data
        }
      })
    },
    addData(){
      if(this.ContactIDsArr.length < 1) {
        return this.$message.error('请选择联系人')
      }
      let str = ''
      this.ContactIDsArr.map((x, i)=>{
        str = str + x + ','
      })
      this.form.BusinessID = this.BusinessId
      this.form.ContactIDs = str.substring(0, str.length - 1)
      this.apipost('/api/Customer/SetBusinessContact', this.form, res=>{
        if (res.data.resultCode == 1) {
          this.$message.success('操作成功')
          this.dialogTableVisible = false;
          this.GetContactByBusinessID()
        }
      })
    },
    guanlian(){
      this.dialogTableVisible = true
    },
    jiechuguanlian(){
      if (this.multipleSelection.length < 1) {
        return this.$message.error('请先勾选')
      }
      this.$confirm('确认取消关联?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let str = ''
        this.multipleSelection.map((x, i)=>{
          str = str + x + ','
        })
        let msg = {
          BusinessID: this.BusinessId,
          ContactIDs: str.substring(0, str.length - 1)
        }
        this.apipost('/api/Customer/DelBusinessContact', msg, res=>{
          if (res.data.resultCode == 1) {
            this.$message.success('操作成功')
            this.dialogTableVisible = false;
            this.getGLcustomerList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });          
      }); 
      
    },
    addContactsOk(){
      this.GetContactByBusinessID()
      this.getGLcustomerList()
    },
    SetFirstContact(scope){
      this.apipost('/api/Customer/SetBusinessFirstContact', {BusinessID: this.BusinessId, ID: scope.row.ID}, res=>{
        if (res.data.resultCode == 1) {
          this.$message.success('操作成功');
          this.GetContactByBusinessID();
        }
      })
    },
    GetContactByBusinessID(){
      if(this.BusinessId === 0) return
      this.loading = true
      this.apipost('/api/Customer/GetContactByBusinessID', {BusinessID: this.BusinessId}, res=>{
        if (res.data.resultCode == 1) {
          this.loading = false
          this.ContactList = res.data.data
        }
      })
    },
  }
}
</script>