<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-right-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-right-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="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="product_item_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('产品SPU')"
        prop="product_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :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/item'
  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,
        queryForm: {
          pageNum: 1,
          pageSize: 10,
        },
        dialogFormVisible: false,
        status: [{}],
      }
    },
    created() {
      this.getTree()
    },
    methods: {
      __,
      // 获取规格数据
      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)
        }
      },
      formatEnable(row) {
        return row.product_state_id === 1001
          ? this.__('正常')
          : row.product_state_id === 1002
          ? this.__('下架厂库中')
          : row.product_state_id === 1003
          ? this.__('待审核')
          : this.__('违规禁售')
      },
      close() {
        this.$refs['treeSelect'].isShowSelect = false
        this.$refs['treeSelect'].selectedData = []
        this.total = 0
        this.queryForm = {
          pageNum: 1,
          pageSize: 10,
        }
        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()
      },
      getParam() {
        let selectItem = this.selectRows.map((item) => item.item_id)
        let param = {
          item_id: JSON.stringify(selectItem),
          activity_id: this.activity_id,
        }
        return param
      },
      change(id) {
        this.queryForm.category_id = id
      },
      checkItem() {
        let state = this.selectRow.product_state_id
        if (state !== 1001) {
          let msg =
            state === 1002
              ? this.__('下架厂库中')
              : state === 1003
              ? this.__('待审核')
              : this.__('违规禁售')
          this.$baseMessage(msg, 'error')
          return
        }

        this.$emit('checkItem', this.selectRow)
        this.close()
      },
    },
  }
</script>
