<template>
  <a-modal :visible="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="seqCode" label="值编号" name="seqCode">
        <a-input v-model:value="formState.seqCode" placeholder="请输入值编号" />
      </a-form-item>
      <a-form-item ref="seqName" label="名称" name="seqName">
        <a-input v-model:value="formState.seqName" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item ref="minValue" label="起始值" name="minValue">
        <a-input-number v-model:value="formState.minValue" placeholder="请输入起始值" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="incValue" label="自增值" name="incValue">
        <a-input-number v-model:value="formState.incValue" placeholder="请输入自增值" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="maxValue" label="最大值" name="maxValue">
        <a-input-number v-model:value="formState.maxValue" placeholder="请输入最大值" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="currValue" label="当前值" name="currValue">
        <a-input-number v-model:value="formState.currValue" placeholder="请输入当前值" style="width: 100%;"/>
      </a-form-item>
     <!-- <a-form-item ref="deleted" label="删除" name="deleted">
        <a-select v-model:value="formState.deleted" :options="seqDeleted" placeholder="请选择是或者否" />
      </a-form-item>
      <a-form-item ref="createBy" label="录入人" name="createBy">
        <a-input v-model:value="formState.createBy" placeholder="请输入录入人" />
      </a-form-item>
      <a-form-item ref="createTime" label="录入时间" name="createTime">
        <a-input v-model:value="formState.createTime" placeholder="请输入录入时间" />
      </a-form-item>
      <a-form-item ref="updateBy" label="修改人" name="updateBy">
        <a-input v-model:value="formState.updateBy" placeholder="请输入修改人" />
      </a-form-item>
      <a-form-item ref="updateTime" label="修改时间" name="updateTime">
        <a-input v-model:value="formState.updateTime" placeholder="请输入修改时间" />
      </a-form-item> -->
      <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
  import {
    message
  } from 'ant-design-vue';
  import {
    edit
  } from "@/api/wms/seqNum";
  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 formState = reactive({});

      const seqDeleted = ref([{
        value: 'false',
        label: '否',
      }, {
        value: 'true',
        label: '是',
      }]);
      watch(props, (props) => {
        formState.id = props.record.id
        formState.seqCode = props.record.seqCode;
        formState.sqeName = props.record.seqName;
        formState.minValue = props.record.minValue;
        formState.incValue = props.record.incValue;
        formState.maxValue = props.record.maxValue;
        formState.currValue = props.record.currValue;
      })

      const formRules = {
        seqCode: [{
          required: true,
          message: '请输入值编号(必填)',
          trigger: 'blur'
        }],
        sqeName: [{
          required: true,
          message: '请输入名称(必填)',
          trigger: 'blur'
        }],
        minValue: [{
          required: true,
          message: '请输入起始值(必填)',
          trigger: 'blur',
          type:'number'
        }],
        incValue: [{
          required: true,
          message: '请输入自增值(必填)',
          trigger: 'blur',
          type:'number'
        }],
        maxValue: [{
          required: true,
          message: '请输入最大值(必填)',
          trigger: 'blur',
          type:'number'
        }],
        currValue: [{
          required: true,
          message: '请输入当前值(必填)',
          trigger: 'blur',
          type:'number'
        }],
      };

      const editKey = "add";

      const submit = (e) => {
        message.loading({
          content: '提交中...',
          key: editKey
        });
        formRef.value
          .validate()
          .then(() => {
            edit(toRaw(formState)).then((response) => {
              if (response.success) {
                message.success({
                  content: '保存成功',
                  key: editKey,
                  duration: 1
                }).then(() => {
                  cancel();
                });
              } else {
                message.error({
                  content: response.msg,
                  key: editKey,
                  duration: 1
                }).then(() => {
                });
              }
            });
          })
          .catch(error => {
            console.log('error', error);
          });
      };

      const cancel = (e) => {
        formRef.value.resetFields();
        context.emit("close", false);
      };

      return {

        submit,
        cancel,
        formRef,
        formState,
        formRules,
        seqDeleted,

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