<template>
  <div class="sellReturnDetail 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="0">
              <show-data-table
                style="margin: 0; padding: 0"
                :sitem="sitem"
                v-if="status"
                :columns="showColumns"
              >
                <template slot="status">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (statusOptions.find((item) => item.value == 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="supplierName">
                  <span>{{ sitem.supplier_name }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <span>销售方公司编号：</span><span>{{ sitem.supplierNo }}</span>
                      </li>
                    </ul>
                    <i class="el-icon-warning-outline fr" slot="reference"></i>
                  </el-popover>
                </template>
                <template slot="companyName">
                  <span>{{ sitem.customer_name }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <span>购买方公司编号：</span
                        ><span>{{ sitem.customer_code }}</span>
                      </li>
                    </ul>
                    <i class="el-icon-warning-outline fr" slot="reference"></i>
                  </el-popover>
                </template>
                <template slot="is_active">
                  <span v-if="sitem.order_type === '1' || sitem.order_type === '2'"
                    ><span
                      >{{ sitem.is_activity === "1" ? "参与活动" : "不参与活动" }}/{{
                        sitem.good_type
                      }}</span
                    ></span
                  >
                  <span v-else>--</span>
                </template>
                <template slot="send_type">
                  <el-tag :size="'mini'">{{
                    sitem.send_type === "1"
                      ? "直接发货"
                      : sitem.send_type === "2"
                      ? "延迟发货"
                      : "--"
                  }}</el-tag>
                </template>
                <template slot="addrinfo">
                  <div
                    v-if="sitem && sitem.addrinfo && sitem.addrinfo.length > 0"
                    style="padding: 2px 8px 0 0"
                  >
                    <el-table
                      :data="sitem.addrinfo"
                      border
                      :size="'mini'"
                      row-key="key"
                      max-height="300px"
                    >
                      <el-table-column
                        label="发货申请单号"
                        prop="outCode"
                        width="160"
                        show-overflow-tooltip
                      />
                      <el-table-column
                        label="收货总数量"
                        prop="receipt_quantity"
                        width="110"
                        show-overflow-tooltip
                      />
                      <el-table-column
                        label="未发货数量"
                        prop="addr_wsend"
                        width="110"
                        show-overflow-tooltip
                      />
                      <el-table-column
                        label="已发货数量"
                        prop="addr_send"
                        width="110"
                        show-overflow-tooltip
                      />
                      <el-table-column
                        label="退货数量"
                        prop="return_num"
                        width="110"
                        show-overflow-tooltip
                      />

                      <el-table-column
                        label="收货人-联系电话-地址"
                        min-width="220"
                        show-overflow-tooltip
                      >
                        <template slot-scope="scope">
                          {{ scope.row.contactor }}-{{ scope.row.mobile }}-{{
                            scope.row.addr_info
                          }}{{ scope.row.addr }}
                        </template></el-table-column
                      >
                    </el-table>
                  </div>
                  <span v-else>--</span>
                </template>
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item title="商品信息" name="2">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :type="sitem && sitem.order_type ? sitem.order_type : ''"
                :skucode="sitem.skuCode"
                :spucode="sitem.good_code"
                :iscgd="false"
              />
            </el-collapse-item>
            <el-collapse-item
              title="业务审核"
              name="3"
              v-if="
                newTime !== '' &&
                sitem &&
                !(
                  (sitem.order_type === '1' || sitem.order_type === '2') &&
                  is_stock === '1'
                ) &&
                ppowers.some((i) => i == '1') &&
                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="4"
              v-if="
                sitem &&
                (status === '4' || status === '3' || status === '2') &&
                !(
                  ((sitem && sitem.order_type === '1') || sitem.order_type === '2') &&
                  is_stock === '1'
                )
              "
            >
              <com-exam-form
                v-if="newTime"
                :statusList="statusList"
                :newTime="newTime"
                :sitem="sitem"
                :disabled="
                  status === '4' ||
                  status === '3' ||
                  !(ppowers.some((i) => i == '2') && status === '2')
                "
                :isMust="false"
                @refresh="initData"
              />
            </el-collapse-item>
            <el-collapse-item
              title="采购主管审批"
              name="5"
              v-if="
                sitem &&
                ppowers.some((i) => i == '3') &&
                status === '3' &&
                !(
                  (sitem.order_type === '1' || sitem.order_type === '2') &&
                  is_stock === '1'
                )
              "
            >
              <exam-form
                v-if="newTime"
                :statusList="statusList"
                :newTime="newTime"
                :disabled="false"
                :isMust="false"
                @searchChange="dirExamForm"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'XSTHD'"
            :orderCode="queryId"
          />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="3"  v-if="sitem.order_type !== '1'">
          <flow-chart process_id="8" type="XSTHD" :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/sellReturn";
// import ReturnCheck from "./components/returnCheck";
import comExamForm from "./components/comExamForm";
import { showColumns } from "./components/ShowDataTableColumns";
import { xs_order_type_options } from "@/assets/js/statusList";
export default {
  name: "sellReturnDetail",
  mixins: [mixinPage, resToken],
  components: {
    comExamForm,
    // ReturnCheck,
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "sellReturnDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "XSTHD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      activeTabs: "1",
      showColumns: showColumns,
      statusOptions: [
        { value: "1", label: "待业务审批" },
        { value: "2", label: "待专员审批" },
        { value: "3", label: "待主管审批" },
        { value: "4", label: "退货完成" },
        { value: "5", label: "业务驳回" },
        { value: "6", label: "采购驳回" },
        { value: "7", label: "专员审批不通过" },
      ],
      xs_order_type_options,
      sitem: null, //传给组件
      activeNames: ["0", "1", "2", "3", "4", "5"],
      sitem: null,
      status: "", //存储详情接口状态
      statusList: [
        {
          value: "1",
          label: "待验货",
        },
        {
          value: "2",
          label: "待验货审核",
        },
        {
          value: "3",
          label: "待业务审核",
        },
      ],
      newTime: "",
      loading: false,
      is_stock: "",
      queryId: "",
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    async statusConfirm(status, message, remark) {
      this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          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" : "5";
        let message = e.state === "1" ? "提交审核结果" : "提交审核结果";
        await this.statusConfirm(type, message, e.remark);
      }
    },
    // 点击采购主管审核的保存按钮
    async dirExamForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "4" : "2";
        let message = e.state === "1" ? "提交审核结果" : "提交审核结果";
        await this.statusConfirm(type, message, e.remark);
      }
    },
    async setstatus(type, message, remark = "") {
      let _model = {
        returnCode: this.queryId,
        status: type,
        remark: remark,
      };
      let res = await asyncRequest.status(_model);
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          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 { code, data, message } = await asyncRequest.detail({
        returnCode: this.queryId,
      });
      if (code === 0) {
        const { is_stock, status } = data;
        this.is_stock = is_stock;
        this.status = status;
        this.sitem = data;
        this.getNewTime();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
  },
};
</script>
