<template>
  <div class="checkDetail pagePadding" v-loading="loading">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <div class="detail-page-title">
        <span>盘点编号：</span><span v-if="sitem">{{ sitem.check_code }}</span>

        <el-button
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          plain
          :size="'mini'"
          class="fr"
          v-if="(status === '0' || status === '1') && powers.some((i) => i == '015')"
          >作废该条信息</el-button
        >
        <el-button
          type="primary"
          :size="'mini'"
          class="fr"
          @click="statusConfirm('-2', '导出盘点商品')"
          v-if="status === '1' && powers.some((i) => i == '019')"
          >导出盘点商品
        </el-button>
      </div>
      <!-- <status-bar
        v-if="newTime !== ''"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      /> -->
      <el-tabs v-model="activeTabs">
        <el-tab-pane label="业务详情" name="1">
          <el-collapse v-model="activeNames" style="margin: -18px 0 0 0">
            <el-collapse-item title="盘点详情" name="-1">
              <add-edit
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item
              v-if="
                (status === '0' && ppowers.some((i) => i == '0')) ||
                (status === '1' && powers.some((i) => i == '014'))
              "
              title="发起审核流程"
              name="1"
            >
              <el-button
                type="primary"
                plain
                :size="'mini'"
                @click="statusConfirm('1', '发起审核流程')"
                v-if="status === '0' && ppowers.some((i) => i == '0')"
                >发起审核流程
              </el-button>
              <el-button
                @click="statusConfirm('0', '取消审核流程')"
                plain
                :size="'mini'"
                v-if="status === '1' && powers.some((i) => i == '014')"
                >取消审核流程</el-button
              >
            </el-collapse-item>
            <el-collapse-item
              v-if="
                status == '3' ||
                status == '4' ||
                (status == '1' && ppowers.some((i) => i == '1')) ||
                (status == '2' && ppowers.some((i) => i == '2'))
              "
              title="盘点结果记录"
              name="1"
            >
              <result-uplod
                :newTime="newTime"
                :sitem="sitem"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item
              v-if="status == '2' && ppowers.some((i) => i == '2')"
              title="盘点审核"
              name="2"
            >
              <exam-form
                :statusList="statusList"
                :newTime="newTime"
                :disabled="false"
                :isMust="false"
                @searchChange="examForm"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'PDD'"
            :orderCode="code"
          />
        </el-tab-pane>


        <el-tab-pane label="流程图" name="3">
          <flow-chart process_id="12" type="PDD" :orderCode="code" />
       </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/stock/check/detail";
import addEdit from "./components/addEdit";
import resultUplod from "./components/resultUplod";
import urlConfig from "@/apis/url-config";
import { mapGetters } from "vuex";

export default {
  name: "checkDetail",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
    resultUplod,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find((item) => item.menu_route == "checkDetail") ||
        {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "PDD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      code: "",
      activeTabs: "1",
      statusOptions: [
        { value: "0", label: "发起审核流程" },
        { value: "1", label: "上传盘点结果" },
        { value: "2", label: "盘点审核" },
        { value: "3", label: "系统更新" },
        { value: "4", label: "盘点完成 " },
      ],

      activeNames: ["-1", "0", "1", "2", "3", "4"],
      sitem: null,
      status: "",
      newTime: "",
      fileUrl: urlConfig.baseURL,
      loading: true,
      queryId: "",
    };
  },
  mounted() {
    this.status = "";
    this.queryId = this.$route.query.id;
    this.initData();
  },

  methods: {
    async initData() {
      this.loading = true;
      const res = await asyncRequest.detail({ id: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        this.status = this.sitem.status;
        this.code = res.data.check_code;
        // this.activeNames = [this.status];
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
      this.loading = false;
    },
    async statusConfirm(status, message) {
      await this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (status === "-1") {
            await this.deleteById(message);
          } else if (status === "-2") {
            await this.exportGood(message);
          } else {
            await this.setStatus(status, message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async exportGood(message) {
      if (!this.loading) {
        this.loading = true;
        let httpType = `aplication/zip`;
        let model = {
          id: this.queryId,
        };
        axios({
          method: "post",
          url: this.fileUrl + "admin/checkexport",
          responseType: "blob",
          data: model,
          headers: {
            Accept: httpType,
          },
        })
          .then((res) => {
            if (res && res.status == 200 && res.data) {
              let blob = new Blob([res.data], {
                type: httpType,
              });
              let url = window.URL.createObjectURL(blob);
              let aLink = document.createElement("a");
              aLink.style.display = "none";
              aLink.href = url;
              aLink.setAttribute("download", `${message}.zip`);
              document.body.appendChild(aLink);
              aLink.click();
              document.body.removeChild(aLink); //下载完成移除元素
              window.URL.revokeObjectURL(url); //释放掉blob对象

              this.$message.success(`${message}导出成功！`);
              setTimeout(() => {
                this.loading = false;
              }, 500);
            } else {
              this.$message.error(res.data.message);
              setTimeout(() => {
                this.loading = false;
              }, 500);
            }
          })
          .catch((error) => {
            console.log(error);
            this.loading = false;
          });
      }
    },
    async deleteById(message) {
      const res = await asyncRequest.delete({ id: this.queryId });
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });

        this.routeGoto("check", {});
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async setStatus(status, message, remark) {
      let model = {
        id: this.queryId,
        remark: remark || "",
        status: status,
      };
      const res = await asyncRequest.status(model);
      this.loading = false;
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });
        this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async examForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "3" : "1";
        await this.setStatus(type, "盘点审核", e.remark);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.checkDetail {
  width: 100%;
}
</style>
