<template>
  <div class="goodsOnlineDetail">
    <!-- {{ status }}-{{ powers }} -->
    <div
      class="goodsOnlineDetail-main"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '007')"
    >
      <el-tabs v-model="activeTabs">
        <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">
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="ShowDataTableColumns"
              >
                <template slot="good_name">
                  <img
                    v-viewer
                    style="width: 26px; height: 26px; margin: 5px 5px 0 0"
                    class="fl"
                    v-if="sitem.good_thumb_img"
                    :src="sitem.good_thumb_img"
                    alt=""
                  />
                  <div>{{ sitem.good_name }}</div>
                </template>
                <template slot="speclist">
                  <span v-for="(si, i) in sitem.speclist" :key="si.spec_id + i">
                    <span v-if="i !== 0">--</span
                    ><span>{{ si.spec_name }}[{{ si.spec_value }}]</span>
                  </span>
                </template>
                <template slot="good_info_img">
                  <img
                    class="fl"
                    style="width: 26px; height: 26px; margin: 5px 5px 2px 0"
                    v-viewer
                    v-for="(si, i) in sitem.good_info_img"
                    :src="si"
                    :key="si + i"
                  />
                </template>
                <template slot="is_auth">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (options3.find((item) => item.id == sitem.is_auth) || {})
                        .name || '--'
                    "
                  />
                </template>
                <!-- <template slot="exclusive">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (options2.find((item) => item.id == sitem.exclusive) || {})
                        .name || '--'
                    "
                  />
                </template> -->
                <template slot="is_stock">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (options4.find((item) => item.id == sitem.is_stock) || {})
                        .name || '--'
                    "
                  />
                </template>
                <template slot="good_type">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        options1.find((item) => item.id == sitem.good_type) ||
                        {}
                      ).name || '--'
                    "
                  />
                  <span
                    v-if="sitem.good_type === '1'"
                    style="padding: 0 0 0 5px"
                    >{{ sitem.moq }}起订/工期{{ sitem.customized }}天</span
                  >
                </template>
                <template slot="exclusive">
                  <span v-for="(si, sii) in sitem.exclusive" :key="si.id">
                    <span v-if="sii !== 0">/</span> <span>{{ si.name }}</span>
                  </span>
                </template>

                <template slot="supply_area">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        options6.find((item) => item.id == sitem.supply_area) ||
                        {}
                      ).name || '--'
                    "
                  />
                </template>
                <template slot="is_step">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (options7.find((item) => item.id == sitem.is_step) || {})
                        .name || '--'
                    "
                  />
                </template>

                <template slot="exam_status">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        options8.find((item) => item.id == sitem.exam_status) ||
                        {}
                      ).name || '--'
                    "
                  />
                </template>
                <template slot="nakelist">
                  <el-table
                    :data="nakelist"
                    :size="'mini'"
                    border
                    stripe
                    style="width: 99%; margin: 10px 10px 10px 0"
                  >
                    <el-table-column prop="min_num" label="起订量(>=)" />
                    <el-table-column prop="nake_fee" label="成本合计" />
                    <el-table-column prop="cost_fee" label="工艺费" />
                    <el-table-column prop="delivery_fee" label="其中运费" />
                  </el-table>
                </template>
                <template slot="ladderlist">
                  <el-table
                    :data="ladderlist"
                    :size="'mini'"
                    border
                    stripe
                    style="width: 99%; margin: 10px 10px 10px 0"
                  >
                    <el-table-column prop="min_num" label="起订量(>=)" />
                    <el-table-column prop="sale_price" label="售价" />
                    <el-table-column prop="market_price" label="市场价" />
                    <el-table-column prop="market_platform" label="对比平台" />
                    <el-table-column prop="status" label="状态">
                      <template slot-scope="scope">{{
                        scope.row.status === "1" ? "启用" : "禁用"
                      }}</template>
                    </el-table-column>
                  </el-table>
                </template>
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item
              title="产品部门审批"
              name="1"
              v-if="
                status === '2' &&
                queryType == 'view' &&
                powers.some((item) => item == '056')
              "
            >
              <online-exam-form
                :newTime="newTime"
                v-if="newTime !== ''"
                :time="sitem.online_time"
                :disabled="false"
                :isMust="false"
                :statusList="[]"
                @searchChange="examForm"
              />
            </el-collapse-item>
            <el-collapse-item
              title="财务定价"
              name="2"
              v-if="
                status === '3' &&
                queryType == 'view' &&
                powers.some((item) => item == '058')
              "
            >
              <fixed-price-form
                :newTime="newTime"
                :id="queryId"
                :spuCode="spuCode"
                v-if="newTime !== ''"
                :sitem="sitem"
                :disabled="false"
                :isMust="false"
                @resSuccess="initForm"
              />
            </el-collapse-item>
            <el-collapse-item
              title="财务审核定价"
              name="3"
              v-if="
                status === '4' &&
                queryType == 'view' &&
                powers.some((item) => item == '059')
              "
            >
              <exam-form
                :newTime="newTime"
                v-if="newTime !== ''"
                :options8="options8"
                :disabled="false"
                :isMust="false"
                @searchChange="examForm2"
              />
            </el-collapse-item>
            <el-collapse-item
              title="操作商品上线"
              name="4"
              v-if="
                status === '5' &&
                queryType == 'view' &&
                powers.some((item) => item == '071')
              "
            >
              <handle-online-form
                :newTime="newTime"
                v-if="newTime !== ''"
                :options="[]"
                :code="sitem.plat_code"
                :disabled="false"
                :isMust="false"
                @resSuccess="examForm3"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2"> 111 </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
   <script>
import asyncRequest from "@/apis/service/goodStore/goodsOnline";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
import ShowDataTableColumns from "./ShowDataTableColumns";
import onlineExamForm from "./components/online-exam-form";
import fixedPriceForm from "./components/fixed-price-form";
import handleOnlineForm from "./components/handle-online-form";

import {
  options1,
  options2,
  options3,
  options4,
  options5,
  options6,
  options7,
  options8,
} from "./columns";
export default {
  name: "goodsOnlineDetail",
  mixins: [resToken],
  components: {
    onlineExamForm,
    fixedPriceForm,
    handleOnlineForm,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "goodsOnlineDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },

  data() {
    return {
      ShowDataTableColumns: ShowDataTableColumns,
      options1: options1,
      options2: options2,
      options3: options3,
      options4: options4,
      options5: options5,
      options6: options6,
      options7: options7,
      options8: options8,
      ladderlist: [],
      nakelist: [],
      spuCode: "",
      size: "small",
      activeTabs: "1",
      activeNames: ["0", "1", "2", "3", "4", "5", "6", "7"],
      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 model = {
          skuCode: this.queryId,
          exam_status: e.state === "1" ? "3" : "7",
          online_time: e.online_time,
          remark: e.remark,
        };
        await this.setstatus("提交产品部门审核结果", model);
      }
    },
    // 财务审核的保存按钮
    async examForm2(e) {
      console.log(e);
      if (!this.loading) {
        let model = {
          skuCode: this.queryId,
          exam_status: e.state === "1" ? "5" : "7",
          remark: e.remark,
        };
        await this.setstatus("提交财务审核结果", model);
      }
    },
    // 操作上线的保存按钮
    async examForm3(e) {
      console.log(e);
      if (!this.loading) {
        let model = {
          skuCode: this.queryId,
          exam_status: "6",
          plat_code: e.plat_code,
          remark: e.remark || "",
        };
        await this.setstatus("提交商品上线结果", model);
      }
    },

    async setstatus(detail, model) {
      await this.$confirm(`确定要${detail}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          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("取消");
        });
    },

    refresh(e) {
      const { spuCode } = e;

      let model = {
        id: this.queryType === "add" ? spuCode : this.queryId,
        type: "view",
      };
      this.routeReGoto("goodsOnlineDetail", model);
    },
    async initData() {
      this.loading = true;
      const { code, message, data } = await asyncRequest.detail({
        skuCode: this.queryId,
      });
      this.loading = false;
      if (code === 0) {
        this.sitem = JSON.parse(JSON.stringify(data));
        const { exam_status, cat_info, ladderlist, nakelist, spuCode } =
          this.sitem;
        this.spuCode = spuCode;
        this.ladderlist = JSON.parse(JSON.stringify(ladderlist));
        this.nakelist = JSON.parse(JSON.stringify(nakelist));
        console.log(this.ladderlist);
        let cat = "";
        if (cat_info && cat_info.length > 0) {
          cat_info.forEach((e, i) => {
            cat += i === 0 ? e.name : "/" + e.name;
          });
        }
        this.sitem.cat = cat;
        this.sitem.good_info_img = this.sitem.good_info_img.split(",");
        this.status = exam_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>
.goodsOnlineDetail {
  width: 100%;
  box-sizing: border-box;
  .goodsOnlineDetail-main {
    padding: 10px;
    width: 100%;
  }
  .goodsOnlineDetail-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>
   