<template>
  <el-dialog
    :title="!isShowProd ? '选择商品' : '查看商品'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :inline="true" :model="dataForm" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input v-model="prodName" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="上级分类">
        <el-cascader
          expand-trigger="hover"
          :options="categoryList"
          :props="categoryTreeProps"
          :clearable="true"
          v-model="selectedCategory"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchProd" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="clean" icon="el-icon-delete">清空</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="prodTable"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <!-- <el-table-column v-if="!isShowProd"
                       type="selection"
                       header-align="center"
                       align="center"
                       width="50">
      </el-table-column>-->
      <el-table-column width="50" header-align="center" align="center">
        <template slot-scope="scope">
          <div>
            <el-radio
              :label="scope.row.prodId"
              v-model="prodId"
              @change.native="getSelectProdRow(scope.$index,scope.row)"
            >&nbsp;</el-radio>
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="prodName" header-align="center" align="center" label="产品名称"></el-table-column>
      <el-table-column align="center" width="140" label="产品图片">
        <template slot-scope="scope">
          <img :src="scope.row.pic" width="100" height="100" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <span slot="footer">
      <!-- <el-button type="primary"
                 v-if="isShowProd"
                 size="small"
      @click="visible = false">确定</el-button>-->
      <el-button
        type="primary"
        size="small"
        :disabled="dataListSelections.length <= 0 && isDistribution"
        @click="selectProd()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate, idList } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        product: ''
      },
      isShowProd: 0,
      allData: [],
      prodId: 0,
      discountProds: [],
      discountId: 0,
      prodName: '',
      shopCategoryId: null,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl,
      categoryList: [],
      selectedCategory: [],
      categoryTreeProps: {
        value: 'categoryId',
        label: 'categoryName'
      }
    }
  },
  props: {
    isDistribution: {
      default: false,
      type: Boolean
    }
  },
  methods: {
    // 获取数据列表
    init (id, discountProds) {
      this.prodId = 0
      this.discountProds = discountProds
      this.discountId = id
      this.visible = true
      this.dataListLoading = true
      if (discountProds) {
        this.discountProds.forEach(row => {
          this.dataListSelections.push(row)
        })
      }
      this.getDataList()
      this.getCategoryList()
    },
    getCategoryList () {
      this.$http({
        url: this.$http.adornUrl('/prod/category/listCategory'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.categoryList = treeDataTranslate(data, 'categoryId', 'parentId')
      })
    },
    showProdInit (id, val) {
      this.getCategoryList()
      this.showProd(id, val)
    },
    showProd (id, val) {
      this.isShowProd = val
      this.discountId = id
      this.visible = true
      this.$http({
        url: this.$http.adornUrl(`/admin/discountProd/info/${id}`),
        method: 'get',
        params: this.$http.adornParams({
          current: this.pageIndex,
          size: this.pageSize,
          prodName: this.prodName ? this.prodName : null,
          shopCategoryId: this.shopCategoryId ? this.shopCategoryId : null
        })
      }).then(({ data }) => {
        this.dataList = data.records
        this.totalPage = data.total
      })
    },
    getDataList () {
      this.$http({
        url: this.$http.adornUrl('/prod/prod/pageOnLineProd'),
        method: 'get',
        params: this.$http.adornParams({
          current: this.pageIndex,
          size: this.pageSize,
          prodName: this.prodName ? this.prodName : null,
          shopCategoryId: this.shopCategoryId ? this.shopCategoryId : null,
          status: 1,
          isDistribution: this.isDistribution ? 1 : 0
        })
      }).then(({ data }) => {
        this.dataList = data.records
        this.totalPage = data.total
        this.dataListLoading = false
        this.$nextTick(() => {
          if (this.discountProds) {
            this.discountProds.forEach(row => {
              let index = this.dataList.findIndex(
                prodItem => prodItem.prodId === row.prodId
              )
              this.$refs.prodTable.toggleRowSelection(this.dataList[index])
            })
          }
        })
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    /**
     * 获取分类id
     */
    handleChange (val) {
      this.shopCategoryId = val[val.length - 1]
    },
    /**
     * 根据条件搜索商品
     */
    searchProd () {
      this.pageIndex = 1
      if (this.isDistribution) {
        this.getDataList()
      } else {
        this.showProd(this.discountId, this.discountProds)
      }
    },
    /**
     * 清空搜索条件
     */
    clean () {
      this.prodName = ''
      this.shopCategoryId = null
      this.selectedCategory = idList(this.categoryList, this.shopCategoryId, 'categoryId', 'children').reverse()
    },
    // 选择产品
    selectProd () {
      var prods = []
      this.dataListSelections.forEach(item => {
        let prodIndex = prods.findIndex(prod => prod.prodId === item.prodId)
        if (prodIndex === -1) {
          prods.push({
            discountProdId: 0,
            prodId: item.prodId,
            prodName: item.prodName,
            pic: item.pic
          })
        }
      })
      // var prods = this.dataListSelections.map(item => {
      //   return {discountProdId: 0, prodId: item.prodId, prodName: item.prodName, pic: item.pic}
      // })
      this.$emit('refreshDiscountProds', prods)
      this.dataListSelections = []
      this.visible = false
    },
    // 获取当选的行
    getSelectProdRow (index, row) {
      this.dataListSelections = []
      this.dataListSelections.push(row)
    }
  }
}
</script>
