<template>
  <div class="warehouse-item-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model="queryForm.warehouse_name"
              clearable
              :placeholder="__('请选择')"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                created
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="product_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('规格')"
        prop="item_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('所属厂库')"
        prop="warehouse_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('单位')"
        prop="unit_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('成本')"
        prop="warehouse_item_cost"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('数量')"
        prop="warehouse_item_quantity"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('库存下限')"
        prop="warehouse_item_quantity_min"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('库磋上限')"
        prop="warehouse_item_quantity_max"
        show-overflow-tooltip
      />
      <el-table-column align="center" :label="__('详情')" width="100">
        <template #default="{ row }">
          <el-button type="text" @click="handleparticulars(row)">
            {{ __('查看') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <Table ref="Table" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList } from '@/api/warehouse/base'
  import Edit from './components/WarehouseItemEdit'
  import Table from './components/StockBillTable'

  export default {
    name: 'WarehouseItem',
    components: { Edit, Table },
    data() {
      return {
        options: [
          {
            value: '默认',
            label: this.__('默认'),
          },
          {
            value: '西郊',
            label: this.__('西郊'),
          },
        ],
        value: '',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },

      handleparticulars(row) {
        console.log(row)
        this.$refs['Table'].handleparticulars(row)
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
