<template>
  <div class="returnOrderDetail pagePadding">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <!-- <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>
              </show-data-table>
            </el-collapse-item>

            <el-collapse-item
              title="退货验收记录"
              name="0"
              v-if="newTime !== '' && sitem && sitem.return_tag === '2'"
            >
              <!----047--->
              <return-record
                :sitem="sitem"
                v-if="newTime !== ''"
                @refresh="initData"
                :newTime="newTime"
              ></return-record>
            </el-collapse-item>

            <el-collapse-item
              title="退货验收审核"
              name="1"
              v-if="
                status == '2' ||
                status == '3' ||
                (status == '4' && newTime !== '' && sitem && sitem.return_tag === '2')
              "
            >
              <!----042--->
              <return-check
                v-if="newTime != ''"
                :newTime="newTime"
                :sitem="sitem"
                @refresh="initData"
              ></return-check>
            </el-collapse-item>
            <el-collapse-item
              title="业务审核"
              name="2"
              v-if="
                status == '3' ||
                (status == '4' && newTime !== '' && sitem && sitem.return_tag === '2')
              "
            >
              <audit-form
                v-if="newTime != ''"
                :newTime="newTime"
                :sitem="sitem"
                @refresh="initData"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'CKTHD'"
            :orderCode="queryId"
          />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="3"  v-if="sitem && sitem.received_num !== '0'">
          <flow-chart process_id="7" type="CKTHD" :orderCode="queryId" />
       </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/returnOrder";
import ReturnRecord from "./components/return-record";
import ReturnCheck from "./components/returnCheck";
import auditForm from "./components/auditForm";
import ShowDataTableColumns from "./ShowDataTableColumns";

export default {
  name: "returnOrderDetail",
  mixins: [mixinPage, resToken],
  components: {
    ReturnRecord,
    ReturnCheck,
    auditForm,
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "returnOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "CKTHD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      activeTabs: "1",
      ShowDataTableColumns: ShowDataTableColumns,
      statusOptions: [
        { value: "0", label: "取消" },
        { value: "1", label: "退货商品验收" },
        { value: "2", label: "退货验收审核" },
        { value: "3", label: "退货业务审核" },
        { value: "4", label: "退货已完成" },
      ],
      sitem: null, //传给组件
      orderCode: "",
      activeNames: ["-1", "0", "1", "2"],
      status: "", //存储详情接口状态
      statusList: [
        {
          value: "1",
          label: "待验货",
        },
        {
          value: "2",
          label: "待验货审核",
        },
        {
          value: "3",
          label: "待业务审核",
        },
      ],
      newTime: "",
      loading: false,
      queryId: "",
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;

    this.initData();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    // 点击业务审核的保存按钮
    async examForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "4" : e.rebut;
        await this.setstatus(type, "提交业务审核", e.remark);
      }
    },
    //
    async setstatus(type, detail, remark) {
      await this.$confirm(`确定要${type === "4" ? "同意" : "驳回"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let _model = {
            thNo: this.queryId,
            status: type,
            remark: remark,
          };
          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);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async initData() {
      this.newTime = "";
      let model = {
        thNo: this.queryId,
      };
      const res = await asyncRequest.detail(model);
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, orderCode, can } = this.sitem;
        this.status = status;
        this.orderCode = orderCode;
        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();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }

      console.log(this.sitem);
    },
  },
};
</script>
