<template>
  <view class="">
    <u--form
      style="margin: 0 20px"
      labelPosition="left"
      :model="form"
      :rules="rules"
      ref="uForm"
      labelWidth="140rpx"
    >
      <u-form-item label="审批建议" prop="reason" required>
        <u--textarea
          style="width: 100%"
          height="120"
          border="surround"
          placeholder="请输入审批建议"
          v-model="form.reason"
        ></u--textarea>
      </u-form-item>
    </u--form>
    <view class="btnList">
      <u-button
        style="width: 45%; margin-bottom: 10rpx"
        :loading="loading"
        type="success"
        text="申请入库"
        @click="storeManagerApprove"
      >
      </u-button>
      <u-button
        style="width: 45%"
        :loading="loading"
        type="error"
        text="驳回"
        @click="handleReject"
      ></u-button>
    </view>
    <view class="btnConcel" v-if="taskDefinitionKey !== 'deptLeaderApprove'">
      <u-button @click="showAction = true">更多</u-button>
    </view>
    <u-action-sheet
      :actions="actionList"
      :closeOnClickOverlay="true"
      :closeOnClickAction="true"
      title="更多操作"
      :show="showAction"
      @close="showAction = false"
      @select="selectActionClick"
    ></u-action-sheet>
  </view>
</template>

<script>
import {
  approveTaskWithVariablesOther,
  cancelTask,
} from "@/api/wt/index.js";
import { storage, notPass } from "@/api/warehouseManagement/index.js";

export default {
  name: "taskSubmit",
  props: {
    businessId: {
      default: "",
    },
    taskId: {
      default: "",
    },
    id: {
      default: "",
    },
    taskDefinitionKey: {
      default: "",
    },
  },

  data() {
    return {
      showAction: false,
      loading: false,
      taskFormData: {},
      actionList: [
        {
          name: "作废",
          fontSize: "28",
          color: "#ffaa7f",
        },
      ],
      form: {
        reason: "同意",
      },
      rules: {
        reason: {
          type: "string",
          required: true,
          message: "请输入审批建议",
          trigger: "blur",
        },
      },
    };
  },
  mounted() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    async storeManagerApprove() {
      try {
        await this.$refs.uForm.validate();
      } catch (e) {
        return;
      }

      let res = await this.getTableValue();
      let storageData = res.returnStorageData;
      if (!storageData || storageData === false) {
        return;
      }

      storageData.isSkip = 1;

      try {
        this.loading = true;
        let storageRes = await storage(storageData);
        await approveTaskWithVariablesOther({
          id: this.taskId,
          reason: this.form.reason,
          outInId: storageRes.data[0],
          businessId: this.businessId,
          variables: {
            pass: true,
          },
        });
        this.$emit("handleAudit", {
          status: 1,
          title: "入库",
        });
      } catch (error) {
        uni.showToast({ title: "保存失败", icon: "none" });
      } finally {
        this.loading = false;
      }
    },

    async handleReject() {
      try {
        this.loading = true;
        await notPass({
          id: this.taskId,
          reason: this.form.reason,
          businessId: this.businessId,
          variables: {
            pass: false,
          },
        });
        this.$emit("handleAudit", {
          status: 0,
          title: "驳回",
        });
      } catch (error) {
        console.error("驳回操作失败", error);
        uni.showToast({ title: "驳回失败", icon: "none" });
      } finally {
        this.loading = false;
      }
    },

    async syncTaskFormData() {
      try {
        const data = await this.getTableValue();
        this.taskFormData = data || {};
      } catch (e) {
        console.warn("获取 taskForm 数据失败", e);
        this.taskFormData = {};
      }
      return this.taskFormData;
    },

    selectActionClick(item) {
      if (item.name == "作废") {
        uni.showModal({
          title: "提示",
          content: "是否确认作废？",
          success: async (modalRes) => {
            if (modalRes.confirm) {
              if (!this.form.reason) {
                return uni.showToast({
                  title: "审批建议不能为空",
                  icon: "none",
                });
              }
              this.loading = true;
              await this.syncTaskFormData();
              cancelTask({
                taskId: this.taskId,
                id: this.id,
                reason: this.form.reason,
                businessId: this.businessId,
                variables: {
                  taskFormData: this.taskFormData,
                },
              })
                .then(() => {
                  this.loading = false;
                  this.$emit("handleAudit", {
                    title: "作废",
                  });
                })
                .catch(() => {
                  this.loading = false;
                  uni.showToast({ title: "流程作废失败", icon: "none" });
                });
            }
          },
        });
      }
    },

    getTableValue() {
      return new Promise((resolve, reject) => {
        this.$emit("getTableValue", async (data) => {
          resolve(await data);
        });
      });
    },
  },
};
</script>

<style scoped>
.btnList {
  display: flex;
}
.btnConcel {
  margin-top: 20rpx;
}
</style>
