<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="1100px"
    @close="close"
  >
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="500"
      width="60%"
    >
      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('规格')"
        prop="item_name"
        show-overflow-tooltip
        width="400"
      />
      <el-table-column
        align="center"
        :label="__('冻结库存')"
        prop="item_quantity_frozen"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('可用库存')"
        prop="item_quantity"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatQuantity"
        :label="__('商品库存')"
        prop="item_quantity"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('商品状态')"
        prop="item_enable"
        show-overflow-tooltip
        width="150px"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList } from '@/api/product/item'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ProductItemTable',
    components: {},
    data() {
      return {
        list: [],
        total: 0,
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {},
    methods: {
      __,
      formatEnable(row) {
        return row.product_state_id === 1001
          ? this.__('销售中')
          : row.product_state_id === 1002
          ? this.__('仓库中')
          : this.__('违规禁售')
      },
      showTable(row) {
        this.queryForm.product_id = row.product_id
        this.dialogFormVisible = true
        this.fetchData()
      },
      close() {
        this.queryForm = this.$options.data().queryForm
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      formatDatetime(row) {
        return renderTime(row.ach_datetime)
      },
      formatQuantity(row) {
        return row.item_quantity + row.item_quantity_frozen
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
