<template>
  <div>
    <el-dialog title="详情" :visible.sync="dialogTableVisible" width="1280px">
      <detailDatatable
        :newTime="newTime"
        v-if="newTime !== '' && sitem"
        :sitem="sitem"
        :columns="columns"
        :border="true"
      >
        <template slot="company">
          <span>{{ sitem.company }}</span>
          <el-popover placement="top" width="300" trigger="hover">
            <ul>
              <li>
                <span>购买方公司编号：</span><span>{{ sitem.companyNo }}</span>
              </li>
            </ul>
            <i class="el-icon-warning-outline fr" slot="reference"></i>
          </el-popover>
        </template>
        <template slot="wsm">
          <span>{{ sitem.wsm_supplier }}/{{ sitem.wsm_name }}</span>
          <el-popover placement="top" width="300" trigger="hover">
            <ul>
              <li>
                <span>仓库公司编号：</span><span>{{ sitem.wsm_supplierNo }}</span>
              </li>
              <li>
                <span>仓库名称编号：</span><span>{{ sitem.wsm_code }}</span>
              </li>
            </ul>
            <i class="el-icon-warning-outline fr" slot="reference"></i>
          </el-popover>
        </template>
        <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'"
            v-text="
              (cg_order_type_options.find((item) => item.id == sitem.order_type) || {})
                .label || '--'
            "
          ></el-tag>
        </template>
        <template slot="sendtype">
          <span>{{
            sitem.sendtype == "1"
              ? "公司自提"
              : sitem.sendtype === "2"
              ? "供应商包邮"
              : "--"
          }}</span></template
        >
        <template slot="good_price">
          <span
            v-if="
              newTime !== '' &&
              private_field &&
              private_field.length > 0 &&
              private_field.some((item) => item == '1')
            "
            >{{ sitem.good_price }}</span
          >
          <span v-else>******</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();
      "
    >
      <template #good_name="{ scope }">
        <span>{{ scope.row.good_name }}</span>
        <span v-for="(si, sii) in scope.row.speclist" :key="si + sii"
          >{{ sii === 0 ? "_" : "-" }}{{ si.spec_name }}[{{ si.spec_value }}]</span
        >
      </template>
      <template #order_type="{ scope }">
        <el-tag
          :size="tablebtnSize"
          v-text="
            (cg_order_type_options.find((item) => item.id == scope.row.order_type) || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '0' ? 'warning' : ''"
          v-text="
            (statusOptions.find((item) => item.value == 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.wsm_in_code)"></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 asyncRequestDetail from "@/apis/service/purchaseIn/purchaseOrder";
import asyncRequest from "@/apis/service/purchaseIn/wsmInOrder";

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 {
      parmValue: {
        order_type: "",
        // bk_code: "", // 备库编码
        wsm_in_code: "", // 入库单号
        cgdNo: "", // 采购单编码
        apply_name: "", // 申请人
        good_name: "", // 产品名称
        good_type_code: "", // 产品属性编号

        status: "", //状态

        wsm_code: "", //入货仓库编码
        start: "", //新建起始时间
        end: "", // 新建结束时间
        last_start: "", //最后入库时间开始
        last_end: "", //最后入库时间结束
        page: 1, // 页码
        size: 15, // 每页显示条数
        supplierNo: "",
        company_name: "", //部门
        wsm_supplierNo: "",
      },
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        // _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      loading: true,
      dialogTableVisible: false,
      listColumns,

      cg_order_type_options,
      statusOptions,
      status: "",

      //——————————————————————
      columns,
      newTime: "",
      sitem: null,
      loading: false,
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async initData(code) {
      const res = await asyncRequestDetail.orderininfo({ wsm_in_code: code });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, can, wsm_in_code } = this.sitem;
        this.status = status;
        if (can && can.length > 0) {
          this.sitem.class_cat = "";
          can.forEach((x, i) => {
            this.sitem.class_cat += i === 0 ? x.name : "/" + x.name;
          });
        }
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async searchList() {
      //通过采购编号去查询所有列表、当采购编号为空时，所有涉及字段不展示
      this.parmValue.cgdNo = this.queryId;
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));

      let _model = {
        cgdNo: model.cgdNo,
        wsm_in_code: model.wsm_in_code,
        start: model.start,
        end: model.end,
        order_type: model.order_type,
        good_type_code: model.good_type_code,
        apply_name: model.apply_name,
        wsm_code: model.wsm_code,
        good_name: model.good_name,
        supplie: "",
        supplierNo: model.wsm_supplierNo,
        status: model.status,
        page: model.page,
        size: model.size,
        company_name: model.company_name,
      };
      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(code) {
      this.dialogTableVisible = true;
      this.initData(code);
    },
  },
};
</script>

<style></style>
