<template>
  <div class="productWeido pagePadding" style="padding: 0">
    <div v-if="powers.some((i) => i == '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" gutter="10">
              <el-col :span="24">
                <el-col :span="5">
                  <el-input
                    v-model="parmValue.type_code"
                    :size="searchSize"
                    maxlength="40"
                    placeholder="sku编码"
                  />
                </el-col>
                <el-col :span="5">
                  <el-input
                    v-model="parmValue.good_name"
                    :size="searchSize"
                    maxlength="40"
                    placeholder="商品名称"
                  />
                </el-col>

                <el-col style="width:400px">
                  <company-select
                    style="width:100%"
                    :value="parmValue.companyNo"
                    @change="handleCompanyChange"
                    size="mini"
                    clearable
                    placeholder="业务公司"
                    :noCompanyCode="true"
                  />
                </el-col>

                <!-- 搜索 -->
                <el-col style="width: 54px">
                  <el-button
                    :size="searchSize"
                    type="primary"
                    class="fr"
                    icon="el-icon-search"
                    label
                    @click="searchList"
                  />
                </el-col>
                <!-- 重置 -->
                <el-col style="width: 56px; 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 #good_code="{ scope }">
          <div style="display:flex;align-items:center">
            <el-tooltip placement="top" content="查看商品详情" v-if="goodCostPremission.includes('007')">
              <i
                @click="linkToGoodDetail(scope.row)"
                class="el-icon-link"
                style="cursor:pointer;margin-right:5px;font-size:16px"
              />
            </el-tooltip>
            <p>{{scope.row.good_code}}</p>
          </div>
        </template>

        <template #expand="{ scope }">
          <el-table :data="scope.row.child" style="width: 100%; paddind: 20px" border size="small">
            <el-table-column label="仓库名称" prop="wsm_name" show-overflow-tooltip min-width="150">
              <template slot-scope="scope">
                <div style="display:flex;align-items:center">
                  <el-tooltip :content="'仓库编码 : ' + scope.row.wsm_code" placement="top">
                    <i class="el-icon-warning-outline" style="margin-right:2px;cursor:pointer" />
                  </el-tooltip>
                  <p>{{scope.row.wsm_name}}</p>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="仓库负责人" prop="contactor_name" show-overflow-tooltip width="120" />
            <!-- <el-table-column
              label="仓库编码"
              prop="wsm_code"
              show-overflow-tooltip
              width="170"
            ></el-table-column>-->
            <el-table-column
              label="仓库所在公司名称"
              prop="supplierName"
              show-overflow-tooltip
              min-width="150"
            >
              <template slot-scope="scope">
                <div style="display:flex;align-items:center">
                  <el-tooltip :content="'仓库所在公司编码 : ' + scope.row.supplierNo" placement="top">
                    <i class="el-icon-warning-outline" style="margin-right:2px;cursor:pointer" />
                  </el-tooltip>
                  <p>{{scope.row.supplierName}}</p>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="供应商负责人" prop="person" show-overflow-tooltip width="120" />
            <!-- <el-table-column
              label="仓库所在公司编码"
              prop="supplierNo"
              show-overflow-tooltip
              width="160"
            ></el-table-column>-->
            <el-table-column label="仓库业务公司名称" prop="company" show-overflow-tooltip min-width="150">
              <template slot-scope="scope">
                <div style="display:flex;align-items:center">
                  <el-tooltip :content="'仓库业务公司名称 : ' + scope.row.companyNo" placement="top">
                    <i class="el-icon-warning-outline" style="margin-right:2px;cursor:pointer" />
                  </el-tooltip>
                  <p>{{scope.row.company}}</p>
                </div>
              </template>
            </el-table-column>
            <!-- <el-table-column
              label="仓库业务公司编码"
              prop="companyNo"
              show-overflow-tooltip
              width="160"
            ></el-table-column>-->
            <el-table-column label="当前库存" width="100" show-overflow-tooltip prop="total_stock"></el-table-column>
            <el-table-column label="待入库" width="100" show-overflow-tooltip prop="wait_in_stock"></el-table-column>
            <el-table-column label="待出库" width="100" show-overflow-tooltip prop="wait_out_stock"></el-table-column>
            <el-table-column label="可用库存" width="100" show-overflow-tooltip prop="usable_stock"></el-table-column>
            <el-table-column label="在途库存" width="100" show-overflow-tooltip prop="intra_stock"></el-table-column>
          </el-table>
        </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_name }}]</span>
        </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 { statusList } from "@/assets/js/statusList";
import asyncRequest from "@/apis/service/stock/productWeido";
import { mapGetters } from "vuex";
import columns from "./columns";
import CompanySelect from "@/layout/components/company-select";

export default {
  name: "productWeido",
  mixins: [mixinPage, resToken],
  components: { CompanySelect },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          item => item.menu_route == "stockCount"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    goodCostPremission() {
      const tran =
        this.$store.getters.btnList.find(
          item => item.menu_route == "goodsCost"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    }
  },
  data() {
    return {
      statusList: statusList,
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      //入参参数
      parmValue: {
        companyNo: "",
        type_code: "", //商品属性编码
        good_name: "", //商品名称
        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: {
    handleCompanyChange(code) {
      this.parmValue.companyNo = code;
      this.searchList();
    },
    linkToGoodDetail(row) {
      // console.log(row)
      // http://localhost:8081/#/goodStore/goodsCostDetail?id=SKU2305311527349138&type=view
      this.$router.push(
        `/goodStore/goodsCostDetail?id=${row.good_code}&type=view`
      );
    },
    //重置参数
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0
      };
      this.parmValue = {
        companyNo: "",
        type_code: "",
        good_name: "", //商品名称
        size: 15,
        curr: 1
      };
      this.searchList();
    },
    //获取列表功能
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.list({
        ...this.parmValue,
        relaComNo: this.parmValue.companyNo,
        useSelfComNo: true
      });

      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>
