<template>
  <a-modal
    centered
    class="relationshipBinding-modal"
    :footer="null"
    :maskClosable="false"
    title="编辑"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <!-- 表单 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="relationshipBinding-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="经销商名称" v-if="nowData">
            {{ nowData.subDealer.dealerName }}
          </a-form-model-item>
          <a-form-model-item label="差价归属">
            <queryChild ref="queryChid" :dealerLevelList="dealerLevelList" @change="handleDisassociate"></queryChild>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="product-category-edit-modal-back" @click="isShow = false" >取消</a-button>
          <a-button type="primary" style="margin-left: 15px;" id="product-category-edit-modal-save" @click="handleSave">确定</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { updateRebate } from '@/api/dealerRelation'
import queryChild from './queryChild.vue'
export default {
  name: 'AssociateModal',
  mixins: [commonMixin],
  components: { queryChild },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      form: {},
      rules: {},
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      dealerLevelList: ['CITY', 'PROVINCE'],
      rebateDealerSn: ''
    }
  },
  methods: {
    handleDisassociate (val) {
      console.log('选中', val)
      this.rebateDealerSn = val ? val.key : ''
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = {
            rebateDealerSn: _this.rebateDealerSn,
            dealerSn: _this.nowData && _this.nowData.subDealer.dealerSn ? _this.nowData.subDealer.dealerSn : undefined
          }
          _this.spinning = true
          updateRebate(formData).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok', formData.dealerSn)
              _this.isShow = false
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.$refs.queryChid.resetForm()
        this.rebateDealerSn = ''
      }else{
        this.$nextTick(()=>{
          this.rebateDealerSn = this.nowData && this.nowData.rebateDealer && this.nowData.rebateDealer.dealerSn ? this.nowData.rebateDealer.dealerSn : ''
          const dealerName = this.nowData && this.nowData.rebateDealer && this.nowData.rebateDealer.dealerName ? this.nowData.rebateDealer.dealerName : ''
          this.$refs.queryChid.setVal(this.rebateDealerSn, dealerName)
        })
      }
    }
  }
}
</script>

<style lang="less">
  .relationshipBinding-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .search-opt{
      padding: 0;
      margin: 0;
      list-style: none;
      li{
        padding: 5px 15px;
      }
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
