<template>
  <a-modal
    centered
    class="bindEdit-modal"
    :footer="null"
    :maskClosable="false"
    title="设置绑定"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <div>
        <a-form-model
          id="bindEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="选择中心店" prop="orgSn">
            <orgList ref="orgList" :disabled="itemSn?itemSn:''" @change="orgChange" />
          </a-form-model-item>
          <a-form-model-item label="选择配送店" prop="childSnList">
            <orgMultipleList ref="orgMultipleList" :disabled="itemSn?itemSn:form.orgSn" :params="{ sn: form.orgSn }" @change="orgMultipleChange" />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="bindEdit-modal-save" @click="handleSave">保存</a-button>
          <a-button id="bindEdit-modal-back" @click="isShow = false" style="margin-left: 15px;">关闭</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import orgList from './orgList.vue'
import orgMultipleList from './orgMultipleList.vue'
import { jxsorgRelationSave, jxsorgRelationDetail } from '@/api/jxsorg'
export default {
  name: 'BindEditModal',
  components: { STable, orgList, orgMultipleList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        orgSn: undefined,
        childSnList: undefined
      },
      rules: {
        orgSn: [ { required: true, message: '请选择中心店', trigger: 'change' } ],
        childSnList: [ { required: true, message: '请选择配送店', trigger: 'change' } ]
      }
    }
  },
  methods: {
    //  详情
    getDetail () {
      const _this = this
      jxsorgRelationDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          _this.form.orgSn = res.data.orgSn || undefined
          _this.$refs.orgList.fetchUser(res.data.orgName)
          _this.$refs.orgList.dealerName = { key: res.data.orgSn }
          const childSnList = []
          const childSnListStr = []
          if (res.data.childList) {
            res.data.childList.map(item => {
              childSnList.push({ key: item.sn })
              childSnListStr.push(item.sn)
            })
          }
          _this.form.childSnList = childSnListStr.length == 0 ? undefined : childSnListStr
          _this.$refs.orgMultipleList.setData(childSnList, res.data.childList || [])
          _this.$refs.orgMultipleList.dealerName = childSnList
        }
      })
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.spinning = true
          jxsorgRelationSave(_this.form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              setTimeout(function () {
                _this.isShow = false
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 中心店 change
    orgChange (obj) {
      this.form.orgSn = obj.key || undefined
    },
    // 配送店  change
    orgMultipleChange (obj) {
      const _this = this
      this.form.childSnList = []
      obj.map(item => {
        _this.form.childSnList.push(item.key || undefined)
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.form.childSnList = undefined
        this.$refs.orgList.resetForm()
        this.$refs.orgMultipleList.resetForm()
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

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