<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
      v-loading="listLoading"
      border
      :data="list"
      height="400"
      @selection-change="setSelectRows"
    >
      <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"
        :label="__('商品价格')"
        prop="item_unit_price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="item_enable"
        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="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

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

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

  export default {
    name: 'PlantformActivityEdit',
    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,
        pa_id: '',
        selectRows: [],
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        dialogFormVisible: false,
      }
    },
    created() {
      this.getTree()
    },
    methods: {
      __,
      // 获取规格数据
      async getTree() {
        const { data } = await getTree()
        this.data = data
      },
      showTable(pa_id) {
        this.pa_id = pa_id
        this.fetchData()
        this.dialogFormVisible = true
      },
      formatEnable(row) {
        return row.item_enable === 1001
          ? this.__('正常')
          : row.item_enable === 1002
          ? this.__('下架厂库中')
          : this.__('违规禁售')
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      close() {
        this.list = []
        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()
      },

      async save() {
        if (this.selectRows.length === 0) {
          this.$baseMessage('请选择商品', 'success')
          return
        }
        let param = this.getParam()
        const { msg, status } = await doEdit(param)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        this.$emit('fetch-data')
        this.close()
      },
      getParam() {
        let param = {}
        let selectItem = this.selectRows.map((item) => item.item_id)
        if (selectItem.length > 0) param.item_id = JSON.stringify(selectItem)
        param.pa_id = this.pa_id
        return param
      },
      change(id) {
        this.queryForm.category_id = id
      },
    },
  }
</script>
