<template>
  <a-modal
    centered
    class="purchaseReceipt-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    :title="itemSn?'编辑':'新增'"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="purchaseReceipt-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <!-- 只显示福麦斯供应商-->
        <a-form-model-item label="供应商名称" prop="supplierSn">
          <supplier v-model="form.supplierSn" enableFlag="1" pageSn="福麦斯" isPermission placeholder="请输入供应商名称"></supplier>
        </a-form-model-item>
        <a-form-model-item label="仓库仓位" prop="warehouseSn">
          <a-select
            v-if="warehouseCascadeData && warehouseCascadeData.length>0"
            id="purchaseReceipt-basicInfo-warehouseSn"
            @change="changeWarehouse"
            placeholder="请选择仓库"
            :allowClear="false"
            v-model="form.warehouseSn"
            style="width: 49%;margin-right:1%;">
            <a-select-option v-for="item in warehouseCascadeData" :value="item.warehouseSn">
              {{ item.name }}
            </a-select-option>
          </a-select>
          <a-select
            id="purchaseReceipt-basicInfo-warehouseLocationSn"
            placeholder="请选择仓位"
            :allowClear="false"
            v-model="form.warehouseLocationSn"
            :disabled="!cascadeList"
            style="width: 50%;">
            <a-select-option v-for="con in cascadeList" :value="con.warehouseLocationSn">
              {{ con.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="客户名称" >
          <span>{{ (itemData&&itemData.dealerName)||'--' }}</span>
        </a-form-model-item>
        <a-form-model-item label="关联单号" >
          <span>{{ (itemData&&itemData.lockBizNo)||'--' }}</span>
        </a-form-model-item>
        <a-form-model-item label="备注">
          <span>{{ (itemData&&itemData.bizRemark)||'--' }}</span>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="purchaseReceipt-basicInfo-modal-cancle" @click="isShow = false">取消</a-button>
        <a-button style="margin-left: 15px;" type="primary" id="purchaseReceipt-basicInfo-modal-save" @click="handleSave">确定</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
import supplier from '@/views/common/supplier.js'
import warehouse from '@/views/common/chooseWarehouse.js'
// 接口
import { detailBaseSave } from '@/api/purchase'
import { warehouseCascadeList } from '@/api/warehouse'
export default {
  name: 'PurchaseReceiptBasicInfoModal',
  mixins: [commonMixin],
  components: { VSelect, supplier, warehouse },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    },
    itemData: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // form 表单样式设置
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 16 }
      },
      warehouseCascadeData: [], // 仓库仓位级联列表
      cascadeList: null, // 仓位列表
      form: {
        supplierSn: undefined, // 供应商
        warehouseSn: undefined, // 仓库
        warehouseLocationSn: undefined // 仓位
      },
      // 表单验证规则
      rules: {
        supplierSn: [
          { required: true, message: '请选择供应商名称', trigger: 'change' }
        ],
        warehouseSn: [{ required: true, message: '请选择入库仓库', trigger: 'change' }]
      }
    }
  },
  methods: {
    //  获取仓库仓位  级联列表
    getWarehouseCascade () {
      const _this = this
      warehouseCascadeList({}).then(res => {
        if (res.status == 200) {
          _this.warehouseCascadeData = res.data
          _this.form.supplierSn = '544577718475575296'
        } else {
          _this.warehouseCascadeData = []
        }
      })
    },
    // 仓库 change
    changeWarehouse (val) {
      const pos = this.warehouseCascadeData.findIndex(item => item.warehouseSn === val)
      if (pos != -1) {
        this.cascadeList = this.warehouseCascadeData[pos].warehouseLocationList || null
        this.form.warehouseLocationSn = (this.cascadeList && this.cascadeList.length > 0) ? this.cascadeList[0].warehouseLocationSn : undefined
      }
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          if (!form.warehouseLocationSn) {
            _this.$message.warning('请选择仓位')
            return
          }
          form.sparePartsSn = _this.itemSn
          _this.spinning = true
          detailBaseSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok')
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    'form.supplierSn' (newVal, oldVal) {
      if (newVal == '544577718475575296') {
        this.form.warehouseSn = '546323677073133568'
        this.changeWarehouse('546323677073133568')
      }
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields()
        })
      } else {
        this.getWarehouseCascade()
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .purchaseReceipt-basicInfo-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
    .ant-form-item:nth-child(5){
      margin-bottom: 0px !important;
    }
  }

</style>
