<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="handleAudit2(1)"
      >
      </u-button>
      <u-button
        style="width: 45%"
        :loading="loading"
        type="error"
        text="驳回"
        @click="handleAudit2(0)"
      ></u-button>
    </view>
  </view>
</template>

<script>
import {
  outsourceAssign,
  outsourceNotPass,
} from "@/api/wt/outsourcedWarehousing.js";

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

  data() {
    return {
      loading: false,
      form: {
        reason: "同意",
      },
      rules: {
        reason: {
          type: "string",
          required: true,
          message: "请输入审批建议",
          trigger: "blur",
        },
      },
    };
  },
  mounted() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    handleAudit2(status) {
      this.$refs.uForm
        .validate()
        .then(() => {
          this._approveTaskThorVariables(status);
        })
        .catch(() => {});
    },

    async _approveTaskThorVariables(status) {
      this.loading = true;
      try {
        if (status == 1) {
          await outsourceAssign({
            businessId: this.businessId,
            id: this.taskId,
            reason: this.form.reason,
            pass: true,
          });
          this.$emit("handleAudit", {
            status,
            title: "",
          });
        } else if (status == 0) {
          await outsourceNotPass({
            businessId: this.businessId,
            id: this.taskId,
            reason: this.form.reason,
            pass: false,
          });
          this.$emit("handleAudit", {
            status,
            title: "驳回",
          });
        }
      } catch (error) {
        console.error("审批操作失败", error);
        uni.showToast({
          title: status == 1 ? "审批失败" : "驳回失败",
          icon: "none",
        });
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.btnList {
  display: flex;
}
</style>
