<style>

</style>
<template>
    <div class="Business">
      <div class="add-Enclosure">
        <el-button class="crm-btn query-btn crm-btn-mini easy-btn" @click="guanlianBox = true">关联</el-button>
        <el-button class="crm-btn query-btn crm-btn-mini easy-btn" @click="jiechuguanlian">解除关联</el-button>
        <el-button icon="el-icon-plus" class="crm-btn query-btn crm-btn-mini margin-right0" @click="addBusiness">新建商机</el-button>
      </div>
      <el-table
        v-loading="loading"
        stripe
        ref="multipleTable"
        :data="dataList"
        tooltip-effect="dark"
        style="width: 100%"
        row-class-name="font-size-12"
          @selection-change="handleSelectionChange">
          <el-table-column fixed type="selection" width="50"></el-table-column>
        <el-table-column 
            prop="BusinessName"
            label="商机名称"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column 
            prop="BudgetPrice"
            label="商机金额"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column 
            prop="CustomerName"
            label="客户名称"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column 
            prop="BusinessStatusStr"
            label="商机阶段"
            show-overflow-tooltip>
        </el-table-column>
      </el-table>
      <el-dialog :visible.sync="guanlianBox" :modal="false" :close-on-click-modal="false" width="450px" custom-class="transfer-box add-box add-box2">
          <div class="add-tit" slot="title">
              <p><span></span>关联商机</p>
              <span icon="el-icon-close"></span>
          </div>
          <el-form :model="msg" ref="form" class="MyEditForm" >
              <div class="MyEditForm-item">
                  <el-form-item label="商机" class="label-pad-left">
                      <el-select
                          filterable
                          multiple
                          v-model="BusinessIDs" placeholder="请选择">
                          <el-option
                              v-for="item in selectList"
                              :label='item.BusinessName'
                              :value='item.ID'
                              :key='item.ID'>
                          </el-option>
                      </el-select>
                  </el-form-item>
              </div>
          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button type="primary" class="add-box-btn" @click="guanlian()">确 定</el-button>
              <el-button class="add-box-btn add-box-cancel" @click="guanlianBox = false">取 消</el-button>
          </div>
      </el-dialog>
    </div>
</template>
<script>
export default {
  props: ['ID', 'CustomerId'],
  data() {
    return {
      dataList: [],
      loading: false,
      guanlianBox: false,
      BusinessIDs: [],
      selectList: [],
      selectID: [],
      msg: {
        BusinessIDs: '',
        ContactIDs: ''
      }
    };
  },watch:{
    
 },
  mounted() {
    this.GetBusinessByContactID()
    this.GetBusinessList()
    let $this = this
    this.MsgBus.$on('RefreshbusSelect', function (){
      $this.GetBusinessList()
    })
  },
  methods: {
    handleSelectionChange(val) {
      this.selectID = val.map(x=>
        x.ID
      )
    },
    GetBusinessList(){
      this.apipost('/api/Customer/GetBusinessList', {CustomerId: this.CustomerId}, res=>{
        if (res.data.resultCode == 1) {
          this.selectList = res.data.data
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    GetBusinessByContactID(){
      this.loading = true
      this.apipost('/api/Customer/GetBusinessByContactID', {ContactID: this.ID}, res=>{
        if (res.data.resultCode == 1) {
          this.dataList = res.data.data
          this.loading = false
        } else {
          this.loading = false
          this.$message.error(res.data.message);
        }
      })
    },
    addBusiness(){
      let $this= this
      this.MsgBus.$emit('addBusinessBoxOpen', $this.CustomerId, $this.ID)
    },
    guanlian(){
      if (this.BusinessIDs.length < 1) {
        return this.$message.error('青选择需要关联的商机！')
      }
      let str = ''
      this.BusinessIDs.map((x, i)=>{
        str = str + x + ','
      })
      this.msg.ContactIDs = this.ID
      this.msg.BusinessIDs = str.substring(0, str.length - 1)
      this.apipost('/api/Customer/SetBusinessContacts', this.msg, res=>{
        if (res.data.resultCode == 1) {
          this.guanlianBox = false
          this.GetBusinessByContactID()
          this.$message.success('操作成功');
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    jiechuguanlian(){
      if(this.selectID.length < 1) {
        return this.$message.error('请选择数据')
      }
      this.$confirm('确定解除关联？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let str = ''
        this.selectID.map((x, i)=>{
          str = str + x + ','
        })
        let msg = {
          BusinessIDs: str.substring(0, str.length - 1),
          ContactID: this.ID
        }
        this.apipost('/api/Customer/DelBusinessContacts', msg, res=>{
          if (res.data.resultCode == 1) {
            this.selectID = []
            this.GetBusinessByContactID()
            this.$message.success('操作成功');
          } else {
            this.$message.error(res.data.message);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });          
      }); 
    },
  }
}
</script>