<template>
  <div class="purchaseOrderDetail pagePadding">
    <div
      style="width: 100%"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <p>{{ status }}---{{ powers }}</p>
     <div class="detail-page-title">
        <span>采购单编号：</span><span>{{ queryId }}</span>
        <el-button
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          plain
          style="margin: 0 0 0 12px"
          class="fr"
          :size="'mini'"
          v-if="
            status !== '' &&
            status !== '5' &&
            powers.some((item) => item == '015')
          "
          >作废该条信息</el-button
        >
        <el-button
          @click="statusConfirm('0', '取消发货')"
          class="fr"
          :size="'mini'"
          style="margin: 0 0 0 12px"
          v-if="status === '1' && powers.some((item) => item == '027')"
          >取消入库</el-button
        >
        <el-button
          type="primary"
          class="fr"
          style="margin: 0 0 0 12px"
          :size="'mini'"
          @click="statusConfirm('1', '开始入库')"
          v-if="status === '0' && powers.some((item) => item == '026')"
          >开始入库
        </el-button>
        <el-button
          type="primary"
          :size="'mini'"
          class="fr"
          style="margin: 0 0 0 12px"
          @click="addForm"
          v-if="status === '0' && powers.some((item) => item == '007')"
          >修改采购反馈
        </el-button>

        <el-button
          type="primary"
          :size="'mini'"
          class="fr"
          v-if="
            (status === '1' || status === '2') &&
            powers.some((item) => item == '007')
          "
          style="margin: 0 0 0 12px"
          @click="diffAdd = true"
          >填写总工差</el-button
        >
        <el-button
          type="primary"
          :size="'mini'"
          class="fr"
          style="margin: 0 0 0 12px"
          @click="ReturnAddModel = true"
          >新建采购单退货</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">
              <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>

              <!-- <work-diff-table></work-diff-table> -->
            </el-collapse-item>
            <el-collapse-item title="备货入库单" name="2">
              <order-in-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'CGD'"
            :orderCode="orderCode"
          />
        </el-tab-pane>
      </el-tabs>
      <!-- <diff-order-add
        :sitem="sitem"
        :show-model="diffAdd"
        @refresh="initData"
        @cancel="diffAdd = false"
      /> -->
      <!-- 采购单退货弹窗 -->
      <purchase-return-add
        :sitem="sitem"
        :show-model="ReturnAddModel"
        @refresh="initData()"
        @cancel="ReturnAddModel = false"
      />
      <order-addedit-model
        :id="modelId"
        :show-model="showModel"
        :sitem="detailSitem"
        @refresh="initData()"
        @cancel="showModel = false"
      />
    </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/purchaseOrder";
import orderInTable from "./components/order-in-table";
import ShowDataTableColumns from "./components/ShowDataTableColumns";
// import diffOrderAdd from "@/views/purchaseIn/purchaseDiffOrder/addEditModel";
// import WorkDiffTable from "./components/workDiff-table.vue";
import orderAddeditModel from "./components/order-addedit-model";
import PurchaseReturnAdd from "@/views/purchaseIn/purchaseReturn/addEdit"; //采购单退货弹窗
export default {
  name: "purchaseOrderDetail",
  mixins: [mixinPage, resToken],
  components: {
    orderInTable,
    orderAddeditModel,
    PurchaseReturnAdd, //采购单退货弹窗
  },

  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "purchaseOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      activeTabs: "1",
      detailSitem: null,
      modelId: "",
      showModel: false,
      ShowDataTableColumns: ShowDataTableColumns,
      isMetal: false,
      finishStatus: "finish",
      activeNames: ["0", "1", "2", "3", "4", "5", "10"],
      sitem: null,
      statusOptions: [
        { value: "0", label: "待同意开始入库" },
        { value: "1", label: "待入库" },
        { value: "2", label: "部分入库" },
        { value: "3", label: "入库完成" },
      ],
      statusList: [
        {
          code: "0",
          label: "待同意发货",
        },
        {
          code: "1",
          label: "待发货",
        },
        {
          code: "2",
          label: "部分发货",
        },
        {
          code: "3",
          label: "发货完成",
        },
      ],
      status: "",
      newTime: "",
      loading: false,
      queryId: "",
      diffAdd: false,
      orderCode: "",
      ReturnAddModel: false, //采购单退货弹窗
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    addForm() {
      this.detailSitem = this.sitem;
      this.modelId = this.id;
      this.showModel = true;
    },
    openModal() {
      this.diffAdd = true;
      this.ReturnAddModel = true;
    },
    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({ cgdNo: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, can, cgdNo } = this.sitem;
        this.status = status;
        this.orderCode = cgdNo;
        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.$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 {
            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("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 = {
        cgdNo: this.sitem.cgdNo,
        remark: remark || "",
        status: status,
      };
      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);
      }
    },
  },
};
</script>
   <style lang="scss" scoped>
.purchaseOrderDetail {
}
</style>
   
   