<template>
  <el-row class="" :gutter="10">
    <el-col :span="10">
      <el-card>
        <div slot="header" class="header">
          <el-select v-model="queryParams.typeId" placeholder="选择类型" style="width:120px" clearable>
            <el-option v-for="item in typeList" :key="item.id" :label="item.titleZh" :value="item.id" />
          </el-select>
          <el-select v-model="queryParams.attrId" placeholder="选择属性" style="width:120px" clearable>
            <el-option v-for="item in attrList" :key="item.id" :label="item.attrName" :value="item.id" />
          </el-select>
          <el-input v-model="queryParams.titleZh" placeholder="产品关键字" style="width:120px" clearable />
          <el-button type="primary" @click="reLoad">搜搜</el-button>
        </div>
        <div class="list">
          <div class="item" v-for="item in list" :key="item.id">
            <el-checkbox @change="toggleCheck(item, $event)" :value="ids.indexOf(item.id) > -1" /> {{item.titleZh}}
            <div>{{item.titleEn}}</div>
          </div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="10">
      <el-card>
        <div slot="header" class="header">
          已选产品
        </div>
        <div class="list">
          <div class="item" v-for="(choosed) in choosedList" :key="choosed.id" :data-data="JSON.stringify(choosed)">
            <el-link class="el-icon-delete" @click="remove(choosed)" /> {{choosed.titleZh}}
            <div>{{choosed.titleEn}}</div>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>
<script>
import { getProductPage } from '@/api/ecw/product'
import { getProductTypeList } from '@/api/ecw/productType'
import { getProductAttrList } from '@/api/ecw/productAttr'

export default {
  props: {
    defaultIds: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      list: [],
      page: 1,
      pages: 1,
      queryParams: {
        page: 1,
        attrId: null,
        titleZh: null,
        typeId: null
      },
      choosedList: [],
      typeList: [],
      attrList: []
    }
  },
  computed: {
    ids: {
      get() {
        let arr = []
        this.choosedList.forEach(item => {
          arr.push(item.id)
        })
        return arr
      },
      set(v) {

      }
    }
  },
  watch: {
    ids(val) {
      this.$emit('input', val)
    }
  },
  created() {
    getProductTypeList().then(res => this.typeList = res.data)
    getProductAttrList().then(res => this.attrList = res.data)
    this.reLoad()
    this.ids = this.defaultIds //数据回显
  },
  methods: {
    reLoad() {
      this.queryParams.page = 1
      this.list = []
      this.getList()
    },
    loadNextPage() {
      if (this.page >= this.pages) {
        return this.$message.error('已加载全部')
      }
      this.queryParams.page++
      this.getList()
    },
    getList() {
      getProductPage(this.queryParams).then(res => {
        this.list = res.data.list //.concat(res.data.list || [])
        this.page = res.data.page
        this.pages = res.data.pages
        this.choosedList = [] //搜搜重置，数据回显
        if (this.defaultIds.length > 0) {
          this.defaultIds.map(item => {
            this.list.map(items => {
              if (items.id == item) {
                this.choosedList.push(items)
              }
            })
          })
        }
      })
    },
    toggleCheck(item, checked) {
      if (checked) {
        this.choose(item)
      } else {
        this.remove(item)
      }
    },
    choose(item) {
      this.choosedList.push(item)
    },
    remove(item) {
      this.choosedList.forEach((choosed, index) => {
        if (choosed.id == item.id) this.choosedList.splice(index, 1)
      })
    }
  }
}
</script>
<style scoped lang="scss">
.header {
  > div {
    margin-right: 5px;
  }
}
.list {
  height: 200px;
  border: 1px solid #ccc;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 10px;

  display: flex;
  flex-wrap: wrap;
  .item {
    width: 50%;
    line-height: 20px;
    height: 50px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>