<template>
  <xcxZCinfo
    :pageType
    :id
    :audit
    :isModdle
    :name
    :queryData
    :taskId
    :assetId
    :type
    ref="xcxZCinfoRef"
    @submit-reject="submitReject"
  >
    <el-row
      style="display: flex; justify-content: center; padding-bottom: 50px"
      v-if="name === 'MyXcxzc'"
    >
      <el-button
        type="danger"
        @click="sendBack"
        v-if="pageType === 'look' && audit != true"
        >返回</el-button
      >
      <el-button type="danger" @click="sendBack" v-if="pageType === 'change' || pageType === 'add'"
        >取消</el-button
      >
      <el-button
        type="primary"
        @click="newAssets()"
        :loading="loading"
        v-if="pageType === 'change' || pageType === 'add'"
        >提交</el-button
      >
    </el-row>
    <el-row
      v-if="name === 'TaskManagement-info'"
      style="display: flex; justify-content: center; padding-bottom: 50px"
    >
      <el-button
        type="danger"
        @click="sendBack"
        v-if="pageType === 'look' && audit != 'true'"
        >返回</el-button
      >
      <el-button
        type="danger"
        :disabled="false"
        @click="newAssets1(0)"
        v-if="pageType === 'change' || (audit == 'true' && pageType == 'look')"
        >不通过</el-button
      >
      <el-button
        type="primary"
        :disabled="false"
        @click.prevent="newAssets1(1)"
        :loading="loading"
        v-if="pageType === 'change' || (audit == 'true' && pageType == 'look')"
        >通过</el-button
      >
    </el-row>
    <el-row
      v-if="name === 'AssetRecord2'"
      style="display: flex; justify-content: center; padding-bottom: 50px"
    >
      <el-button type="danger" @click="sendBack" v-if="pageType === 'look'"
        >返回</el-button
      >
      <el-button
        type="danger"
        :disabled="false"
        @click="newAssets(1)"
        v-if="pageType === 'change' "
        >取消</el-button
      >
      <el-button
        type="primary"
        :disabled="false"
        @click.prevent="newAssets()"
        :loading="loading"
        v-if="pageType === 'change' "
        >提交</el-button
      >
    </el-row>
    <el-row
      style="display: flex; justify-content: center; padding-bottom: 50px"
      v-if="name === 'XcxZC'"
    >
      <el-button
        type="danger"
        @click="sendBack"
        v-if="pageType === 'look' && audit != true"
        >返回</el-button
      >
      <el-button type="danger" @click="sendBack" v-if="pageType === 'change'"
        >取消</el-button
      >
      <el-button
        type="primary"
        @click="newAssets()"
        :loading="loading"
        v-if="pageType === 'change'"
        >提交</el-button
      >
      <el-button type="danger" @click="newAssets(1)" v-if="pageType === 'add'"
        >取消</el-button
      >
      <el-button
        type="primary"
        @click="newAssets(null)"
        v-if="pageType === 'add'"
        >保存</el-button
      >
    </el-row>
    <el-row
      style="display: flex; justify-content: center; padding-bottom: 50px"
      v-if="name === 'Mytask-info'"
    >
      <el-button type="danger" @click="newAssets(0)" v-if="pageType !== 'look'"
        >关停</el-button
      >
      <el-button @click="newAssets(1)" v-if="pageType !== 'look'"
        >取消</el-button
      >
      <el-button type="warning" @click="newAssets(2)" v-if="pageType !== 'look'"
        >暂存</el-button
      >
      <el-button
        type="success"
        @click="newAssets(3)"
        :loading="loading"
        v-if="pageType !== 'look'"
        >提交</el-button
      >
      <el-button type="danger" @click="sendBack" v-if="pageType == 'look'"
        >返回</el-button
      >
    </el-row>
    <el-row
      style="display: flex; justify-content: center; padding-bottom: 50px"
      v-if="name === 'UnitFillsec'"
    >
      <el-button
        type="danger"
        :disabled="false"
        @click="btnReject"
        v-if="pageType === 'change'"
        >不通过</el-button
      >
      <el-button
        type="primary"
        :disabled="false"
        @click.prevent="btnPass"
        :loading="loading"
        v-if="pageType === 'change'"
        >通过</el-button
      >
      <el-button
        type="danger"
        @click="sendBack"
        v-if="pageType === 'look' && audit != true"
        >返回</el-button
      >
    </el-row>
  </xcxZCinfo>
</template>

<script setup>
import xcxZCinfo from "./xcxZCinfo.vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import {
  assetTaskxcxzc,
  assetTaskxcxjyTj,
  assetTaskminitaskSh,
  assetTasksh,
  assetTaskhistory,
} from "@/api/renwuApi/index.js";
import {
  assetMiniPrograms,
  miniProgramsInfo,
  unitAllList,
  unitEdit,
  assetAudit,
  lookInfo,
} from "@/api/auditPagesApi/index";
import { ref } from "vue";

const route = useRoute();
const router = useRouter();

let pageType = ref(route.query.pageType);
console.log(pageType.value, "pageType");
const id = ref(route.query.id);
const audit = ref(route.query.audit);
console.log(audit.value, "audit");
const isModdle = ref(route.query.isModdle);
let name = ref(route.query.name);
console.log(name.value, "name");
const queryData = ref(route.query.queryData);
let taskId = ref(route.query.taskId || "");
let assetId = ref(route.query.assetId);
const type = ref(route.query.type);
let loading = ref(false);

const xcxZCinfoRef = ref();
const liebiaoDialogRef = ref();
const ruleFormRef = ref();

const sendBack = () => {
  router.push({ name: route.query.name, params: queryData.value });
};

const newAssets = async (typeButtom1) => {
  const typeButtom = typeButtom1 !== undefined ? typeButtom1 : "";
  let ruleForm = xcxZCinfoRef.value?.ruleForm;

  if (!ruleForm) {
    ElMessage.error("获取表单数据失败");
    return;
  }
  
  console.log(typeButtom, "typeButtom");
  if (!Object.is(typeButtom, undefined)) {
    console.log(typeButtom, "typeButtom");
    if (typeButtom === 1) {
      loading.value = true;
      router.go(-1);
      loading.value = false;
      return;
    }

    // 设置任务和资产ID
    ruleForm.taskId = taskId.value;
    ruleForm.assetId = assetId.value;

    // 关停
    if (typeButtom === 0) {
      console.log(typeButtom, "typeButtom");
      baofeiRef.value?.open();
    }

    // 暂存
    if (typeButtom === 2) {
      loading.value = true;
      try {
        const res = await assetTaskxcxzc(ruleForm);
        console.log(res, "res");
        ElMessage.success("修改成功");
        router.go(-1);
      } catch (error) {
        console.error(error);
        ElMessage.error("修改失败");
      } finally {
        loading.value = false;
      }
    }

    // 提交
    if (typeButtom === 3) {
      loading.value = true;
      try {
        // 表单验证
        await xcxZCinfoRef.value.ruleFormRef.validate();
        const res = await assetTaskxcxjyTj(ruleForm);
        console.log(res, "res");
        ElMessage.success("提交成功");
        router.go(-1);
      } catch (error) {
        console.error(error);
        ElMessage.info("请填写完整");
      } finally {
        loading.value = false;
      }
    }
  } else {
    try {
      await xcxZCinfoRef.value.ruleFormRef.validate();
      
      loading.value = true;
      if (pageType.value === "change") {
        await unitEdit(ruleForm);
        ElMessage.success("提交成功，待管理员审核通过后完成修改");
      } else {
        ruleForm.xzType = 1;
        await assetMiniPrograms("post", ruleForm);
        if (pageType.value === "add" && name.value == "XcxZC") {
          ElMessage.success("新增成功");
        } else if (pageType.value === "add") {
          ElMessage.success("提交成功，待管理员审核通过后完成新增");
        }
      }
      router.push({ name: name.value, params: queryData.value });
    } catch (err) {
      console.error(err);
    } finally {
      loading.value = false;
    }
  }
};

const newAssets1 = async (type) => {
  //不通过
  if (type == 0) {
    // 打开子组件的弹框让用户填写不通过意见
    butongguoRef.value?.open();
  }
  
  if (type == 1) {
    try {
      await xcxZCinfoRef.value.ruleFormRef.validate();
      let tijioaobj = {
        taskId: route.query.taskId,
        assetId: route.query.assetId,
        xtmc: xcxZCinfoRef.value.ruleForm.xcxmc,
        dwmc: route.query.zcdwmc,
        status: 2,
        type: route.query.type,
        assetMiniProgramsCpPo: xcxZCinfoRef.value.ruleForm,
      };
      
      console.log(xcxZCinfoRef.value.ruleForm, "tijioaobj");
      loading.value = true;
      const res = await assetTasksh(tijioaobj);
      
      if (res.code == 200) {
        ElMessage.success("审核成功");
        router.go(-1);
      }
    } catch (error) {
      ElMessage.info("请填写完整");
      console.error(error);
    } finally {
      loading.value = false;
    }
  }
};

const btnReject = () => {
  // 打开子组件的弹框
  xcxZCinfoRef.value.butongguoRef.open();
};

const submitReject = () => {
  // 校验弹框表单
  xcxZCinfoRef.value.dialogruleFormRef?.validate(async (valid) => {
    if (valid) {
      // 根据不同的name使用不同的接口
      if (name.value === "TaskManagement-info") {
        // 使用assetTasksh接口
        let tijioaobj = {
          taskId: route.query.taskId,
          assetId: route.query.assetId,
          xtmc: xcxZCinfoRef.value.ruleForm.xcxmc,
          btgyy: xcxZCinfoRef.value.dialogruleForm.auditYy,
          status: 4,
          type: route.query.type,
          assetMiniProgramsCpPo: xcxZCinfoRef.value.ruleForm,
        };

        try {
          loading.value = true;
          const res = await assetTasksh(tijioaobj);
          if (res.code == 200) {
            ElMessage.success("审核成功");
            xcxZCinfoRef.value.butongguoRef.close();
            router.go(-1);
          }
        } catch (e) {
          ElMessage.error("操作失败");
        } finally {
          loading.value = false;
        }
      } else if (name.value === "Mytask-info") {
        xcxZCinfoRef.value.ruleForm.bfyy = xcxZCinfoRef.value.dialogruleForm?.auditYy;
        loading.value = true;
        
        try {
          const res = await assetTaskxcxzc(xcxZCinfoRef.value.ruleForm);
          console.log(res, "res");
          ElMessage({
            type: "info",
            message: "已关停",
          });
          router.go(-1);
          xcxZCinfoRef.value.ruleForm.bfyy = "";
        } catch (error) {
          console.error(error);
          ElMessage.error("操作失败");
        } finally {
          loading.value = false;
        }
      } else {
        // 其他页面使用原来的assetAudit接口
        let tijioaobj = {
          auditState: "3",
          auditYy: xcxZCinfoRef.value.dialogruleForm.auditYy,
          currentId: Number(id.value),
          xzType: 1,
        };

        try {
          const res = await assetAudit(tijioaobj);
          if (res.code == 200) {
            ElMessage.success("审核驳回");
            xcxZCinfoRef.value.butongguoRef.close();
            router.push({ name: name.value, params: queryData.value });
          }
        } catch (e) {
          ElMessage.error("操作失败");
        }
      }
    }
  });
};

const btnPass = async () => {
  loading.value = true;
  try {
    // 获取 ruleForm 数据
    const ruleForm = xcxZCinfoRef.value?.ruleForm;
    const res = await assetAudit({
      currentId: Number(id.value),
      auditState: "2",
      xzType: 1,
      dwmc: ruleForm?.ssdw,
      xtmc: ruleForm?.xcxmc,
    });
    ElMessage.success("审核通过");
    router.push({ name: name.value, params: queryData.value });
  } catch (err) {
    console.error(err);
    ElMessage.error("操作失败");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped></style>
