<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="1%"
    :visible.sync="dialogFormVisible"
    width="70%"
    @close="close"
  >
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="product_item_name"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        align="center"
        :label="__('颜色SKU')"
        prop="color_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatDefault"
        :label="__('是否默认')"
        prop="item_is_default"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('SKU商家编码')"
        prop="item_number"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('条形码')"
        prop="item_barcode"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品价格')"
        prop="item_unit_price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('冻结库存')"
        prop="item_quantity_frozen"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatQuantity"
        :label="__('可用库存')"
        prop="item_quantity"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品库存')"
        prop="item_quantity"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="item_enable"
        show-overflow-tooltip
      />
      <el-table-column align="center" :label="__('批发价格')">
        <template slot-scope="scope">
          <el-button plain type="warning" @click="showWholesaleEdit(scope.row)">
            {{ __('设置') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column v-if="false" align="center" :label="__('价格策略')">
        <template slot-scope="scope">
          <el-button plain type="warning" @click="showLevelEdit(scope.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"
    />
    <Table ref="Table" />
    <TableLeave ref="TableLeave" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList } from '@/api/product/item'
  import { getList as getWholesale } from '@/api/product/pricing/policy/wholesale'
  import { getList as getLevel } from '@/api/product/pricing/policy/level'
  import Table from './PricingPolicyWholesaleTable'
  import TableLeave from './PricingPolicyLevelTable'

  export default {
    name: 'ProductListEdit',
    components: { Table, TableLeave },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        itemUnitPrice: undefined,
        list: [],
        levelList: [],
        wholesaleList: [],
        row: {},
        title: '',
        dialogFormVisible: false,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        levelTotal: 0,
        wholesaleTotal: 0,
        levelQueryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        wholesaleQueryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        enableOptions: [
          {
            value: 1001,
            label: this.__('正常'),
          },
          {
            value: 1002,
            label: this.__('下架仓库中'),
          },
          {
            value: 1000,
            label: this.__('违规禁售'),
          },
        ],
      }
    },
    created() {},
    methods: {
      formatEnable(row) {
        const enable = row.item_enable
        const obj = this.enableOptions.find((item) => item.value === enable)
        return obj ? obj.label : ''
      },
      formatDefault(row) {
        return row.item_is_default ? this.__('是') : this.__('否')
      },
      levelSizeChange(val) {
        this.levelQueryForm.pageSize = val
      },
      levelCurrentChange(val) {
        this.levelQueryForm.pageNum = val
      },
      wholesaleSizeChange(val) {
        this.wholesaleQueryForm.pageSize = val
      },
      wholesaleCurrentChange(val) {
        this.wholesaleQueryForm.pageNum = val
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.getList()
      },
      __,
      async showLevelEdit(row) {
        this.$refs['TableLeave'].showTable(row)
      },
      closeLevel() {
        this.itemUnitPrice = undefined
        this.levelVisible = false
      },
      async showWholesaleEdit(row) {
        this.$refs['Table'].showTable(row)
      },
      closeWholesale() {
        this.wholesaleVisible = false
      },
      async showEdit(row) {
        this.queryForm.product_id = row.product_id
        this.dialogFormVisible = true
        this.getList()
      },
      close() {
        this.dialogFormVisible = false
      },
      async getList() {
        this.listLoading = true

        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      async getWholesale() {
        const { data } = await getWholesale(this.wholesaleQueryForm)
        this.wholesaleList = data.records
        this.wholesaleTotal = data.total
      },
      async getLevel() {
        const { data } = await getLevel(this.levelQueryForm)
        this.levelList = data.records
        this.levelTotal = data.total
      },
      formatQuantity(row) {
        return row.item_quantity - row.item_quantity_frozen
      },
    },
  }
</script>
