<template>
  <a-modal
    centered
    class="shelfSet-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="数字货架基础设置"
    v-model="isShow"
    @cancle="isShow = false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <p style="text-align: center;margin: 0 0 20px;font-size: 14px;font-weight: bold;">{{ nowData && nowData.shelfName || '--' }}</p>
      <a-form-model
        id="shelfSet-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="关联客户" prop="customerSn">
          <custList ref="custList" :isEnabled="true" @change="custChange"></custList>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="shelfSet-basicInfo-modal-save" @click="handleSave">保存</a-button>
        <a-button id="shelfSet-basicInfo-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import custList from '@/views/common/custList.vue'
import { shelfSave } from '@/api/shelf'
export default {
  name: 'ShelfSetBasicInfoModal',
  components: { VSelect, custList },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        customerSn: undefined
      },
      rules: {
        customerSn: [{ required: true, message: '请选择关联客户', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 客户 change
    custChange (obj) {
      this.form.customerSn = obj.key || undefined
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.shelfSn = _this.nowData && _this.nowData.shelfSn
          _this.spinning = true
          shelfSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok', res.data)
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.$refs.custList.resetForm()
      } else {
        if (this.nowData.customerSn) {
          const _this = this
          this.$nextTick(() => {
            _this.form.customerSn = _this.nowData.customerSn
            _this.$refs.custList.fetchUser(_this.nowData.customerName)
            _this.$refs.custList.dealerName = { key: _this.nowData.customerSn, label: _this.nowData.customerName, row: _this.nowData }
          })
        }
      }
    }
  }
}
</script>

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