<template>
  <div style="padding: 0 40px;">
    <div class="selectClass">
      <Checkbox v-model="allSelect" @on-change="isSelectAllCompany">全选</Checkbox>
      <i-input icon="search" placeholder="请输入零售公司名称或编码" style="width: 250px"></i-input>
    </div>
    <div class="companyTable">
      <div class="companyListClass">
          <div :class="isSelect[index] == true ? 'tagColorClass' : ''" class="tagClass" @click="selectTag(item,index)" v-for="(item, index) in companyList" :key="index" >{{item.label}}</div>
      </div>
    </div>
  </div>
</template>
<script>
import ActivityManager from "../../services/ActivityManager/ActivityManager"
import store from "../../store/index"
export default {
  name: "useActivityStepTwo",
  data() {
		return {
      allSelect: false,
      companyList: [],
      isSelect:[],
      selectValue: [],
      companyListdata:[]
		}
  },
	mounted() {
    this.getCompanyInfo()
  },
  methods: {
    getCompanyInfo() {
      let that = this
      let data = {
        userId: JSON.parse(sessionStorage.getItem("loginInfo")).userId
      }
			ActivityManager.getCompany(data, function (data) {
          that.companyList = []
          that.companyListdata = data.data.results
          data.data.results.forEach(element => {
            that.isSelect.push('')
            that.selectValue.push('')
            that.companyList.push({
              label: element.name,
              value: element.id
            })
          });
			})
    },
    isSelectAllCompany() {
      if(this.allSelect === true) {
        this.isSelect.forEach((element,index) => {
          this.isSelect.splice(index,1,true)
        })
        this.companyList.forEach((element,index) => {
          this.selectValue.splice(index,1,element.id)
        })
      } else {
        this.isSelect.forEach((element,index) => {
          this.isSelect.splice(index,1,'')
        })
        this.companyList.forEach((element,index) => {
          this.selectValue.splice(index,1,'')
        })
      }
    },
    selectTag(value, index) {
      this.isSelect.splice(index,1,!this.isSelect[index])
      if(this.isSelect[index] === true) {
        this.selectValue.splice(index, 1, value.value)
      } else if(this.isSelect[index] === false) {
        this.selectValue.splice(index, 1, '')
      }
      let selectCompanyValue = []
      this.isSelect.forEach((element,index) => {
        if(element === true) {
          selectCompanyValue.push(this.selectValue[index])
        }
      })
      let selectCompanyValueObject = []
      for(let j=0; j< selectCompanyValue.length; j++) {
        for(let i = 0; i< this.companyList.length; i++){
          if(this.companyList[i].value === selectCompanyValue[j]) {
            selectCompanyValueObject.push(this.companyListdata[i])
          }
        }
      }
      store.getters.useData.selectCompanyValue = selectCompanyValueObject
    }
  }
}
</script>

<style  scoped>
.selectClass {
  display: flex;
  justify-content: space-between;
}
.companyTable {
  width: 3150px;
  height: 1000px;
  border: 1px solid;
  margin-top: 40px;
  padding: 40px;
  overflow:scroll;
}
.companyListClass {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.tagClass {
  width: 31%;
  margin-bottom: 30px;
  height: 120px;
  line-height: 120px;
  border: 1px solid #e3e8ee!important;
  color: #657180!important;
  background: #fff;
  padding: 0 12px;
}
.tagColorClass {
  background: #6ac1d1;
}
</style>