<template>
  <a-modal :open="visible" title="新增盘点单明细" cancelText="取消" okText="提交" @ok="submit" @cancel="cancel">
    <a-form ref="formRef" :model="formState" :rules="formRules" :label-col="labelCol" :wrapper-col="wrapperCol">

      <a-form-item ref="invoiceCode" label="单据号" name="invoiceCode">
         <a-input v-model:value="formState.invoiceCode" />
       </a-form-item>

       <a-form-item ref="skuCode" label="SKU编号" name="skuCode">
         <a-input v-model:value="formState.skuCode" placeholder="请输入SKU编号" />
       </a-form-item>
       <a-form-item ref="skuName" label="SKU名称" name="skuName">
         <a-input v-model:value="formState.skuName" placeholder="请选择SKU名称" />
       </a-form-item>

       <a-form-item ref="batch" label="批次" name="batch">
         <a-input v-model:value="formState.batch" placeholder="请选择批次" />
       </a-form-item>
       <a-form-item ref="skuQty" label="实际数量" name="skuQty">
         <a-input-number v-model:value="formState.skuQty" placeholder="请输入实际数量" style="width: 100%" />
       </a-form-item>
       <a-form-item label="特殊库存" name="spInv">
         <a-select v-model:value="formState.spInv" :options="checkBillDetailSpInv" placeholder="请选择特殊库存">
         </a-select>
       </a-form-item>
       <a-form-item label="库存类型" name="invType">
         <a-select v-model:value="formState.invType" :options="checkBillDetailInvType" placeholder="请选择库存类型">
         </a-select>
       </a-form-item>
       <a-form-item label="质量标志" name="qualityFlag">
         <a-select v-model:value="formState.qualityFlag" :options="checkBillDetailQualityFlag" placeholder="请选择换质量标志">
         </a-select>
       </a-form-item>
      <a-form-item ref="baseUnit" label="物流单位" name="baseUnit">
        <a-input v-model:value="formState.baseUnit" placeholder="请输入物流单位" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
    import {
    message
  } from '@hwork/ant-design-vue';
  import {
    save
  } from "@/api/wms/checkDetail";

  import {
    defineComponent,
    reactive,
    ref,
    toRaw,
    watch
  } from "vue";
  export default defineComponent({
    props: {
      visible: {
        type: Boolean,
      },
      record: {
        type: Object,
      }
    },
    emit: ["close"],
    setup(props, context) {

      const formRef = ref();




      const checkBillDetailSpInv = ref([{
        value: '1',
        label: '是',
      }, ]);
      const checkBillDetailQualityFlag = ref([{
          value: 'Q',
          label: '合格',
        },
        {
          value: 'N',
          label: '不合格',
        },
        {
          value: 'W',
          label: '待检',
        },
        {
          value: 'E',
          label: '免检',
        },
        {
          value: 'T',
          label: '试验料',
        },
        {
          value: 'S',
          label: '待确定',
        },
        {
          value: 'C',
          label: '待验',
        },
        {
          value: 'D',
          label: '冻结',
        },
      ]);
      const checkBillDetailInvType = ref([{
          value: '满托',
          label: '满托',
        },
        {
          value: '非满托',
          label: '非满托',
        },
        {
          value: '混托',
          label: '混托',
        },
        {
          value: '非混托',
          label: '非混托',
        },
      ]);

      const state = reactive({
        options: [],
        skus: ref[{}]
      });

      const formState = reactive({
        baseUnit: "件",
      });

      watch(props, (props) => {
        formState.invoiceCode = props.record.invoiceCode;
      })


      //获取单价
      const checkunitPrice = async (rule, value) => {
        console.log(rule, value);
      };
      //获取数量
      const checkqty = async (rule, value) => {
        console.log(rule, value);
      };


      const formRules = {
        skuCode: [{
          required: true,
          message: '请输入SKU编号',
          trigger: 'blur'
        }, ],
        skuName: [{
          required: true,
          message: '请输入SKU名称',
          trigger: 'blur'
        }, ],

        batch: [{
          required: true,
          message: '请输入批次',
          trigger: 'blur'
        }, ],

        actQty: [{
          required: true,
          message: '请输入实际数量',
          trigger: 'blur',
          type: 'number',
        }, ],

        invType: [{
          required: true,
          message: '请输入库存类型',
          trigger: 'blur'
        }, ],
        qualityFlag: [{
          required: true,
          message: '请输入质量标志',
          trigger: 'blur'
        }, ],
        qty: [{
          required: true,
          message: '请输入出库数量',
          trigger: 'change',
          type: 'number',
        }, ],
      };



      const submit = (e) => {
        formRef.value
          .validate()
          .then(() => {
            save(toRaw(formState)).then((response) => {
              console.log("监听报错", response)
              if (response.success) {
                message.success({
                  content: '保存成功',
                  duration: 1
                }).then(() => {
                  cancel();
                });
              } else {
                message.error({
                  content: response.msg,
                  duration: 1
                }).then(() => {
                  // cancel();
                });
              }
            });
          })
          .catch(error => {
            console.log('error', error);
          });
      }

      const valueChanged = (e) => {
        console.log(e)

      }
      const cancel = (e) => {
        formRef.value.resetFields();
        context.emit("close", false);
      };
      // const handleChange = (e) => {
      //   context.emit("状态值改变事件", e);
      // };


      return {
        state,
        submit,
        cancel,
        formRef,
        formState,
        formRules,
        // projectStatus,
        valueChanged,
        checkBillDetailSpInv,
        checkBillDetailInvType,
        checkBillDetailQualityFlag,


        labelCol: {
          span: 8
        },
        wrapperCol: {
          span: 12
        },
      };
    },
  });
</script>
