<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="itemParent" label="列表项的父节点" name="itemParent">
        <a-input v-model:value="formState.itemParent" placeholder="列表项的父节点" />
      </a-form-item> -->
      <a-form-item ref="item" label="列表项" name="item">
        <a-input v-model:value="formState.item" placeholder="请输入列表项" />
      </a-form-item>
      <a-form-item ref="listId" label="编号" name="listId">
        <a-input v-model:value="formState.listId" placeholder="请输入编号" />
      </a-form-item>
      <a-form-item ref="listDesc" label="名称" name="listDesc">
        <a-input v-model:value="formState.listDesc" placeholder="请输入名称" />
      </a-form-item>
      <!-- <a-form-item ref="deleted" label="删除" name="deleted">
        <a-select v-model:value="formState.deleted" :options="listDeleted" 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 '@hwork/ant-design-vue';
  import {
    edit
  } from "@/api/wms/list";
  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 listDeleted = ref([{
        value: 'false',
        label: '否',
      }, {
        value: 'true',
        label: '是',
      }]);
      watch(props, (props) => {
        formState.id = props.record.id
        formState.item = props.record.item;
        formState.listId = props.record.listId;
        formState.listDesc = props.record.listDesc;
      })

      const formRules = {
        /* itemParent: [{
          required: true,
          message: '请输入列表项的父节点(必填)',
          trigger: 'blur'
        }], */
        item: [{
          required: true,
          message: '请输入列表项(必填)',
          trigger: 'blur'
        }],
        listId: [{
          required: true,
          message: '请输入编号(必填)',
          trigger: 'blur'
        }],
        listDesc: [{
          required: true,
          message: '请输入名称(必填)',
          trigger: 'blur'
        }],
      };

      const editKey = "add";

      const submit = (e) => {
        formRef.value
        .validate()
        .then(() => {
            message.loading({
              content: '提交中...',
              key: editKey
            });
            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,
        listDeleted,

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