<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" width="1000px" title="选择商品" :visible.sync="visible">
    <div class="proSelectBox">
      <!-- 搜索 -->
      <div class="topSearch">
        <div class="formSearch">
          <el-form :inline="true" :model="formData">
            <el-form-item label="">
              <el-input v-model="formData.keyword" class="inputKeyword" placeholder="店铺名称/商品ID/商品分组" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" plain @click="search">查询</el-button>
              <el-button plain @click="clear">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <!-- 表格 -->
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="tableData"
          max-height="500"
          border
          row-key="productId"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            v-if="isMultiple"
            width="40"
            type="selection"
            :reserve-selection="true"
            fixed="left"
          />
          <el-table-column v-else label="" width="40" align="center">
            <template slot-scope="scope">
              <el-radio v-model="tableRadio" :label="scope.row"><i /></el-radio>
            </template>
          </el-table-column>
          <el-table-column label="产品主图" width="180" align="center">
            <template slot-scope="scope">
              <el-image
                style="width: 80px; height: 80px"
                :src="scope.row.image"
                fit="contain"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="productName"
            label="产品名称"
            width="180"
          />
          <el-table-column
            prop="productId"
            label="产品ID"
          />
          <el-table-column
            prop="price"
            label="售价"
          />
          <el-table-column
            prop="originalPrice"
            label="原价"
          />
          <el-table-column
            prop="stockNumber"
            label="库存"
          />
          <el-table-column
            prop="number"
            label="销量"
          />
        </el-table>
        <div class="fenye">
          <el-pagination
            :current-page="currentPage"
            :hide-on-single-page="true"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="doSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getProducts
} from '@/api/public'
export default {
  name: 'ProductTable',
  props: {
    proId: {
      type: Number,
      default: 0
    },
    proIds: {
      type: Array,
      default: () => []
    },
    isMultiple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: false,
      tableRadio: '',
      formData: {
        keyword: '',
        status: '',
      },
      currentPage: 1,
      total: 0,
      pageSize: 10,
      tableData: [],
      multipleSelection: []
    }
  },
  mounted() {
    this.getTableData()
  },
  methods: {
    // 获取产品信息
    getTableData() {
      var _this = this
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize,
        shelveState: 1
      }
      if (this.formData.keyword) {
        params.search = this.formData.keyword
      }
      getProducts(params).then(res => {
        _this.tableData = res.data.list
        _this.total = res.data.total
        // 表格斌值
        if (this.isMultiple) {
          if (this.proIds.length > 0) {
            this.tableData.forEach(row => {
              for (let i = 0; i < this.proIds.length; i++) {
                if (this.proIds[i] === row.id) {
                  this.$refs.multipleTable.toggleRowSelection(row, true)
                }
              }
            })
          }
        } else {
          if (this.proId !== 0) {
            this.tableRadio = this.proId
            this.tableData.forEach(row => {
              if (this.proId === row.id) {
                this.tableRadio = row
              }
            })
          }
        }
      })
    },
    // 搜索
    search() {
      this.getTableData()
    },
    clear() {
      this.formData.keyword = ''
      this.getTableData()
    },
    // 每页条数改变
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableData()
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.currentPage = val
      this.getTableData()
    },
    // 多选改变
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 提交
    doSubmit() {
      if (this.isMultiple) {
        this.$emit('doSubmit', this.multipleSelection)
      } else {
        this.$emit('doSubmit', this.tableRadio)
      }
      this.visible = false
    }
  }
}
</script>

<style scoped>
.proSelectBox .formSearch .inputKeyword{
  width: 200px;
}
</style>
