<template>
  <div>
    <el-button type="primary" size="small" @click="submit()" plain>导 出</el-button>
  </div>
</template>

<script>
export default {
  props:['tablecols','param','excelTitle','total','specialKey'],
  data () {
    return {}
  },
  methods:{
    async submit () {
      let data = {}
      // 如果传入了total,需要赋值保证数量准确性
      // data = Object.assign({},data,this.param)
      data = JSON.parse(JSON.stringify(this.param))
      if (this.total) {
        data.content.pageSize = this.total
      }
      data.content.isAll = false

      const res = await this.$api.requested(data)
      if (this.specialKey) {
        res.data.forEach(e=>{
          this.specialKey.forEach(s=>{
            e[s.key] = e[s.key]? e[s.key]:[]
            e[s.key] = e[s.key].map(k=>{return s.value?k[s.value]:k}).toString()
          })
        })
      }
      let table = JSON.parse(JSON.stringify(this.tablecols))
      table.forEach((e,index) => {
        // 由于表格数据结构问题这里需要判断一下
        if (e.title === '省市县') {
          table[index].title = '省'
          table.splice(index + 1,0,{title:'市',columnname:'city'},{title:'县',columnname:'county'})
        }else if(e.title === '标签'){
          res.data.forEach(e => {
            if (e.tag.length !== 0){
              e.tag_sys = e.tag_sys + ',' + e.tag
            }else {
              e.tag_sys = e.tag_sys
            }
          })
        }else if(e.title === '项目类型'){
          res.data.forEach(e => {
            e.projecttype = e.projecttype + '-' + e.projecttype_remarks
          })
        } else if (e.title === '授权角色') {
          res.data.forEach(e => {
            e.roleName = e.userrole.map(item => item.remarks).join(',')
          })
          console.log(res.data,'结果');
        }
      });
      let hd = table.map(e=>{
        return e.title
      })
      let ft = table.map(e=>{
        return e.columnname
      })
      this.tool.exportExcel(hd,ft,res.data,this.excelTitle)
    }
  }
}

</script>
<style>
</style>