<template>
  <div class="sellReturnDetail pagePadding">
    <p>{{ status }}--- {{ powers }}</p>
    <div
      style="width: 100%"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <div class="detail-page-title">
        <span>销售退货单编号：</span><span v-if="sitem">{{ sitem.id }}</span>
        <el-button
          type="danger"
          plain
          class="fr"
          style="margin: 0 0 0 15px"
          :size="'mini'"
          @click="statusConfirm('-1', '作废该条信息', '')"
          v-if="status === '0' && powers.some((item) => item == '015')"
          >作废该条信息
        </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="0">
          <edit-form :sitem="sitem" :newTime="newTime" v-if="newTime != ''" />
        </el-collapse-item>
        <el-collapse-item
          title="发起流程"
          name="1"
          v-if="status === '0' && powers.some((item) => item == '012')"
        >
          <div style="height: 50px; padding: 0 0 15px 0">
            <el-button
              type="primary"
              plain
              :size="'mini'"
              class="fr"
              @click="statusConfirm('1', '发起审核流程', '')"
              >发起审核流程
            </el-button>
          </div>
        </el-collapse-item>

        <el-collapse-item
          title="业务审核"
          name="2"
          v-if="powers.some((item) => item == '041') && status === '1'"
        >
          <exam-form
            v-if="newTime"
            :statusList="statusList"
            :newTime="newTime"
            :disabled="false"
            :isMust="false"
            @searchChange="examForm"
          />
        </el-collapse-item>
        <el-collapse-item
          title="通知库管"
          name="1"
          v-if="powers.some((item) => item == '037') && status === '2'"
        >
          <div
            style="height: 60px; padding: 20px 0 0px 0"
            v-if="status === '2'"
          >
            <el-button
              type="primary"
              class="fr"
              size="mini"
              @click="statusConfirm('3', '确定我已知晓并同意')"
              >我已知晓</el-button
            >
          </div>
        </el-collapse-item>
        <el-collapse-item
          title="通知采购部门"
          name="1"
          v-if="powers.some((item) => item == '038') && status === '3'"
        >
          <div
            style="height: 60px; padding: 20px 0 0px 0"
            v-if="status === '3'"
          >
            <el-button
              type="primary"
              class="fr"
              size="mini"
              @click="statusConfirm('4', '确定我已知晓并同意')"
              >我已知晓</el-button
            >
          </div>
        </el-collapse-item>
        <el-collapse-item
          title="通知财务部门"
          name="1"
          v-if="powers.some((item) => item == '039') && status === '4'"
        >
          <div
            style="height: 60px; padding: 20px 0 0px 0"
            v-if="status === '4'"
          >
            <el-button
              type="primary"
              class="fr"
              size="mini"
              @click="statusConfirm('5', '确定我已知晓并同意')"
              >我已知晓</el-button
            >
          </div>
        </el-collapse-item>
      </el-collapse>
      </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
         <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'QRTHD'"
            :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/sellReturn";
// import ReturnCheck from "./components/returnCheck";
import editForm from "./components/editForm";
export default {
  name: "sellReturnDetail",
  mixins: [mixinPage, resToken],
  components: {
    editForm,
    // ReturnCheck,
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "sellReturnDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      activeTabs:"1",
      statusOptions: [
        { value: "0", label: "待发起流程" },
        { value: "1", label: "待业务审核" },
        { value: "2", label: "待通知库管" },
        { value: "3", label: "待通知采购" },
        { value: "4", label: "待通知财务部门" },
        { value: "5", label: "退货已完成" },
      ],
      sitem: null, //传给组件
      // --
      orderCode: "",
      activeNames: ["0", "1", "2","3","4","5", ],
      sitem: null,
      status: "", //存储详情接口状态
      statusList: [
        {
          value: "1",
          label: "待验货",
        },
        {
          value: "2",
          label: "待验货审核",
        },
        {
          value: "3",
          label: "待业务审核",
        },
      ],
      newTime: "",
      loading: false,
      qid: "",
    };
  },
  mounted() {
    this.qid = this.$route.query.id;
    this.initData();
  },
  methods: {
    async statusConfirm(status, message, remark) {
      this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (status === "-1") {
            await this.delete(message);
          } else {
            await this.setstatus(status, message, remark);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    // 点击业务审核的保存按钮
    async examForm(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, message, remark = "") {
      let _model = {
        returnCode: this.qid,
        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);
      }
    },
    async initData() {
      const res = await asyncRequest.detail({ returnCode: this.qid });
      if (res && res.code === 0 && res.data) {
        this.status = res.data.status;
        this.sitem = res.data;
        this.orderCode = res.data.returnCode;
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>
   
   