<template>
  <div class="goodsCostDetail">
    <!-- {{ status }}---{{ powers }} -->
    <div
      class="goodsCostDetail-main"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '007')"
    >
      <el-tabs v-model="activeTabs">
        <el-tab-pane label="新建商品成本" name="0" v-if="queryType === 'add'">
          <base-form
            v-if="newTime !== ''"
            :type="queryType"
            :id="queryId"
            :newTime="newTime"
            :sitem="sitem"
            @refresh="refresh"
          />
        </el-tab-pane>
        <el-tab-pane label="商品成本详情" name="1" v-if="queryType !== 'add'">
          <el-collapse v-model="activeNames" style="margin: -18px 0 0 0">
            <el-collapse-item title="商品成本详情" name="0">
              <base-form
                v-if="newTime !== ''"
                :type="queryType"
                :id="queryId"
                :newTime="newTime"
                :sitem="sitem"
                @refresh="refresh"
              />
            </el-collapse-item>
            <el-collapse-item
              title="采购部门审批"
              name="1"
              v-if="
                (status === '0' || status === '2' || status === '3') &&
                queryType === 'view' &&
                powers.some((item) => item == '036')
              "
            >
              <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" v-if="queryType !== 'add'">
            <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'SPCB'"
            :orderCode="queryId"
          />
        </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
   <script>
import asyncRequest from "@/apis/service/goodStore/goodsCost";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
import baseForm from "./components/baseForm";
export default {
  name: "goodsCostDetail",
  mixins: [resToken],
  components: {
    baseForm,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "goodsCostDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },

  data() {
    return {
      statusList: [],
      size: "small",
      activeTabs: "1",
      activeNames: ["0", "1"],
      newTime: "",
      loading: false,
      queryType: "",
      queryId: "",
      status: "",
      sitem: null,
    };
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      const { id, type } = this.$route.query;
      this.queryId = id;
      this.queryType = type;
      this.activeTabs = type === "add" ? "0" : "1";
      this.loading = true;
      if (this.queryType === "add") {
        this.sitem = {};
        this.getNewTime();
      } else {
        await this.initData();
      }

      this.loading = false;
    },
    // 点击业务审核的保存按钮

    async examForm(e) {
      console.log(e);
      if (!this.loading) {
        let type = "";
        if (e.state === "1") {
          type = "1";
        } else {
          type =
            this.status === "0"
              ? "6"
              : this.status === "2"
              ? "4"
              : this.status === "3"
              ? "5"
              : "";
        }
        await this.setstatus(type, "提交采购部门审核", e.remark);
      }
    },
    async setstatus(type, detail, remark) {
      await this.$confirm(`确定要${detail}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let _model = {
            spuCode: this.queryId,
            status: type,
            remark: remark,
          };
          let res = await asyncRequest.status(_model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: "提交成功！",
              message: "",
            });
            await this.initForm();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    handleClick(row) {
      console.log(row);
    },
    async refresh(e) {
      await this.routeReGoto("goodsCost", {});
    },
    async initData() {
      this.loading = true;
      const { code, message, data } = await asyncRequest.detail({
        spuCode: this.queryId,
      });
      this.loading = false;
      if (code === 0) {
        this.sitem = JSON.parse(JSON.stringify(data));
        const { status } = this.sitem;
        this.status = status;
        this.getNewTime();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
  },
};
</script>
   <style lang="scss" scoped>
.goodsCostDetail {
  width: 100%;
  box-sizing: border-box;
  .goodsCostDetail-main {
    box-sizing: border-box;
    padding: 10px;
    height: 100%;
    width: 100%;
  }
  .goodsCostDetail-title {
    border-top: 1px solid #ebeef5;
    span {
      height: 50px;
      line-height: 50px;
      font-family: "微软雅黑", sans-serif;
      font-weight: 400;
      font-style: normal;
      font-size: 16fpx;
      text-align: left;
    }
  }
  /deep/ .ddiv {
    border-top: 1px solid #dcdfe6;
  }
  /deep/ .dtitle {
    width: 40px;
    text-align: center;
    height: 100%;
    min-height: 100%;
    ul {
      padding: 12px 0 0 0;
    }
  }
  /deep/ .dmain {
    padding: 20px 0 0 0;
    width: calc(100% - 40px);
    border-left: 1px solid #dcdfe6;
  }
}
</style>
   