<template>
  <div class="bargainListDetail">
    <div class="bargainListDetail-main" v-if="powers.some((i) => i == '007')">
      <el-tabs v-model="projectTabs" v-loading="loading">
        <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
                style="padding: -5px 0 10px 0"
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="editColumns"
              >
                <template slot="status">
                  <el-tag
                    :size="tablebtnSize"
                    :type="
                      (statusList.find((item) => item.id == sitem.status) || {}).type ||
                      ''
                    "
                    v-text="
                      (statusList.find((item) => item.id == sitem.status) || {}).label ||
                      '--'
                    "
                  ></el-tag>
                </template>

                <template slot="is_addrs">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (options12.find((item) => item.id == sitem.is_addrs) || {}).name ||
                      '--'
                    "
                  ></el-tag>
                </template>
                <template slot="is_custom">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (options1.find((item) => item.id == sitem.is_custom) || {}).name ||
                      '--'
                    "
                  ></el-tag>
                </template>

                <template slot="good_num">
                  <span>{{ sitem.good_num ? sitem.good_num : "0" }}{{ sitem.unit }}</span>
                </template>
                <template slot="rate_status">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        rate_status_options.find(
                          (item) => item.id == sitem.rate_status + ''
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item title="商品信息" name="20">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :type="'3'"
                :skucode="''"
                :spucode="sitem.spuCode"
                :iscgd="true"
              />
            </el-collapse-item>
            <el-collapse-item
              title="商品成本信息"
              name="6"
              v-if="
                newTime !== '' &&
                private_field &&
                private_field.length > 0 &&
                private_field.some((item) => item == '1')
              "
            >
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="costArr"
              >
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item
              title="商品毛利信息"
              name="7"
              v-if="
                newTime !== '' &&
                private_field &&
                private_field.length > 0 &&
                private_field.some((item) => item == '3')
              "
            >
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="bargainingArr"
              >
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item
              title="业务部门审批"
              name="2"
              v-if="
                eaxmList.some((item) => item.info_status + '' == '0') ||
                (status == '0' && ppowers.some((i) => i == '0'))
              "
            >
              <exam-formss
                :newTime="newTime"
                v-if="newTime !== ''"
                :disabled="!(status == '0' && ppowers.some((i) => i == '0'))"
                :sitem="orderItem"
                @searchChange="examFormSubmit($event, '业务部门')"
              />
            </el-collapse-item>
            <el-collapse-item
              title="财务专员审批"
              name="3"
              v-if="
                eaxmList.some((item) => item.info_status + '' == '2') ||
                (status == '2' && ppowers.some((i) => i == '2'))
              "
            >
              <exam-formss
                :newTime="newTime"
                v-if="newTime !== ''"
                :disabled="!(status == '2' && ppowers.some((i) => i == '2'))"
                :sitem="moneyDirItem"
                @searchChange="examFormSubmit($event, '财务专员')"
              />
            </el-collapse-item>
            <el-collapse-item
              title="财务主管审批"
              name="4"
              v-if="
                eaxmList.some((item) => item.info_status + '' == '4') ||
                (status == '4' && ppowers.some((i) => i == '4'))
              "
            >
              <exam-formss
                :newTime="newTime"
                v-if="newTime !== ''"
                :disabled="!(status == '4' && ppowers.some((i) => i == '4'))"
                :sitem="moneyItem"
                @searchChange="examFormSubmit($event, '财务主管')"
              />
            </el-collapse-item>
            <el-collapse-item
              title="BOSS审批"
              name="5"
              v-if="
                eaxmList.some((item) => item.info_status + '' == '7') ||
                (status == '7' && ppowers.some((i) => i == '7'))
              "
            >
              <exam-formss
                :newTime="newTime"
                v-if="newTime !== ''"
                :disabled="!(status == '7' && ppowers.some((i) => i == '7'))"
                :sitem="BossItem"
                @searchChange="examFormSubmit($event, 'BOSS')"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="3">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'YJD'"
            :orderCode="queryId"
          />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="5">
          <flow-chart process_id="13" :orderCode="queryId" />
        </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import examFormss from "./exam-form.vue";
import asyncRequest from "@/apis/service/sellOut/bargainList";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";

import {
  editColumns,
  costArr,
  bargainingArr,
  options1,
  options2,
  options3,
  options4,
  options5,
  options6,
  options7,
  options8,
  options9,
  options10,
  options11,
  options12,
  statusList,
  rate_status_options,
} from "./columns";
export default {
  components: {
    examFormss,
  },
  name: "bargainListDetail",
  mixins: [resToken],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size", "private_field"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "bargainListDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "YJD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },

  data() {
    return {
      size: "small",
      eaxmList: [],
      costArr,
      bargainingArr,
      editColumns,
      options1,
      options2,
      options3,
      options4,
      options5,
      options6,
      options7,
      options8,
      options9,
      options10,
      options11,
      options12,
      statusList,
      rate_status_options,
      projectTabs: "1",
      activeNames: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "20"],
      newTime: "",
      loading: false,
      queryType: "",
      queryId: "",
      status: "",
      sitem: null,

      orderItem: {},
      moneyDirItem: {},
      moneyItem: {},
      BossItem: {},
    };
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      const { id, type } = this.$route.query;
      this.queryId = id;
      this.queryType = type;
      this.projectTabs = "1";
      this.loading = true;
      await this.initData();
      this.loading = false;
    },

    handleClick(row) {
      console.log(row);
    },
    async refresh() {
      this.routeReGoto("bargainList", {});
    },
    async initData() {
      this.loading = true;
      const { code, message, data } = await asyncRequest.detail({
        bargainNo: this.queryId,
      });

      this.eaxmList = [];
      this.loading = false;
      if (code === 0) {
        this.sitem = JSON.parse(JSON.stringify(data));

        const { status, exam_info } = this.sitem;
        this.orderItem = {};
        this.moneyDirItem = {};
        this.moneyItem = {};
        this.BossItem = {};
        if (exam_info && exam_info.length > 0) {
          exam_info.forEach((e) => {
            console.log(e.status);
            switch (e.info_status + "") {
              case "0":
                this.orderItem = JSON.parse(JSON.stringify(e));
                break;
              case "2":
                this.moneyDirItem = JSON.parse(JSON.stringify(e));
                break;
              case "4":
                this.moneyItem = JSON.parse(JSON.stringify(e));
                break;
              case "7":
                this.BossItem = JSON.parse(JSON.stringify(e));
                break;
              default:
                this.orderItem = JSON.parse(JSON.stringify(e));
            }
          });
          this.eaxmList = JSON.parse(JSON.stringify(exam_info));
        }

        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();
    },

    // 点击业务审核的保存按钮

    async examFormSubmit(e, title) {
      if (!this.loading) {
        const { state, remark } = e;
        let model = {
          bargainNo: this.queryId,
          status: state,
          remark: remark,
        };
        await this.$confirm(`确定要提交${title}审批结果？`, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            const { after_price, lower_price } = this.sitem;
            if (
              this.status === "7" &&
              after_price * 1 < lower_price * 1 &&
              state + "" === "1"
            ) {
              this.loading = false;
              await this.$confirm(
                `当前商品同意议价后，售价已低于系统最低售价${this.sitem.lower_price}元！`,
                `最终售价已低于系统最低售价！是否继续？`,

                {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "error",
                }
              )
                .then(async () => {
                  this.loading = true;
                  await this.statusSubmit(model, title);
                })
                .catch(() => {
                  this.loading = false;
                  console.log("取消1");
                });
            } else {
              await this.statusSubmit(model, title);
            }
          })
          .catch(() => {
            this.loading = false;
            console.log("取消");
          });
      }
    },
    async statusSubmit(model, title) {
      const { code, data, message } = await asyncRequest.status(model);
      this.loading = false;
      // console.log("res", data);
      if (code === 0) {
        this.$notify.success({
          title: title + "成功！",
          message: "",
        });
        await this.initForm();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.bargainListDetail {
  position: relative;
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  .bargainListDetail-main {
    position: relative;
    padding: 10px;
    height: 100%;
    width: 100%;
  }
  .bargainListDetail-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>
