<template>
  <div class="purchaseReturnDetail pagePadding">
    <div v-if="powers.some((i) => i == '007')">
      <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="0">
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="ShowDataTableColumns"
              >
                <template slot="status">
                  <el-tag
                    :size="'mini'"
                    :type="sitem.status == '0' ? 'warning' : ''"
                    v-text="
                      (statusOptions.find((item) => item.value == sitem.status) || {})
                        .label || '--'
                    "
                  ></el-tag>
                </template>
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item title="商品信息" name="1">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :type="'1'"
                :skucode="sitem.skuCode"
                :spucode="sitem.spuCode"
                :iscgd="true"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <!-- <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'CGTHD'"
            :orderCode="queryId"
          />
        </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/purchaseIn/purchaseReturn";
import ShowDataTableColumns from "./ShowDataTableColumns";
import { mapGetters } from "vuex";
// import PurchaseOrderAddEdit from "./components/purchase-order-addEdit.vue";

export default {
  name: "purchaseReturnDetail",
  mixins: [mixinPage, resToken],
  components: {
    // PurchaseOrderAddEdit,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "purchaseReturnDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      activeTabs: "1",
      orderCode: "",
      ShowDataTableColumns: ShowDataTableColumns,
      activeNames: ["0", "1", "2", "3", "4", "10"],
      newTime: "",
      queryId: "",
      loading: true,
      sitem: null,
      status: "",
      statusOptions: [
        // { value: "0", label: "取消" },
        // { value: "1", label: "待业务审核" },
        // { value: "2", label: "通知财务部门" },
        { value: "1", label: "采购退货成功" },
      ],
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    async examForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "2" : "0";
        await this.setStatus(type, "提交业务审核", e.remark);
      }
    },
    async initData() {
      this.loading = true;
      const res = await asyncRequest.detail({ returnCode: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { can, status, bk_code } = this.sitem;
        this.status = status;
        this.orderCode = bk_code;
        if (can && can.length > 0) {
          this.sitem.class_cat = "";
          can.forEach((x, i) => {
            this.sitem.class_cat += i === 0 ? x.name : "/" + x.name;
          });
        }
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.sitem = {};
        this.status = 0;
      }
      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 {
            await this.setStatus(status, message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    // 作废该条信息
    async deleteById(message) {
      const res = await asyncRequest.delete({ id: this.queryId });
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });
        this.routeGoto("purchaseReturn", {});
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    // 发起备库申请
    async setStatus(status, message) {
      let model = {
        id: this.sitem.id,
        status: status,
        returnCode: this.queryId,
      };
      const res = await asyncRequest.status(model);
      this.loading = false;
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });
        await this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
  },
};
</script>
