<template>
  <a-modal
    centered
    class="chainTransferOut-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="新增盘点单"
    v-model="isShow"
    @cancle="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="chainTransferOut-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="盘点类型" prop="checkType">
          <a-radio-group v-model="form.checkType" @change="checkTypeChange">
            <a-radio v-for="(item,index) in checkTypeList" :key="index" :value="item.code">{{ item.dispName }}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="是否区分仓库" prop="warehouseFlag" v-if="form.checkType=='SELECT'">
          <a-radio-group v-model="form.warehouseFlag" @change="warehouseFlagChange">
            <a-radio v-for="(item,index) in warehouseFlagList" :key="index" :value="item.code">{{ item.dispName }}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="盘点仓库" prop="warehouseSnList" v-if="form.checkType=='SELECT' && form.warehouseFlag=='1'">
          <a-checkbox-group v-model="form.warehouseSnList">
            <a-checkbox v-for="(item,index) in warehouseList" :key="index" :value="item.warehouseSn">{{ item.name }}</a-checkbox>
          </a-checkbox-group>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="chainTransferOut-basicInfo-modal-save" @click="handleSave">保存</a-button>
        <a-button id="chainTransferOut-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 { checkWarehouseSave, checkWarehouseWarehouse } from '@/api/checkWarehouse'
export default {
  name: 'ChainTransferOutBasicInfoModal',
  components: { VSelect },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 20 }
      },
      form: {
        checkType: 'ALL',
        warehouseFlag: '0',
        warehouseSnList: undefined
      },
      rules: {
        checkType: [{ required: true, message: '请选择盘点类型', trigger: 'change' }],
        warehouseFlag: [{ required: true, message: '请选择是否区分仓库', trigger: 'change' }],
        warehouseSnList: [{ required: true, message: '请选择盘点仓库', trigger: 'change' }]
      },
      checkTypeList: [
        { dispName: '全盘', code: 'ALL' },
        { dispName: '自选盘点', code: 'SELECT' }
      ],
      warehouseFlagList: [
        { dispName: '不区分仓库', code: '0' },
        { dispName: '区分仓库', code: '1' }
      ],
      warehouseList: []
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          if (form.checkType == 'ALL') {
            delete form.warehouseFlag
          }
          if (form.warehouseSnList) {
            form.warehouseSnList = form.warehouseSnList.join(',')
          }
          _this.spinning = true
          checkWarehouseSave(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
        }
      })
    },
    checkTypeChange (e) {
      this.form.warehouseFlag = '0'
      this.form.warehouseSnList = undefined
    },
    warehouseFlagChange (e) {
      this.form.warehouseSnList = undefined
    },
    // 获取仓库列表
    getWarehouseList () {
      checkWarehouseWarehouse({}).then(res => {
        if (res.status == 200) {
          this.warehouseList = res.data || []
        } else {
          this.warehouseList = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        this.getWarehouseList()
      }
    }
  }
}
</script>

<style lang="less">
.chainTransferOut-basicInfo-modal {
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
