<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) => item.id === 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="status === '1' && ppowers.some((i) => i == '1')"
            >
              <exam-form
                :statusList="statusList"
                :newTime="newTime"
                :disabled="false"
                :isMust="false"
                @searchChange="examForm"
              />
            </el-collapse-item>
            <el-collapse-item
              title="采购审核"
              name="4"
              v-if="
                (status == '2' ||
                  status == '3' ||
                  status == '4' ||
                  status == '5' ||
                  status == '7') &&
                newTime != ''
              "
            >
              <purchase-exam
                :sitem="sitem"
                :newTime="newTime"
                v-if="newTime != ''"
                @refresh="initData"
              />
            </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 { xs_order_type_options } from "@/assets/js/statusList";
export default {
  name: "sellAfterApplyDetail",
  mixins: [mixinPage, resToken],
  components: {
    purchaseExam,
    ExpressFeedback,
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "sellAfterApplyDetail"
        ) || {};
      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: "申请已取消" },
      ],
      // 未收到货状态
      status2Options: [
        { id: "1", label: "待业务审核" },
        { id: "2", label: "待采购审核" },
        { id: "3", label: "待同意退货" },
        { id: "5", label: "售后已完成" },
        { id: "6", label: "业务已驳回" },
        { id: "7", label: "采购已驳回" },
        { id: "8", label: "申请已取消" },
      ],
      orderCode: "",
      return_code: "",
      code: "",
    };
  },
  mounted() {
    this.code = this.$route.query.id;

    this.initData();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    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) {
        let type = e.state === "1" ? "2" : "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);
      }
    },
    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>
