<template>
  <div v-if="isShow">
    <a-modal
      centered
      class="merge-customer-modal"
      :footer="null"
      :maskClosable="false"
      v-drag
      title="合并"
      v-model="isShow"
      @cancle="isShow=false"
      width="35%">
      <a-form-model
        id="bulkPartsTypeEdit-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="主体客户:">
          <div>{{ mainCustomerName }}</div>
        </a-form-model-item>
        <a-form-model-item label="被合并的客户名称:" prop="customerSnSource">
          <custList ref="custList" @change="custChange" placeholder="输入客户名称搜索"></custList>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="modal-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="modal-save" @click="handleSave">确定</a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { custMerge } from '@/api/customer'
import custList from '@/views/common/custList.vue'
export default {
  name: 'SalesRecordModal',
  components: { custList },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default(){
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        customerSnTarget: '', // 主体客户sn 
        customerSnSource: '', // 被合并客户sn
      },
      mainCustomerName: '', // 主体客户名称
      rules: {
        customerSnSource: [
          { required: true, message: '输入客户名称搜索', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 客户 change
    custChange (obj, row) {
      if (row) {
        this.form.customerSnSource = row && row.customerSn || undefined
      } else {
        this.form.customerSnSource = undefined
      }
    },
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.confirmedSubmit()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    confirmedSubmit () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定要合并吗？合并后数据不可恢复。',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          custMerge(_this.form).then(res => {
            if(res.status == 200){
              _this.$message.info(res.message)
              _this.$emit('close',true)
            }
          })
        },
        onCancel () {
          console.log('Cancel')
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.form.customerSnSource = undefined
        this.form.customerSnTarget = undefined
        this.mainCustomerName = undefined
        this.$refs.custList.setData(undefined)
      }else{
        this.mainCustomerName = this.params.customerName
        this.form.customerSnTarget = this.params.customerSn
      }
    }
  }
}
</script>

<style lang="less">
  .merge-customer-modal{
    .ant-modal-body {
      padding: 20px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
