<template>
    <div>
        <div style="border-bottom: 1px solid #e9e9e9;padding-bottom:6px;margin-bottom:6px;">
            <Checkbox size="large"
                :indeterminate="indeterminate"
                :value="checkAll"
                @click.prevent.native="handleCheckAll">全选</Checkbox>
        </div>
        <CheckboxGroup v-model="checkAllGroup" size="large" @on-change="checkAllGroupChange">
            <Checkbox v-for="(item,index) in items" :key="item.colunmnName" :label="item.colunmnName">
                <span>{{item.colunmnContent}}</span>
            </Checkbox>
        </CheckboxGroup>
    </div>
</template>

<script>

import miment from "miment";
import Server from "./server.js";
const server = new Server();
export default {
  name: "exportPage",
  components: {
  },
  props: {
    title: {
      type: String,
      default: null
    },
    businessType: {
          type: String,
          default: null
    }
  },
  data() {
    return {
      server: server,
      indeterminate: false,
      checkAll: false,
      checkAllGroup: [],
      params:{businessType:this.businessType},
      items: [],
      resData:null
    };
  },
  created() {
    this.$emit("SetTitle", this.title);
    //加载导出列
    this.server.queryExportColunmns(this.params).then(response => {
            if (response.statusCode === "-1") {
              this.$Message.error(response.msg);
              return;
            }else{
               if(response.data){
                  this.items=response.data;
                  this.handleCheckAll();//默认全选
               }
            }
     });
    //当前页 “关闭”按钮配置
    this.$emit("SetPageActions", [
      {
              text: "确认",
              theme: "warning",
              icon: "edit",
              action: "save",
              handle: this.exportExcel
          },
      {
        text: "取消",
        theme: "primary",
        handle:()=>{
            this.$emit("Close");
        }
      }
    ]);
    this.$emit("SetPageSize", 500);
  },
  beforeMount: function() {},
  mounted: function() {},
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
  //导出
  async exportExcel() {
            if(this.checkAllGroup.length<2){
                this.$Message.error("至少选择2列导出项");
                return;
            }
            //拼装选中对象数组
            let selectData=[];
            for(var index in this.checkAllGroup){
                for(var sub in this.items){
                    //选中值附加都数组中
                    if(this.checkAllGroup[index]==this.items[sub].colunmnName){
                    selectData=selectData.concat(this.items[sub]);
                    }
                }
            }
            //返回上级组件
            this.resData={selectColunms:this.checkAllGroup,selectData:selectData}
            this.$emit("Close", this.resData);
  },
  handleCheckAll () {
          if (this.indeterminate) {
              this.checkAll = false;
          } else {
              this.checkAll = !this.checkAll;
          }
          this.indeterminate = false;

          if (this.checkAll) {
              this.checkAllGroup = [];
              for(var arr in this.items){
                this.checkAllGroup=this.checkAllGroup.concat(this.items[arr].colunmnName);
              }
          } else {
              this.checkAllGroup = [];
          }
      },
      checkAllGroupChange (data) {
          if (data.length === 3) {
              this.indeterminate = false;
              this.checkAll = true;
          } else if (data.length > 0) {
              this.indeterminate = true;
              this.checkAll = false;
          } else {
              this.indeterminate = false;
              this.checkAll = false;
          }
      }

  },
  watch: {},
  directives: {}
};
</script>
