<template>
  <div>
    <el-dialog title="详情" :visible.sync="dialogTableVisible" width="1280px">
      <detailDatatable
        :newTime="newTime"
        v-if="newTime !== ''"
        :sitem="sitem"
        :columns="columns"
        :border="true"
      >
        <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>
        <template slot="order_type">
          <el-tag :size="'mini'" :type="''">{{
            sitem.status === "1" ? "发货申请单" : "竞价发货申请单"
          }}</el-tag>
        </template>
        <template slot="sale_price">
          <span>{{
            private_field &&
            private_field.length > 0 &&
            private_field.some((item) => item == "2")
              ? sitem.sale_price
              : "******"
          }}</span>
        </template>
        <template slot="total_price">
          <span>{{
            private_field &&
            private_field.length > 0 &&
            private_field.some((item) => item == "2")
              ? sitem.total_price
              : "******"
          }}</span>
        </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();
      "
      @selection="selection_change"
    >
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '0' ? 'warning' : ''"
          v-text="
            (listStatusOptions.find((item) => item.id == scope.row.status) || {}).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.outCode)"></i>
        </el-tooltip>
      </template>
    </ex-table>
  </div>
</template>

<script>
import { columns, 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/sellOutOrder";
import { mapGetters } from "vuex";
import detailDatatable from "../detail-data-table";

export default {
  components: {
    detailDatatable,
  },
  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 ?? [];
    },
  },
  mixins: [mixinPage, resToken],
  props: ["queryId"],
  data() {
    return {
      listColumns,
      loading: true,
      dialogTableVisible: false,
      parmValue: {
        order_type: "",
        orderCode: "",
        apply_name: "", //申请人名称
        cgdNo: "",
        start: "",
        end: "",
        // supplierNo: "",
        companyNo: "",
        good_code: "",
        good_name: "",
        outCode: "",
        status: "",
        company_name: "", //申请人部门
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      listStatusOptions: [
        { id: "0", label: "待发货" },
        { id: "1", label: "待库管发货" },
        { id: "2", label: "已发货待收货" },
        { id: "3", label: "已收货" },
        { id: "4", label: "已全部退货" },
      ],
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },

      // cg_order_type_options,
      // statusOptions,
      // cost_detailArr: [],
      returnCode: "",
      returnCode_type: "",
      activeTabs: "1",
      activeNames: ["0", "1", "2", "3", "4"],
      orderOptions: [
        { id: "1", label: "线上商品销售" },
        { id: "2", label: "项目线上商品销售" },
        { id: "3", label: "竞价单销售" },
        { id: "4", label: "项目竞价单销售" },
        { id: "5", label: "平台部订单销售" },
      ],
      statusOptions: [
        { value: "0", label: "待采购备货" },
        { value: "1", label: "待库管发货" },
        { value: "2", label: "已发货待收货" },
        { value: "3", label: "已收货" },
        { value: "4", label: "已全部退货" },
      ],
      // 收到货状态
      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: "申请已取消" },
      ],
      returnShowColumns: [
        {
          prop: "is_th",
          label: "物流承担方",
          _slot_: "is_th",
          span: 6,
        },
        {
          prop: "addr",
          label: "收货地址",
          _slot_: "addr",
          span: 18,
        },
      ],
      returnShowResColumns: [
        {
          prop: "post_fee",
          label: "退货物流费用",
          span: 6,
        },
        {
          prop: "post_company",
          label: "退货物流公司",
          span: 6,
        },
        {
          prop: "post_code",
          label: "退货物流单号",
          span: 12,
        },
      ],
      expect_options: [],
      expect_options1: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "换货",
        },
      ],
      expect_options0: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "补发",
        },
      ],
      is_th_options: [
        {
          value: "1",
          label: "收货人承担",
        },
        {
          value: "2",
          label: "业务公司承担",
        },
        {
          value: "3",
          label: "供应商承担",
        },
      ],
      //——————————————————————
      columns,
      newTime: "",
      status: "",
      sitem: null,
      loading: false,
      did: "", //详情页上的id
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async initData(outCode) {
      this.returnCode = "";
      this.returnCode_type = "";
      let model = {
        // outCode: this.queryId,
        outCode: outCode,
      };
      const res = await asyncRequest.detail(model);
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, can, id, order_return } = this.sitem;
        this.status = status;
        this.did = id;
        if (order_return) {
          this.returnCode = order_return.returnCode;
          this.returnCode_type = order_return.status;
          await this.receive_initData(this.returnCode);
        }
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async receive_initData(code) {
      let model = {
        returnCode: code,
      };
      const res = await asyncRequest.afterinfo(model);
      if (res && res.code === 0 && res.data) {
        this.resitem = res.data;
        this.resitem.error_img = this.resitem.error_img.split(",");
        this.expect_options = JSON.parse(
          JSON.stringify(
            this.resitem.is_receive === "1" ? this.expect_options1 : this.expect_options0
          )
        );
      } 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);
        this.tableData.forEach((v) => {
          v.good_class = "";
          if (v.can && v.can.length > 0) {
            v.can.forEach((x, i) => {
              v.good_class += i === 0 ? x.name : "/" + x.name;
            });
          }
        });
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },

    dilog(outCode) {
      this.dialogTableVisible = true;
      this.initData(outCode);
    },
  },
};
</script>

<style></style>
