<template>
  <div class="sellAfterApplyDetail pagePadding">
    <div
      style="width: 100%"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <p>{{ status }}---{{ powers }}</p>
      <span>销售售后申请编号：</span>
      <span v-if="sitem">{{ sitem.returnCode }}</span>
      <el-button
        style="margin: 10px 0 20px 0"
        type="primary"
        plain
        :size="'mini'"
        @click="statusConfirm('1', '发起审核流程', '')"
        v-if="status === '0' && powers.some((item) => item == '012')"
        >发起审核流程
      </el-button>
      <status-bar
        style="margin: 20px 0 0 0"
        v-if="newTime !== ''"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      />
      <el-collapse v-model="activeNames">
        <el-collapse-item title="售后申请详情" name="1">
          <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 === '0' && 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-item title="审批记录" name="10">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'SHD'"
            :orderCode="orderCode"
          />
        </el-collapse-item>
      </el-collapse>
    </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";
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 {
      activeNames: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
      status: "", //存储详情接口状态
      statusList: [],
      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.status = res.data.status;
        this.sitem = res.data;
        this.orderCode = res.data.returnCode;
        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>
   
   