<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.product_name"
              clearable
              :placeholder="__('请输入商品名称')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.product_id"
              clearable
              :placeholder="__('请输入产品SPU编码')"
            />
          </el-form-item>
          <el-form-item>
            <tree-select
              ref="treeSelect"
              :checked-keys="defaultCheckedKeys"
              clearable
              :data="data"
              :default-props="defaultProps"
              :height="300"
              :node-key="nodeKey"
              size="small"
              :width="220"
              @change="change"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="fetchData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      ref="tb"
      v-loading="listLoading"
      border
      :data="list"
      height="400"
      @select="setSelectRows"
      @select-all="selectAll"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        :label="__('产品名称')"
        prop="product_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品单价')"
        prop="product_unit_price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatVerify"
        :label="__('商品审核')"
        prop="product_verify_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('商家状态')"
        prop="product_state_id"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="checkItem">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

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

  import { getList } from '@/api/product/base'
  import TreeSelect from '@/components/treeSelect'
  import { getTree } from '@/api/base/product/category'

  export default {
    name: 'ProductItemTable',
    components: { TreeSelect },
    data() {
      return {
        defaultCheckedKeys: [],
        list: [],
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        listLoading: true,
        form: {},
        data: [], // 产品规格数据
        nodeKey: 'id',
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRow: {},
        queryForm: {
          pageNum: 1,
          pageSize: 10,
        },
        dialogFormVisible: false,
        stateOptions: [
          {
            value: 1001,
            label: '正常',
          },
          {
            value: 1002,
            label: '下架仓库中',
          },
          {
            value: 1000,
            label: '违规禁售',
          },
        ],
        verifyOptions: [
          {
            value: 3001,
            label: '审核通过',
          },
          {
            value: 3002,
            label: '审核中',
          },
          {
            value: 3000,
            label: '审核未通过',
          },
        ],
      }
    },
    created() {
      this.getTree()
    },
    methods: {
      formatVerify(row) {
        const obj = this.verifyOptions.find(
          (item) => item.value === row.product_verify_id
        )
        return obj ? obj.label : ''
      },
      formatState(row) {
        const obj = this.stateOptions.find(
          (item) => item.value === row.product_state_id
        )
        return obj ? obj.label : ''
      },
      __,
      // 获取规格数据
      async getTree() {
        const { data } = await getTree()
        this.data = data
      },
      showTable() {
        this.fetchData()
        this.dialogFormVisible = true
      },
      setSelectRows(selection, row) {
        if (selection.length > 1) {
          this.$refs.tb.clearSelection()
          this.$refs.tb.toggleRowSelection(row)
        }
        this.selectRow = row
      },
      selectAll() {
        this.$refs.tb.clearSelection()
        if (this.selectRow) {
          this.$refs.tb.toggleRowSelection(this.selectRow)
        }
      },
      close() {
        this.$refs['treeSelect'].isShowSelect = false
        this.$refs['treeSelect'].selectedData = []
        this.list = []
        this.selectRow = {}
        this.dialogFormVisible = false
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      change(id) {
        this.queryForm.category_id = id
      },
      checkItem() {
        this.$emit('checkItem', this.selectRow)
        this.close()
      },
    },
  }
</script>
