<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="higherLevel" :isShowSn="rebateDealerSn" placeholderInfo="请输入上级经销商名称搜索" :dealerLevelList="dealerLevelList" @change="handleHigherLevel"></queryChild>
          </a-form-model-item>
          <a-form-model-item label="指定差价归属">
            <queryChild ref="queryChid" :isShowSn="higherDealerSn" placeholderInfo="请输入指定差价归属经销商名称搜索" :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: [],
      rebateDealerSn: '',
      higherDealerSn: ''
    }
  },
  methods: {
    handleDisassociate (val) {
      console.log('选中', val)
      this.rebateDealerSn = val ? val.key : ''
    },
    handleHigherLevel (e) {
      this.higherDealerSn = e ? e.key : ''
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = {
            parentDealerSn: _this.higherDealerSn,
            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.higherLevel.resetForm()
        this.$refs.queryChid.resetForm()
        this.rebateDealerSn = ''
        this.higherDealerSn = ''
        this.dealerLevelList = []
      } else {
        // 上级经销商
        if (this.nowData && this.nowData.supDealer) {
          this.higherDealerSn = this.nowData.supDealer.dealerSn ? this.nowData.supDealer.dealerSn : ''
          const higherDealerName = this.nowData.supDealer.dealerName ? this.nowData.supDealer.dealerName : ''
          if (higherDealerName) {
            this.$refs.higherLevel.setVal(this.higherDealerSn, higherDealerName)
          }
        }
        // 指定差价归属
        if (this.nowData && this.nowData.rebateDealer) {
          this.rebateDealerSn = this.nowData.rebateDealer.dealerSn ? this.nowData.rebateDealer.dealerSn : ''
          const dealerName = this.nowData.rebateDealer.dealerName ? this.nowData.rebateDealer.dealerName : ''
          this.$refs.queryChid.setVal(this.rebateDealerSn, dealerName)
        }
        // 特约  ['CITY', 'PROVINCE'] 市级经销商 ['PROVINCE']
        if (this.nowData && this.nowData.subDealer) {
          this.dealerLevelList = this.nowData.subDealer.dealerLevel == 'CITY' ? ['PROVINCE'] : ['CITY', 'PROVINCE']
        }
      }
    }
  }
}
</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>
