<template>
  <div class="dataWeido 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%">
            <el-row style="padding: 0 0 0 80px">
              <el-col :span="24">
                <el-col :span="12" style="width: 240px">
                  <search-work-company
                    :value="parmValue.companyNo"
                    :names="''"
                    :size="searchSize"
                    :is-detail="id !== 'add'"
                    :placeholder="'业务公司'"
                    @searchChange="supplierChange"
                  />
                </el-col>
                <el-col :span="12" style="width: 300px; padding: 0 0 0 10px">
                  <search-supplier
                    :size="searchSize"
                    :placeholder="'供应商公司'"
                    :disabled="parmValue.companyNo === ''"
                    :value="parmValue.supplier_code"
                    @searchChange="selectSupplier"
                  />
                </el-col>
                <el-col :span="12" style="width: 200px; padding-left: 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: 54px">
                  <el-button
                    :size="searchSize"
                    type="primary"
                    class="fr"
                    icon="el-icon-search"
                    label=""
                    @click="searchList"
                  ></el-button>
                </el-col>
                <!-- 重置 -->
                <el-col
                  :span="4"
                  style="width: 54px; margin-left: 10px"
                  class="mr5"
                >
                  <el-button
                    :size="searchSize"
                    class="fr"
                    type="warning"
                    @click="restSearch"
                    >重置</el-button
                  >
                </el-col>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #expand="{ scope }">
          <el-table
            :data="scope.row.child"
            style="width: 100%; paddind: 20px"
            border
            size="small"
          >
            <el-table-column
              label="商品编码"
              prop="spuCode"
              width="160"
            ></el-table-column>

            <el-table-column
              label="商品名称"
              prop="good_name"
              min-width="120"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              label="规格"
              prop="attribute"
              show-overflow-tooltip
              min-width="120"
            >
              <template slot-scope="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>
            </el-table-column>
            <el-table-column label="分类" min-width="120" show-overflow-tooltip>
              <template slot-scope="scope">
                <span
                  v-for="(si, sii) in scope.row.can"
                  :key="sii + si.spec_value_id"
                  >{{ sii === 0 ? "" : "_" }}{{ si.name }}</span
                >
              </template></el-table-column
            >
            <el-table-column
              label="品牌"
              prop="brand_name"
              show-overflow-tooltip
              width="100"
            ></el-table-column>
            <el-table-column
              label="单位"
              prop="unit"
              show-overflow-tooltip
              width="80"
            ></el-table-column>
            <el-table-column
              label="当前库存"
              show-overflow-tooltip
              prop="total_stock"
              width="80"
            ></el-table-column>
            <el-table-column
              label="待入库存"
              show-overflow-tooltip
              prop="wait_in_stock"
              width="80"
            ></el-table-column>
            <el-table-column
              label="待出库存"
              show-overflow-tooltip
              prop="wait_out_stock"
              width="80"
            ></el-table-column>
            <el-table-column
              label="可用库存"
              show-overflow-tooltip
              prop="usable_stock"
              width="80"
            ></el-table-column>
            <el-table-column
              label="在途库存"
              show-overflow-tooltip
              prop="intra_stock"
              width="80"
            ></el-table-column>
          </el-table>
        </template>
      </ex-table>
      <add-edit
        :id="modelId"
        :sitem="sitem"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      />
    </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/dataWeido";
import columns from "./columns";
import { mapGetters } from "vuex";

export default {
  name: "dataWeido",
  mixins: [mixinPage, resToken],

  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 {
      loading: false,
      stock_code: "",
      supplier_code: "",
      parmValue: {
        companyNo: "",
        supplier_code: [],
        stock_code: [],
        page: 1, // 页码
        size: 15, // 每页显示条数
      },

      // 表格 - 数据
      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 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 = {
        companyNo: "",
        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.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>

   