<template>
  <div class="spaceCount pagePadding" style="padding: 0">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <ex-table
        v-loading="loading"
        :table="table"
        :data="tableData"
        :columns="columns"
        :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 #table-header="{}">
          <div style="width: 100%" class="min">
            <el-row style="padding: 0 0 0 80px">
              <el-col :span="12" style="width: 300px">
                <search-work-company
                  :value="parmValue.companyNo"
                  :names="''"
                  :size="searchSize"
                  :is-detail="id !== 'add'"
                  :placeholder="'业务公司'"
                  @searchChange="supplierChange"
                />
              </el-col>
              <el-col :span="10" style="width: 330px; padding: 0 0 0 10px">
                <el-input
                  v-model="parmValue.type_code"
                  :size="searchSize"
                  placeholder="商品属性编码"
                />
              </el-col>

              <!-- 重置 -->
              <el-col :span="4" style="width: 66px" class="fr">
                <el-button
                  :size="searchSize"
                  class="fl"
                  type="warning"
                  @click="restSearch"
                  >重置</el-button
                >
              </el-col>
            </el-row>
            <el-row style="padding: 10px 0 0 0">
              <el-col :span="12" style="width: 380px">
                <search-supplier
                  :size="searchSize"
                  :placeholder="'供应商公司'"
                  :disabled="parmValue.companyNo === ''"
                  :value="parmValue.supplier_code"
                  @searchChange="selectSupplier"
                />
              </el-col>
              <el-col :span="12" style="width: 330px; padding:0 0 0 10px">
                <search-stock
                  :isDetail="true"
                  :size="searchSize"
                  :value="parmValue.stock_code"
                  :disabled="supplier_code === ''"
                  :placeholder="'出库仓库'"
                  :isRelation="true"
                  :companyCode="supplier_code"
                  :names="''"
                  @searchChange="selectStock"
                />
              </el-col>
              <!-- 刷新 -->
              <el-col :span="4" style="width: 66px" class="fr">
                <el-button
                  :size="searchSize"
                  type="primary"
                  label=""
                  @click="searchList"
                  >刷新</el-button
                >
              </el-col>
            </el-row>
          </div>
        </template>
        <template #specinfo="{ scope }">
          <span
            v-for="(si, sii) in scope.row.specinfo"
            :key="sii + si.spec_value_id"
            >{{ sii === 0 ? "" : "-" }}{{ si.spec_name }}[{{
              si.spec_value
            }}]</span
          >
        </template>
        <template #can="{ scope }">
          <span v-for="(si, sii) in scope.row.cat_info" :key="sii + si.id"
            >{{ sii === 0 ? "" : "-" }}{{ si.name }}</span
          >
        </template>
      </ex-table>
    </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/stock/spaceCount";
import columns from "./columns";
import { mapGetters } from "vuex";
export default {
  name: "spaceCount",
  mixins: [mixinPage, resToken],
  components: {},
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockCount"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      stock_code: "",
      supplier_code: "",
      parmValue: {
        supplier_code: [], //供应商编码
        stock_code: [], //仓库编码
        companyNo: "",
        start: "",
        end: "",
        type_code: "", //商品编码
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      loading: true,
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
    };
  },
  mounted() {
    this.searchList();
  },

  methods: {
    //业务公司选择
    async supplierChange(e) {
      const { code } = e;
      this.parmValue.companyNo = code || ""; //业务公司编码
      this.parmValue.supplier_code = [];
      this.supplier_code = "";
      this.parmValue.stock_code = [];
      this.stock_code = "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    // 选择时间操作
    async handleTime(e) {
      this.parmValue.start = e.startTime || "";
      this.parmValue.end = e.endTime || "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    // 选择供应商仓库
    async selectStock(e) {
      const { code } = e;
      this.parmValue.stock_code = code ? [code] : [];
      this.stock_code = code || "";
      this.parmValue.stock_code = [];
      this.stock_code = "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    // 选择供应商
    async selectSupplier(e) {
      const { code } = e;
      this.parmValue.supplier_code = code ? [code] : [];
      this.supplier_code = code || "";
      this.parmValue.stock_code = [];
      this.stock_code = "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        start: "",
        end: "",
        companyNo: "",
        type_code: "", //商品编码
        supplier_code: [],
        stock_code: [], //仓库编码
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },

    async searchList() {
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      model.supplierNo = model.supplier_code.toString() || "";
      model.wsm_code = model.stock_code.toString() || "";
      delete model["supplier_code"];
      delete model["stock_code"];
      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.forEach((e) => {
          e.supplier_wsm_name =
            e.supplier_name === e.wsm_name
              ? e.supplier_name
              : e.supplier_name + "/" + e.wsm_name;
        });

        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;
    },
  },
};
</script>
   