<template>
  <div class="stockApplyDetail pagePadding">
    <div v-if="powers.some((i) => i == '007')">
      <!-- <status-bar
        v-if="newTime !== '' && queryType === 'view'"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      /> -->
      <el-tabs v-model="activeTabs">
        <el-tab-pane label="新建备库申请单" name="0" v-if="queryType === 'add'">
          <add-edit
            :sitem="sitem"
            :id="queryId"
            :newTime="newTime"
            :type="queryId"
            @refresh="routeReGoto('stockApply', {})"
            v-if="newTime != ''"
          />
        </el-tab-pane>
        <el-tab-pane label="业务详情" name="1" v-if="queryType === 'view'">
          <el-collapse v-model="activeNames" style="margin: -18px 0 0 0">
            <el-collapse-item name="1" title="备库详情">
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="showColumns"
              >
                <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>
                <template slot="file_url">
                  <el-link :underline="false" :href="sitem.file_url" target="_blank">{{
                    sitem.file_name
                  }}</el-link>
                </template>
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item name="2" title="商品详情">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :type="'1'"
                :iscgd="true"
                :skucode="sitem.skuCode"
                :spucode="sitem.spuCode"
              />
            </el-collapse-item>
            <el-collapse-item
              name="3"
              title="采购反馈-设置入库的仓库"
              v-if="status === '1' && ppowers.some((i) => i == '1')"
            >
              <purchase-back
                :sitem="sitem"
                :id="queryId"
                :newTime="newTime"
                v-if="newTime != ''"
                @refresh="initData"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2" v-if="queryType === 'view'">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'BHD'"
            :orderCode="queryId"
          />
        </el-tab-pane>
        
        <el-tab-pane label="流程图" name="5" v-if="queryType === 'view'">
          <flow-chart process_id="3" type="BHD" :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/sellOut/stockApply";
import addEdit from "./components/addEdit";
import { mapGetters } from "vuex";
import { showColumns } from "./columns";
import PurchaseBack from "./components/purchaseBack.vue";
export default {
  name: "stockApplyDetail",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
    PurchaseBack,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockApplyDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "BHD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      activeTabs: "0",
      activeNames: ["0", "1", "2", "3", "4"],
      showColumns,
      newTime: "",
      isDisabled: false,
      queryId: "",
      queryType: "",
      loading: true,
      sitem: null,
      status: "",
      statusOptions: [
        { value: "1", label: "待采购反馈" },
        { value: "2", label: "已生成采购单" },
        {
          value: "4",
          label: "已取消采购",
        },
      ],
    };
  },
  mounted() {
    const { id, type } = this.$route.query;
    this.queryId = id;
    this.queryType = type;
    this.activeTabs = type === "add" ? "0" : "1";
    if (this.queryType === "add") {
      this.sitem = {};
      this.getNewTime();
    } else {
      this.initData();
    }
  },
  methods: {
    async initData() {
      this.loading = true;
      const { code, data, message } = await asyncRequest.detail({
        id: this.queryId,
      });
      if (code === 0) {
        const { status } = data;
        this.sitem = data;
        this.status = status;
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(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 {
            this.isDisabled = true;
            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("stockApply", {});
      } 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.queryId,
        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);
      }
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
  },
};
</script>
