<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    top="25px"
    :visible.sync="isVisible"
    class="goods-dialog"
    @opened="open"
  >
    <div class="product-page">
      <div class="toolbar">
        <el-form :inline="true" :model="formParams">
          <el-form-item>
            <el-input
              v-model="formParams.keyword"
              size="mini"
              placeholder="店铺名称/商品ID/商品分组"
            />
          </el-form-item>
          <el-form-item label="上架状态">
            <el-select v-model="formParams.status" size="mini">
              <el-option label="全部" value="" />
              <el-option label="上架" :value="1" />
              <el-option label="下架" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="官方分类">
            <el-cascader
              v-model="formParams.categoryId"
              class="category-select"
              size="mini"
              :options="categoryList"
              clearable
              :props="{
                checkStrictly: true,
                expandTrigger: 'hover',
                label: 'categoryName',
                value: 'id',
                children: 'childs'
              }"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="fetch">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="product-content">
        <el-table
          ref="multipleTable"
          :data="tableData.adminProductVOList"
          style="width: 100%"
          :row-key="getRowKeys"
          @selection-change="handleSelectionChange"
        >
          <el-table-column v-if="radioState" align="center">
            <template slot-scope="scope">
              <el-radio
                v-model="radioId"
                :label="scope.row.id"
                size="medium"
                class="textRadio"
                @change.native="getCurrentRow(scope.row)"
              >&nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column
            v-else
            :reserve-selection="true"
            type="selection"
            width="55"
          />
          <el-table-column label="产品主图">
            <template slot-scope="scope">
              <img
                class="product-img"
                height="80"
                width="80"
                :src="scope.row.productImg"
              />
            </template>
          </el-table-column>
          <el-table-column prop="productName" label="产品名称" />
          <el-table-column
            prop="id"
            width="200"
            class="text-overflow"
            label="产品ID"
          />
          <el-table-column prop="price" label="售价" />
          <el-table-column prop="applyPrice" label="原价" />
          <el-table-column prop="stock" label="库存" />
          <el-table-column prop="sellCount" label="销量" />
        </el-table>
      </div>
      <pagination
        v-show="tableData.total > 0"
        :limit.sync="formParams.pageSize"
        :page.sync="formParams.pageIndex"
        :total="Number(tableData.total)"
        @pagination="fetch"
      />
    </div>
    <div slot="footer" class="add-btn-wrap">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import Goods from '@/api/Goods'
import Pagination from '@/components/Pagination'
export default {
  components: {
    Pagination
  },
  props: {
    exclude: {
      type: Array,
      default() {
        return []
      }
    },
    dialogVisible: {
      type: Boolean,
      default: false
    },
    radioState: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formParams: {
        categoryId: '',
        hasStock: '',
        keyword: '',
        pageSize: 10,
        pageIndex: 1,
        status: ''
      },
      tableData: [],
      categoryList: [],
      multipleSelectionMap: [],
      select_orderId: [],
      select_order_number: 0,
      selectOneGoods: {},
      radioId: null,
      getRowKeys(row) {
        return row.id
      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    },
    title() {
      return '选择商品'
    }
  },
  watch: {
    exclude() {
      this.select_orderId = []
      this.multipleSelection = []
    }
  },
  methods: {
    getCurrentRow(row) {
      this.radioId = row.id
      this.selectOneGoods = {
        id: row.id,
        productName: row.productName
      }
    },
    // 复选框
    handleSelectionChange(rows) {
      this.multipleSelection = rows
      this.select_order_number = this.multipleSelection.length
      this.select_orderId = []
      if (rows) {
        rows.forEach(row => {
          if (row && row.id) {
            const isHas = this.select_orderId.some(i => i.id === row.id)
            if (!isHas) {
              this.select_orderId.push({
                productName: row.productName,
                id: row.id
              })
            }
          }
        })
      }
    },
    async getProductList() {
      const { categoryId } = this.formParams
      if (typeof categoryId === 'object') {
        this.formParams.categoryId = categoryId.splice(-1, 1)[0]
      }
      const res = await Goods.findAdminProductList(this.formParams)
      const resData = res.data
      const { code } = resData
      if (code === 0) {
        this.tableData = resData.data
        this.formatData()
      }
    },
    async queryAllCategory() {
      const res = await Goods.queryAllCategory()
      const resData = res.data
      this.categoryList = resData.data
    },
    fetch(config) {
      const { limit, page } = config
      this.formParams.pageIndex = page || 1
      this.formParams.pageSize = limit || 10
      this.getProductList()
    },
    editClose() {
      this.dialog.isVisible = false
    },
    onSubmit() {
      if (this.radioState) {
        this.$emit('success', [this.selectOneGoods])
      } else {
        this.$emit('success', this.select_orderId)
      }
      this.close()
    },
    close() {
      this.$emit('close')
    },
    open() {
      this.getProductList()
      this.queryAllCategory()
    },
    formatData() {
      if (this.radioState) {
        this.tableData.adminProductVOList.map(item => {
          if (this.radioId === item.id) {
            this.selectOneGoods = {
              id: item.id,
              productName: item.productName
            }
          }
        })
      } else {
        const excludeIds = this.exclude.map(i => i.id)
        this.tableData.adminProductVOList.map(item => {
          if (excludeIds.indexOf(item.id) > 0) {
            this.$refs.multipleTable &&
              this.$refs.multipleTable.toggleRowSelection(item, true)
          } else {
            this.$refs.multipleTable &&
              this.$refs.multipleTable.toggleRowSelection(item, false)
          }
        })
      }
    },
    reset() {
      this.radioId = null
      this.formParams = {
        categoryId: '',
        hasStock: '',
        keyword: '',
        pageSize: 10,
        pageIndex: 1,
        status: ''
      }
    }
  }
}
</script>
<style lang="less">
.add-dialog-component {
  .form-inline {
    display: inline-block;
    margin: auto;
  }
  .tree-box {
    .el-tree-node__content {
      margin-bottom: 15px;
      height: auto;
    }
  }
}
</style>
<style lang="less" scoped>
.goods-dialog {
  /deep/ .el-dialog {
    display: flex;
    flex-direction: column;
    max-height: 90vh;
    min-width: 1000px;
    overflow: hidden;
    .el-dialog__body {
      flex: 1;
      overflow: auto;
    }
  }
  .dialog-from {
    width: 80%;
    margin: auto;
  }
  .btn-wrap {
    margin: 45px auto 0;
    text-align: right;
  }
  .form-item-row {
    padding-top: 40px;
    /deep/ .el-input {
      width: 100px;
      margin: 0 5px;
    }
  }
}
.product-page {
  padding: 15px 20px;
  .toolbar {
    margin-bottom: 25px;
    .product-name {
      width: 180px;
    }
  }
}
</style>
