<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="houseCode" label="仓库号" name="houseCode">
        <a-input v-model:value="formState.houseCode" placeholder="请输入仓库号" />
      </a-form-item>
      <a-form-item ref="taskCode" label="任务号" name="taskCode">
        <a-input v-model:value="formState.taskCode" placeholder="请输入任务号" />
      </a-form-item>
      <a-form-item label="优先级" name="priority">
        <a-select v-model:value="formState.priority" placeholder="请选择优先级" :options="taskPriority">
        </a-select>
      </a-form-item>
      <a-form-item label="任务类型" name="taskType">
        <a-select v-model:value="formState.taskType" placeholder="请选择任务类型" :options="taskTypes">
        </a-select>
      </a-form-item>
      <a-form-item ref="deliver" label="搬运类型" name="deliver">
        <a-input v-model:value="formState.deliver" placeholder="请输入搬运类型" />
      </a-form-item>
      <a-form-item label="方向标志" name="directFlag">
        <a-select v-model:value="formState.directFlag" placeholder="请选择方向标志" :options="directFlags">
        </a-select>
      </a-form-item>
      <a-form-item label="任务发送类型" name="sendType">
        <a-select v-model:value="formState.sendType" placeholder="请选择任务发送类型" :options="taskSendType">
        </a-select>
      </a-form-item>
      <a-form-item label="任务状态" name="status">
        <a-select v-model:value="formState.status" placeholder="请选择任务状态" :options="taskStatus">
        </a-select>
      </a-form-item>
      <a-form-item ref="palletCode" label="托盘号" name="palletCode">
        <a-input v-model:value="formState.palletCode" placeholder="请输入托盘号" />
      </a-form-item>
      <a-form-item ref="sourceLoc" label="WMS源地址" name="sourceLoc">
        <a-input v-model:value="formState.sourceLoc" placeholder="请输入WMS源地址" />
      </a-form-item>
      <a-form-item ref="sourceLocParent" label="WMS源地址父节点" name="sourceLocParent">
        <a-input v-model:value="formState.sourceLocParent" placeholder="请输入WMS源地址父节点" />
      </a-form-item>
      <a-form-item ref="sourceLocWcs" label="WCS源地址" name="sourceLocWcs">
        <a-input v-model:value="formState.sourceLocWcs" placeholder="请输入WCS源地址" />
      </a-form-item>
      <a-form-item ref="targetLoc" label="目标位置" name="targetLoc">
        <a-input v-model:value="formState.targetLoc" placeholder="请输入目标位置" />
      </a-form-item>
      <a-form-item ref="targetLocParent" label="目标位置父节点" name="targetLocParent">
        <a-input v-model:value="formState.targetLocParent" placeholder="请输入目标位置父节点" />
      </a-form-item>
      <a-form-item ref="targetLocWcs" label="WCS目标地址" name="targetLocWcs">
        <a-input v-model:value="formState.targetLocWcs" placeholder="请输入WCS目标地址" />
      </a-form-item>
      <a-form-item ref="invoiceCode" label="单据号" name="invoiceCode">
        <a-input v-model:value="formState.invoiceCode" placeholder="请输入单据号" />
      </a-form-item>
      <a-form-item ref="invoiceItemCode" label="单据明细号" name="invoiceItemCode">
        <a-input v-model:value="formState.invoiceItemCode" placeholder="请输入单据明细号" />
      </a-form-item>
      <a-form-item ref="skuDesc" label="物料描述" name="skuDesc">
        <a-input v-model:value="formState.skuDesc" placeholder="请输入物料描述" />
      </a-form-item>
      <a-form-item ref="skuName" label="物料名称" name="skuName">
        <a-input v-model:value="formState.skuName" placeholder="请输入物料名称" />
      </a-form-item>
      <a-form-item ref="beginDate" label="开始时间" name="beginDate">
        <a-date-picker v-model:value="formState.beginDate" style="width: 100%" />
      </a-form-item>
      <a-form-item ref="finishDate" label="完成时间" name="finishDate">
        <a-date-picker v-model:value="formState.finishDate" style="width: 100%" />
      </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/task";
  import {
    comparetime,
  } from "@/tools/dateTimeUtil";
  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({});

      watch(props, (props) => {
        formState.taskCode = props.record.taskCode;
        formState.priority = props.record.priority;
        formState.taskType = props.record.taskType;
        formState.status = props.record.status;
        formState.directFlag = props.record.directFlag;
        // formState.beginDate = props.record.beginDate;
        // formState.finishDate = props.record.finishDate;
      })

      const taskPriority = ref([{
        value: '2',
        label: '高',
      }, {
        value: '1',
        label: '中',
      }, {
        value: '0',
        label: '低',
      }]);

      const taskTypes = ref([{
        value: 'In',
        label: '入库',
      }, {
        value: 'Out',
        label: '出库',
      }, {
        value: 'Move',
        label: '移库',
      }, {
        value: 'CheckOut',
        label: '抽检出库',
      }, {
        value: 'CheckReturn',
        label: '抽检返库',
      }, {
        value: 'InventoryOut',
        label: '盘点出库',
      }, {
        value: 'InventoryReturn',
        label: '盘点返库',
      }, {
        value: 'EmptyIn',
        label: '空托入库',
      }, {
        value: 'EmptyOut',
        label: '空托出库',
      }, {
        value: 'PickReturn',
        label: '拣选返库',
      }, {
        value: 'MoveCommand',
        label: '单机移动指令',
      }, {
        value: 'AGVCommand',
        label: 'AGV移动指令',
      }]);

      const taskStatus = ref([{
        value: 'New',
        label: '新建',
      }, {
        value: 'Doing',
        label: '进行中',
      }, {
        value: 'Error',
        label: '异常',
      }, {
        value: 'Cancel',
        label: '取消',
      }, {
        value: 'Over',
        label: '结束',
      }]);

      const taskSendType = ref([{
        value: 'Manual',
        label: '手动下发',
      }, {
        value: 'Scheduler',
        label: '定时器下发',
      }, {
        value: 'Direct',
        label: '直接下发',
      }]);

      const directFlags = ref([{
        value: 'In',
        label: '向里',
      }, {
        value: 'Out',
        label: '向外',
      }, {
        value: 'Move',
        label: '移动',
      }]);

      const formRules = {
        taskCode: [{
          required: true,
          message: '请输入任务号(必填)',
          trigger: 'blur',
          type: 'number',
        }],
        priority: [{
          required: true,
          message: '请选择优先级(必选)',
          trigger: 'blur',
        }],
        taskType: [{
          required: true,
          message: '请选择任务类型(必选)',
          trigger: 'blur'
        }],
        directFlag: [{
          required: true,
          message: '请选择方向标志(必选)',
          trigger: 'change'
        }],
      };

      const editKey = "add";

      const submit = (e) => {

        let beginTime = formState.beginDate; //计划开始时间
        console.log("pbeginTimme", beginTime)

        let endTime = formState.finishDate; //计划结束时间
        console.log("pendTimme", endTime)
        console.log("时间", formState)
        if (comparetime(beginTime, endTime)) {
          console.log("beginTime > endTime")
          message.error({
            content: '计划结束时间不能早于计划开始时间',
            duration: 1
          }).then(() => {
            // cancel();
          });
          return;
        } else {
          console.log("TIJIAO")
          console.log('JSON.stringify(formState)', JSON.stringify(formState))
          formRef.value
            .validate()
            .then(() => {
              edit(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 cancel = (e) => {
        formRef.value.resetFields();
        context.emit("close", false);
      };

      return {

        submit,
        cancel,
        formRef,
        formState,
        taskPriority,
        taskTypes,
        taskSendType,
        taskStatus,
        directFlags,
        formRules,

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