<template>
  <div>
    <el-dialog title="详情" :visible.sync="dialogTableVisible" width="1280px">
      <detailDatatable
        style="margin: 0; padding: 0"
        :sitem="sitem"
        v-if="sitem"
        :columns="columns"
        :border="true"
      >
        <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="收货人-联系电话-地址"
                min-width="300"
                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-column label="收货总数量" prop="receipt_quantity" width="100" />

              <el-table-column label="退货数量" prop="return_num" width="100">
              </el-table-column>
              <el-table-column label="未发货数量" prop="wsend_num" width="90" />
              <el-table-column label="已发货数量" prop="send_num" width="90" />
            </el-table>
          </div>
          <span v-else>--</span>
        </template>
      </detailDatatable>
    </el-dialog>

    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="listColumens"
      :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="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.returnCode)"></i>
        </el-tooltip>
      </template>
    </ex-table>
  </div>
</template>

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

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

export default {
  components: {
    detailDatatable,
  },
  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 ?? [];
    },
  },
  props: ["queryId"],
  data() {
    return {
      listStatusOptions: [
        { id: "1", label: "待业务审批" },
        { id: "2", label: "待专员审批" },
        { id: "3", label: "待主管审批" },
        { id: "4", label: "退货完成" },
        { id: "5", label: "业务驳回" },
        { id: "6", label: "采购驳回" },
        { id: "7", label: "专员审批不通过" },
      ],
      parmValue: {
        returnCode: "", //销售退货code
        orderCode: "", //销售订单code
        company_name: "",
        apply_name: "", //申请人
        start: "",
        end: "",
        status: "", //节点状态
        page: 1, // 页码
        size: 15, // 每页显示条数
        order_type: "",
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      loading: true,
      dialogTableVisible: false,
      listColumens,

      xs_order_type_options,
      statusOptions,
      is_stock: "",

      // cost_detailArr: [],

      //——————————————————————
      columns,
      newTime: "",
      status: "",
      sitem: null,
      loading: false,
    };
  },
  mounted() {
    // this.searchList();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async initData(returnCode) {
      const { code, data, message } = await asyncRequest.detail({
        // returnCode: this.queryId,
        returnCode,
      });
      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);
      }
    },

    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>
