<template>
  <div>
    <el-dialog title="详情" :visible.sync="dialogTableVisible" width="1280px">
      <detailDatatable
        :newTime="newTime"
        v-if="sitem"
        :sitem="sitem"
        :columns="columns"
        :border="true"
      >
        <template slot="status">
          <el-tag
            :size="'mini'"
            :type="sitem.status == '0' ? 'warning' : ''"
            v-text="
              (
                (sitem.is_receive === '0' ? status2Options : status1Options).find(
                  (item) => item.id === 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="is_receive">
          <el-tag :size="'mini'" :type="''">{{
            sitem.is_receive === "1"
              ? "已收到货"
              : sitem.is_receive === "0"
              ? "未收到"
              : "--"
          }}</el-tag>
        </template>
        <template slot="except_code">
          <el-tag
            :size="'mini'"
            v-text="
              (expect_options.find((item) => item.value == sitem.except_code) || {})
                .label || '--'
            "
          ></el-tag>
        </template>

        <template slot="error_img">
          <img
            class="fl hover"
            style="width: 23px; height: 23px; margin: 0 5px 0 0"
            v-viewer
            v-for="(si, i) in sitem.error_img"
            :src="si"
            :key="si + i"
          />
        </template>
      </detailDatatable>
    </el-dialog>
    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="listColumns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
    >
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="''"
          v-text="
            (
              (scope.row.is_receive === '0' ? status2Options : status1Options).find(
                (item) => item.id == scope.row.status
              ) || {}
            ).label || '--'
          "
        ></el-tag>
      </template>
      <!-- 费用承担方 -->
      <template #post_own="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '0' ? 'warning' : ''"
          v-text="
            (post_ownOptions.find((item) => item.id == scope.row.post_own) || {}).label ||
            '--'
          "
        ></el-tag>
      </template>
      <!-- 期望意愿 -->
      <template #except_code="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '0' ? 'warning' : ''"
          v-text="
            (except_codeOptions.find((item) => item.id == scope.row.except_code) || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #operation="{ scope }">
        <el-tooltip effect="dark" content="详情" placement="top">
          <i class="el-icon-view tb-icon" @click="dilog(scope.row.returnCode)"></i>
        </el-tooltip>
      </template>
    </ex-table>
  </div>
</template>

<script>
import { columns, statusOptions, listColumns } from "./columns";
// import { cg_order_type_options } from "@/assets/js/statusList";

//————————————————
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/sellAfterApply";
import { xs_order_type_options } from "@/assets/js/statusList";
import { mapGetters } from "vuex";
import detailDatatable from "../detail-data-table";

export default {
  mixins: [mixinPage, resToken],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size", "private_field"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "standingBookDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  components: {
    detailDatatable,
  },
  props: ["queryId"],
  data() {
    return {
      loading: true,
      dialogTableVisible: false,
      parmValue: {
        good_code: "", //商品编码
        orderCode: "",
        outCode: "",
        except_code: "", //期望意愿
        serviceValue: "", //处理状态
        start: "",
        end: "",
        order_type: "",
        customer_code: "", //客户公司code
        returnCode: "", //售后单号
        apply_name: "", //申请人名称
        good_name: "", //商品名称
        except_code: "", //期望意愿
        company_name: "", //申请人部门
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      listColumns,
      // cg_order_type_options,
      except_codeOptions: [
        { id: "1", label: "退货" },
        { id: "2", label: "换货" },
        { id: "3", label: "改变售价" },
      ],
      post_ownOptions: [
        { id: "1", label: "客户承担" },
        { id: "2", label: "公司承担" },
        { id: "3", label: "供应商承担" },
      ],
      xs_order_type_options,
      activeTabs: "1",
      expect_options: [],
      expect_options1: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "换货",
        },
      ],
      expect_options0: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "补发",
        },
      ],
      activeNames: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      status: "", //存储详情接口状态
      statusList: [],
      sitem: null,
      newTime: "",
      loading: false,
      // 收到货状态
      status1Options: [
        { id: "1", label: "待业务审核" },
        { id: "2", label: "待采购审核" },
        { id: "3", label: "待设置仓库" },
        { id: "4", label: "待客户退货" },
        { id: "5", label: "售后已完成" },
        { id: "6", label: "业务已驳回" },
        { id: "7", label: "采购已驳回" },
        { id: "8", label: "申请已取消" },
      ],
      // 未收到货状态
      status2Options: [
        { id: "1", label: "待业务审核" },
        { id: "2", label: "待采购审核" },
        { id: "3", label: "待同意退货" },
        { id: "5", label: "售后已完成" },
        { id: "6", label: "业务已驳回" },
        { id: "7", label: "采购已驳回" },
        { id: "8", label: "申请已取消" },
      ],
      orderCode: "",
      return_code: "",
      code: "",
      // cost_detailArr: [],

      //——————————————————————
      columns,
    };
  },
  mounted() {
    // this.initData();
    this.searchList();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async initData(code) {
      let model = {
        // returnCode: this.queryId,
        returnCode: 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;
        this.sitem.error_img = this.sitem.error_img.split(",");
        this.expect_options = JSON.parse(
          JSON.stringify(
            this.sitem.is_receive === "1" ? this.expect_options1 : this.expect_options0
          )
        );
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async searchList() {
      //通过订单编号去查询所有列表、当订单编号为空时，所有涉及字段不展示
      this.parmValue.orderCode = this.queryId;
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    dilog(returnCode) {
      this.dialogTableVisible = true;
      this.initData(returnCode);
    },
  },
};
</script>

<style></style>
