<template>
  <div class="stockApplyDetail pagePadding">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '007')"
    >
      <p>{{ status }}---{{ powers }}</p>
      <div class="detail-page-title">
        <span>备货申请编号：</span><span v-if="sitem">{{ sitem.bk_code }}</span>

        <el-button
          v-if="
            powers.some((item) => item === '015') &&
            (status === '0' || status === '1' || status === '2')
          "
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          plain
          class="fr"
          :size="'mini'"
          style="margin: 0 0 0 12px"
          >作废该条信息</el-button
        >
        <el-button
          @click="statusConfirm('0', '取消审核流程')"
          plain
          class="fr"
          type="primary"
          :size="'mini'"
          v-if="status === '1' && powers.some((item) => item == '014')"
          >取消审核流程</el-button
        >
      </div>
      <status-bar
        v-if="newTime !== ''"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      />
      <el-tabs v-model="activeTabs">
        <el-tab-pane label="新建备库申请单" name="0">
          <add-edit
            :sitem="sitem"
            :id="queryId"
            :newTime="newTime"
            :type="queryId"
            @refresh="initData"
            v-if="newTime != ''"
          />
        </el-tab-pane>
        <el-tab-pane label="业务详情" name="1">
          <el-collapse v-model="activeNames" style="margin: -18px 0 0 0">
            <el-collapse-item name="1" title="备货详情">
              <add-edit
                :sitem="sitem"
                :id="queryId"
                :newTime="newTime"
                :type="'1'"
                @refresh="initData"
                v-if="newTime != ''"
              />
            </el-collapse-item>
            <el-collapse-item
              name="2"
              title="发起审核流程"
              v-if="status === '0' && powers.some((item) => item == '012')"
            >
              <div class="exam-content">
                <el-button
                  type="primary"
                  class="fr"
                  :size="'mini'"
                  @click="statusConfirm('1', '发起审核流程')"
                  >发起审核流程
                </el-button>
              </div>
            </el-collapse-item>
            <el-collapse-item name="3" title="采购反馈" v-if="status !== '0'">
              <purchase-back
                :sitem="sitem"
                :id="queryId"
                :newTime="newTime"
                v-if="newTime != ''"
                @refresh="initData"
              />
            </el-collapse-item>

            <el-collapse-item
              name="4"
              title="同意采购"
              v-if="status === '2' && powers.some((item) => item === '025')"
            >
              <el-row style="padding: 0 0 20px 0">
                <el-col :span="2" offset="20">
                  <el-button
                    :disabled="
                      !(status === '2' && powers.some((item) => item === '025'))
                    "
                    type="primary"
                    size="small"
                    @click="feeChange('同意反馈', '3')"
                  >
                    同 意
                  </el-button>
                </el-col>
                <el-col :span="2">
                  <el-button
                    :disabled="
                      !(status === '2' && powers.some((item) => item === '025'))
                    "
                    type="warning"
                    size="small"
                    @click="feeChange('拒绝反馈', '4')"
                  >
                    拒 绝
                  </el-button>
                </el-col>
              </el-row>
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'BHD'"
            :orderCode="orderCode"
          />
        </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 PurchaseBack from "./components/purchaseBack.vue";
export default {
  name: "stockApplyDetail",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
    PurchaseBack,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockApplyDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      orderCode: "",
      activeTabs: "1",
      activeNames: ["0", "1", "2", "3", "4"],
      newTime: "",
      isDisabled: false,
      queryId: "",
      queryType: "",
      loading: true,
      sitem: null,
      status: "",
      statusOptions: [
        { value: "0", label: "待发起审核流程" },
        { value: "1", label: "待采购反馈" },
        { value: "2", label: "待同意采购" },
        { value: "3", label: "已同意采购" },
      ],
    };
  },
  mounted() {
    const { id, type } = this.$route.query;
    this.queryId = id;
    this.queryType = type;
    if (this.queryType === "add") {
      this.sitem = {};
      this.getNewTime();
    } else {
      this.initData();
    }
  },
  methods: {
    async feeChange(val, status) {
      this.loading = true;
      await this.$confirm(`确定要${val}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = {};
          let model = {
            id: this.sitem.id,
            status,
          };
          if (status === "3") {
            res = await asyncRequest.feeChange(model);
            if (res && res.code === 0 && res.data) {
              this.getNewTime();
              this.$notify({
                title: val + "成功",
                message: "",
                type: "success",
              });
              this.initData();
            } else if (res && res.code >= 100 && res.code <= 104) {
              await this.logout();
            } else {
            }
          } else if (status === "4") {
            res = await asyncRequest.feeChange(model);
            if (res && res.code === 0 && res.data) {
              this.getNewTime();
              this.$notify({
                title: val + "成功",
                message: "",
                type: "warning",
              });
              this.initData();
            } else if (res && res.code >= 100 && res.code <= 104) {
              await this.logout();
            } else {
              console.log("error");
            }
          } else {
            await this.setStatus(status, message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
      this.loading = false;
    },
    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 = res.data.status;
        this.orderCode = res.data.bk_code;
        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 {
            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>
   