<template>
  <a-modal :visible="visible" title="批量更改位置状态" cancelText="取消" okText="提交" @ok="submit" @cancel="cancel" style="margin-top: 120px;">
    <a-form ref="formRef" :model="formState" :rules="formRules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-item label="新位置状态" name="newStatus">
        <a-select v-model:value="formState.newStatus" placeholder="请选择新位置状态" :options="locStatusOption">
        </a-select>
      </a-form-item>
      <a-form-item ref="msg" label="变更的原因" name="msg">
        <a-textarea v-model:value="formState.msg" placeholder="请输入变更的原因" style="height: 60px;" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
  import {
    message
  } from 'ant-design-vue';
  import {
    changeLocStatus
  } from "@/api/wms/loc";
  import {
    defineComponent,
    reactive,
    ref,
    toRaw,
  } from "vue";
  export default defineComponent({
    props: {
      visible: {
        type: Boolean,
      },
      record: {
        type: Object,
      }
    },
    emit: ["close"],
    setup(props, context) {

      const formRef = ref();

      const formState = reactive({
        ids: props.record,
      });

      const locStatusOption = ref([{
        value: 'Normal',
        label: '正常',
      }, {
        value: 'NoIn',
        label: '禁入',
      }, {
        value: 'NoOut',
        label: '禁出',
      }, {
        value: 'Freeze',
        label: '冻结',
      }]);

      const formRules = {
        newStatus: [{
          required: true,
          message: '请输入新位置状态(必填)',
          trigger: 'blur'
        }],
        msg: [{
          required: true,
          message: '请输入变更的原因(必填)',
          trigger: 'blur'
        }],
      };

      const editKey = "add";

      const submit = (e) => {
        message.loading({
          content: '提交中...',
          key: editKey
        });
        console.log(formState.ids)
        formRef.value
          .validate()
          .then(() => {
            changeLocStatus(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(() => {
                  // cancel();
                });
              }
            });
          })
          .catch(error => {
            console.log('error', error);
          });
      };

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

      return {

        submit,
        cancel,
        formRef,
        formState,
        formRules,

        locStatusOption,

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