<template>
  <div class="sellAfterApplyDetail pagePadding">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <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="sellshowColumns"
              >
                <template slot="status">
                  <el-tag
                    :size="'mini'"
                    :type="sitem.status == '0' ? 'warning' : ''"
                    v-text="
                      (
                        (sitem.is_receive + ''=== '0' ? status2Options : status1Options).find(
                          (item) => String(item.id) === String( sitem.status)
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
                <template slot="order_type">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        xs_order_type_options.find(
                          (item) => item.id == sitem.order_type
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
                <template slot="is_receive">
                  <el-tag :size="'mini'" :type="''">{{
                    sitem.is_receive + '' === "1"
                      ? "销售订单退货，采购订单也退货"
                      : sitem.is_receive + '' === "0"
                      ? "未收到"
                      : "--"
                  }}</el-tag>
                </template>
                <template slot="except_code">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        expect_options.find((item) => item.value == sitem.except_code) ||
                        {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>

                <template slot="error_img">
                  <img
                    class="fl hover"
                    style="width: 23px; height: 23px; margin: 0 5px 0 0"
                    v-viewer
                    v-for="(si, i) in sitem.error_img"
                    :src="si"
                    :key="si + i"
                  />
                </template>
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item title="商品信息" name="2">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== '' && sitem"
                :type="sitem.order_type"
                :skucode="sitem.skuCode"
                :spucode="sitem.good_code"
              />
            </el-collapse-item>

            <!-- <el-collapse-item
              title="业务审核"
              name="3"
              v-if="isSupertube && String(sitem.status) === '1' && this.ppowers.includes('1')"
            >
              <exam-form
                :statusList="statusList"
                :newTime="newTime"
                :disabled="false"
                :isMust="false"
                @searchChange="examForm"
              />
            </el-collapse-item> -->

            <!-- <el-collapse-item name="4" title="供应商驳回，待采购审核"  v-if="isSupertube && String(sitem.status) === '2' && this.ppowers.includes('2')">
              <purchase-exam :sitem="sitem" @changeStatus="handlePurchaseConfirm" />
            </el-collapse-item> -->

            <el-collapse-item name="5" title="供应商审核"   v-if="!isSupertube && String(sitem.status) === '9' && this.ppowers.includes('9')">
              <supplier-exam :sitem="sitem" @changeStatus="handleSupplierExam" />
            </el-collapse-item>

            <!-- <el-collapse-item name="6" title="待客户退货"  v-if="isSupertube && String(sitem.status) === '4' && this.ppowers.includes('4')">
              <wait-return :sitem="sitem" @changeStatus="handleExpress" />
            </el-collapse-item> -->

            <el-collapse-item name="7" title="业务公司修改待供应商确认"  v-if="!isSupertube && String(sitem.status) === '10' && this.ppowers.includes('10')">
              <supplier-confrim :sitem="sitem"  @changeStatus="handleSupplierConfirm"/>
            </el-collapse-item>
            
            <!-- <el-collapse-item
              title="设置退货仓库"
              name="4"
              v-if="
                sitem &&
                sitem.is_receive === '1' &&
                (status == '3' || status == '4' || status == '5')
              "
            >
              <express-feedback
                :sitem="sitem"
                :newTime="newTime"
                v-if="newTime != ''"
                @refresh="initData"
              ></express-feedback>
            </el-collapse-item> -->
          </el-collapse>
        </el-tab-pane>

        
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'SHD'"
            :orderCode="orderCode"
          />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="4"  v-if="newTime !== ''">
          <flow-chart process_id="6" type="SHD" :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/sellAfterApply";
import ExpressFeedback from "./components/express_feedback";
import { sellshowColumns } from "./columns";
import purchaseExam from "./components/purchase_exam";
import SupplierExam from "./components/supplier_exam.vue";
import { xs_order_type_options } from "@/assets/js/statusList";
import WaitReturn from "./components/wait-return.vue";
import SupplierConfrim from "./components/supplier-confirm.vue";
import { mapGetters } from "vuex";

export default {
  name: "sellAfterApplyDetail",
  mixins: [mixinPage, resToken],
  components: {
    WaitReturn,
    SupplierExam,
    purchaseExam,
    ExpressFeedback,
    SupplierConfrim
  },
  computed: {
    ...mapGetters(['isSuppertube']),
    powers() {
      const tran = this.$store.getters.btnList.find(
          (item) => item.menu_route == "supplierSellAfterApplyDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "SHD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      xs_order_type_options,
      activeTabs: "1",
      expect_options: [],
      expect_options1: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "换货",
        },
      ],
      expect_options0: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "补发",
        },
      ],
      activeNames: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      status: "", //存储详情接口状态
      statusList: [],
      sellshowColumns: sellshowColumns,
      sitem: null,
      newTime: "",
      loading: false,
      // 收到货状态
      status1Options: [
        { id: "1", label: "待业务审核" },
        { id: "2", label: "供应商驳回,待采购审核" },
        // { id: "3", label: "待设置仓库" }, // 废弃
        { id: "4", label: "待客户退货" },
        { id: "5", label: "售后已完成" },
        { id: "6", label: "业务已驳回" },
        // { id: "7", label: "采购已驳回" }, // 废弃
        { id: "8", label: "申请已取消" },
        { id: "9", label: "供应商审核" },
        { id: "10", label: "业务公司修改,待供应商确认" }
      ],
      // 未收到货状态
      status2Options: [
        { id: "1", label: "待业务审核" },
        { id: "2", label: "供应商驳回,待采购审核" },
        // { id: "3", label: "待设置仓库" }, // 废弃
        { id: "4", label: "待客户退货" },
        { id: "5", label: "售后已完成" },
        { id: "6", label: "业务已驳回" },
        // { id: "7", label: "采购已驳回" }, // 废弃
        { id: "8", label: "申请已取消" },
        { id: "9", label: "供应商审核" },
        { id: "10", label: "业务公司修改,待供应商确认" }
      ],
      orderCode: "",
      return_code: "",
      code: "",
    };
  },
  mounted() {
    this.code = this.$route.query.id;

    this.initData();
  },
  methods: {
    async handleExpress(data){
      let res = await asyncRequest.express({
        ...data,
        returnCode: this.code
      });
      
      if (res && res.code === 0) {
        this.$notify.success({
          title: "修改成功！",
          message: "",
        });
        await this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async handleSupplierConfirm(data){
      let res = await asyncRequest.status({
        ...data,
        returnCode: this.code
      });
      
      if (res && res.code === 0) {
        this.$notify.success({
          title: "修改成功！",
          message: "",
        });
        await this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async handleSupplierExam(data){
      let res = await asyncRequest.status({
        ...data,
        returnCode: this.code
      });
      
      if (res && res.code === 0) {
        this.$notify.success({
          title: "修改成功！",
          message: "",
        });
        await 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();
    },
    async handlePurchaseConfirm(data){
      const model = {
        returnCode: this.code,
        ...data
      };

      let res = await asyncRequest.status(model);
      if (res && res.code === 0) {
        this.$notify.success({
          title: "修改成功！",
          message: "",
        });
        await this.initData();
        
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async statusConfirm(status, message, remark = "") {
      this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (status + '' === "-1") {
            await this.deleteById(message);
          } else {
            await this.setstatus(status, message, remark);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    // 点击业务审核的保存按钮
    async examForm(e) {
      console.log(e);
      if (!this.loading) {
        //通过为9「待供应商审核」，不通过为6「业务已驳回」
        let type = e.state + '' === "1" ? "9" : "6";
        let message = e.state + '' === "1" ? "提交业务审核" : "提交业务审核";
        await this.statusConfirm(type, message, e.remark);
      }
    },
    //
    async setstatus(type, detail, remark = "") {
      const model = {
        remark,
        returnCode: this.code,
        status: type,
      };
      let res = await asyncRequest.status(model);

      if (res && res.code === 0) {
        this.$notify.success({
          title: "修改成功！",
          message: "",
        });
        await this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    hasPermission({status, process}){
      console.log(this.sitem)
      return !this.isSupertube && String(this.sitem.status) === status && this.ppowers.includes(process)
    },
    async initData() {
      let model = {
        returnCode: this.code,
      };
      const res = await asyncRequest.detail(model);
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, can, returnCode } = this.sitem;
        this.status = status;
        this.orderCode = returnCode;
        this.sitem.error_img = this.sitem.error_img.split(",");
        this.expect_options = JSON.parse(
          JSON.stringify(
            this.sitem.is_receive + '' === "1" ? this.expect_options1 : this.expect_options0
          )
        );
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>
