<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.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" id="product-category-edit-modal-save" style="margin-left: 15px;" @click="handleSave">确定</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { bindRelation } from '@/api/dealerRelation'
import queryChild from './queryChild.vue'
export default {
  name: 'RelationshipBindingModal',
  mixins: [commonMixin],
  components: { queryChild },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    tabInd: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      form: {},
      rules: {},
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      dealerLevelList: [],
      subDealerSn: '',
      isRelation: '' //  是否已被关联过
    }
  },
  methods: {
    handleDisassociate (val, parentDealerSn) {
      this.subDealerSn = val.key
      this.isRelation = parentDealerSn
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (_this.subDealerSn == '') {
            this.$message.info('请选择下级')
            return
          }
          const formData = {
            subDealerSn: _this.subDealerSn,
            parentDealerSn: _this.nowData && _this.nowData.dealerSn ? _this.nowData.dealerSn : undefined
          }
          if (_this.isRelation) {
            _this.$confirm({
              title: '提示',
              content: '确认要解除原有绑定关系，建立新的绑定关系吗？',
              centered: true,
              closable: true,
              onOk () {
                _this.handleBind(formData)
              }
            })
          } else {
            _this.handleBind(formData)
          }
        } else {
          return false
        }
      })
    },
    handleBind (formData) {
      const _this = this
      _this.spinning = true
      bindRelation(formData).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$emit('ok', formData.parentDealerSn)
          _this.isShow = false
          _this.spinning = false
        } else {
          _this.spinning = 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.subDealerSn = []
      } else {
        this.dealerLevelList = [this.tabInd == '1' ? 'CITY' : 'SPECIAL']
      }
    },
    tabInd (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.dealerLevelList = [newValue == '1' ? 'CITY' : 'SPECIAL']
      }
    }
  }
}
</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>
