<template>
  <div class="sellAfterApplyDetail pagePadding">
    <div
      style="width: 100%"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <p>{{ status }}---{{ powers }}</p>

      <div class="detail-page-title">
        <span>销售售后申请编号：</span><span v-if="sitem">{{ sitem.returnCode}}</span>
    
      <!-- <el-button
        style="margin: 10px 0 20px 0"
        type="primary"
        plain
        class="fr"
        :size="'mini'"
        @click="statusConfirm('1', '发起审核流程', '')"
        v-if="status === '0' && powers.some((item) => item == '012')"
        >发起审核流程
      </el-button> -->
 </div>
      <status-bar
        v-if="newTime !== ''"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      />
       <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="ShowDataTableColumns"
          >
            <template slot="status">
              <el-tag
                :size="'mini'"
                :type="sitem.status == '0' ? 'warning' : ''"
                v-text="
                  (
                    statusOptions.find((item) => item.value == sitem.status) ||
                    {}
                  ).label || '--'
                "
              ></el-tag>
            </template>
            <template slot="order_type">
              <el-tag :size="'mini'" :type="''">{{
                sitem.status === "1" ? "销售出库单" : sitem.status === "2" ?"咨询出库单":"--"
              }}</el-tag>
            </template>
              <template slot="except_code">
              <el-tag :size="'mini'" :type="''">{{
                sitem.except_code === "1" ? "退货" : sitem.except_code === "2" ?"换货":sitem.except_code === "2" ?'改变售价':"--"
              }}</el-tag>
            </template>
          </show-data-table>
          <add-edit-form
            :sitem="sitem"
            :newTime="newTime"
            v-if="newTime != ''"
          ></add-edit-form>
        </el-collapse-item>
        <el-collapse-item
          title="业务审核"
          name="1"
          v-if="status === '1' && powers.some((item) => item == '034')"
        >
          <exam-form
            :statusList="statusList"
            :newTime="newTime"
            :disabled="false"
            :isMust="false"
            @searchChange="examForm"
          />
        </el-collapse-item>
        <el-collapse-item
          title="物流反馈"
          name="2"
          v-if="
            (status == '2' && powers.some((item) => item == '040')) ||
            status == '3' ||
            status == '4' ||
            status == '5'
          "
        >
          <express-feedback
            :sitem="sitem"
            :newTime="newTime"
            v-if="newTime != ''"
            @refresh="initData"
          ></express-feedback>
        </el-collapse-item>
        <el-collapse-item
          title="退货物流"
          name="4"
          v-if="status == '3' || status == '4'"
        >
          <return-express
            @refresh="initData(true)"
            :sitem="sitem"
            :newTime="newTime"
            v-if="newTime != ''"
          ></return-express>
        </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-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 addEditForm from "./components/addEditForm";
import ExpressFeedback from "./components/express_feedback";
import PurchaseCheck from "./components/purchase_check";
import ReturnExpress from "./components/returnExpress";
import ShowDataTableColumns from "./ShowDataTableColumns";
export default {
  name: "sellAfterApplyDetail",
  mixins: [mixinPage, resToken],
  components: {
    addEditForm,
    ExpressFeedback,
    PurchaseCheck,
    ReturnExpress,
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "sellAfterApplyDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
       activeTabs: "1",
      activeNames: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      status: "", //存储详情接口状态
      statusList: [],
      ShowDataTableColumns: ShowDataTableColumns,
      sitem: null,
      newTime: "",
      loading: false,
      statusOptions: [
        { value: "0", label: "业务审核" },
        { value: "1", label: "物流信息反馈" },
        // { value: "2", label: "反馈物流费" },
        { value: "3", label: "填写退货信息" },
        { value: "4", label: "已退货" },
        // { value: "5", 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" : "0";
        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(type) {
      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;

        if (can && can.length > 0) {
          this.sitem.class_cat = "";
          can.forEach((x, i) => {
            this.sitem.class_cat += i === 0 ? x.name : "/" + x.name;
          });
        }

        this.getNewTime();
        if (type) {
          this.$confirm("是否要跳转到销售售后退货单界面?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
          })
            .then(() => {
              this.routeGoto("/sellOut/returnOrder");
            })
            .catch(() => {
              console.log("取消");
            });
        }
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>
   
   