<template>
  <!-- 仓储设备投资建设上报 -->
  <div class="content-main">
    <span class="spanStyle">仓储设备投资建设明细表</span>
    <span class="spanStyleRight">单位：万元、个、把、台</span>
    <el-table :data="tableData" :span-method="objectSpanMethod" stripe border fit highlight-current-row style="width: 100%">
      <el-table-column v-if="orgCode != 'LS001'" align="center" type="index" label="序号" width="70">
      </el-table-column>
	  <el-table-column v-else width="260px" align="center" prop="qyjt" label="区域/集团">
	  </el-table-column>
      <el-table-column width="260px" align="center" prop="kqmc" label="库区名称">
	  </el-table-column>
      <el-table-column align="center" prop="sblx" label="设备类型">
	  </el-table-column>
      <el-table-column align="center" prop="sblb" label="设备类别"></el-table-column>
      <el-table-column align="center" prop="sbmc" label="设备名称">
	  </el-table-column>
      <el-table-column align="center" prop="grsl" label="购入数量">
	  </el-table-column>
      <el-table-column align="center" prop="jrhj" label="金额合计">
	  </el-table-column>
      <el-table-column align="center" prop="zt" label="状态">
	  </el-table-column>
      <el-table-column align="center" prop="qrsj" label="确认时间">
	  </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import { queryCcsbtzjsmxForMx,queryCcsbtzjsmxForMxGld,queryCcsbtzjsmxForMxGldExport
   ,queryCcsbtzjsmxForMxExport} from '@/api/zgsq/ccsbtzmx'
  export default {
    data() {
      return {
        nf: '',
        tableData: [],
		orgCode:sessionStorage.getItem('orgCode'),
		spanArr: [], //每行合并数
		pos:0, //角标索引
      }
    },
    created() {
    },
	mounted() {
	    
	  },
    methods: {
		exportExcel(data){
			if(this.orgCode == 'LS001'){
				queryCcsbtzjsmxForMxGldExport(data).then(res => {
					let blob = new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
					const link = document.createElement('a');
					link.style.display = 'none';
					link.href = URL.createObjectURL(blob);
					link.setAttribute('download', '仓储设备投资建设明细表');
					document.body.appendChild(link);
					link.click();
					document.body.removeChild(link);
				}).catch((err) => {
				    this.$message.error(err);
				});
			}else{
				queryCcsbtzjsmxForMxExport(data).then(res => {
					let blob = new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
					const link = document.createElement('a');
					link.style.display = 'none';
					link.href = URL.createObjectURL(blob);
					link.setAttribute('download', '仓储设备投资建设明细表');
					document.body.appendChild(link);
					link.click();
					document.body.removeChild(link);
				}).catch((err) => {
				    this.$message.error(err);
				});
			}
		  
		},
		getSpanArr(data){
			for (var i = 0; i < data.length; i++) {
				if(i == 0){
					this.spanArr.push(1)
					this.spanArrOne.push(1)
					this.pos = 0
					this.pos1=0
				}else{
					console.log(data[i-1].qyjt)
					if(data[i].qyjt == data[i-1].qyjt){
						 this.spanArr[this.pos] += 1;
						 this.spanArr.push(0); 
					}else{
						 this.spanArr.push(1);
						 this.pos = i;
					}
					if(data[i].kqmc == data[i-1].kqmc){
						 this.spanArrOne[this.pos1] += 1;
						 this.spanArrOne.push(0); 
					}else{
						 this.spanArrOne.push(1);
						 this.pos1 = i;
					}
				}
				
			}
		},
		//合并单元格
		objectSpanMethod({ row, column, rowIndex, columnIndex }){
			if(this.orgCode== 'LS001'){
				if (columnIndex === 0) {
					const _row = this.spanArr[rowIndex];
					const _col = _row > 0 ? 1 : 0; 
					return {
					  rowspan: _row, 
					  colspan: _col, 
					};
				}else if(columnIndex === 1){
					const _row = this.spanArrOne[rowIndex];
					const _col = _row > 0 ? 1 : 0; 
					return {
					  rowspan: _row, 
					  colspan: _col, 
					};
				}
			}
			
		},
      //合计数据
      // GetsummaryMethod(param){
      //   const { columns, data } = param;
      //   const sums = [];
      //   columns.forEach((column, index) => {
      //     if (index === 0) {
      //       sums[index] = '合计';
      //       return;
      //     }
      //     const values = data.map(item => Number(item[column.property]));
      //     if (column.property == 'grsl' || column.property == 'jrhj') {
      //       sums[index] = values.reduce((prev, curr) => {
      //         const value = Number(curr);
      //         if (!isNaN(value)) {
      //           return prev + curr;
      //         } else {
      //           return prev;
      //         }
      //       }, 0);
      //       sums[index] += '';
      //     } else {
      //       sums[index] = '--';
      //     }
      //   });

      //   return sums;
      // },
      // 报表数据
      init(data,type) {
		  if( this.orgCode== 'LS001'){
			 queryCcsbtzjsmxForMxGld(data).then(res =>{
			   if(res.infoCode == 200){
			     this.tableData=res.data.data
				 let arr=[]
				 this.tableData.forEach((item) =>{
					 item[Object.keys(item)].forEach((val) =>{
						val.qyjt=Object.keys(item)[0]
						arr.push(val)
					 })
				 })
				 this.tableData=arr
				 console.log(this.tableData)
				 //清空合并坐标
				 this.spanArr = []; //每行合并数
				 this.pos = 0; //角标索引
				 this.spanArrOne=[];
				 this.pos1 =0
				 this.getSpanArr(this.tableData)
			   }
			 })
		  }else{
			  queryCcsbtzjsmxForMx(data).then(res =>{
			    if(res.infoCode == 200){
			      this.tableData=res.data.data
			      // this.ccsjList=res.data[3].ccsjList
			    }
			  })
		  }
        
        // 模拟数据
        // this.tableData = [{"id":"1","lx":"按设备类型分","grsl":7,"hj":2.4,"children":[{"id":"11","lx":"粮食设备","grsl":3,"hj":1.4},{"id":"12","lx":"仓储器材","grsl":3,"hj":1}]},{"id":"2","lx":"按设备器材类别分","grsl":7,"hj":2.4,"children":[{"id":"22","lx":" 按粮食设备类别分","grsl":3,"hj":2.4,"children":[{"id":"222","lx":" 粮油通用机械设备","grsl":3,"hj":1.4,"children":[{"id":"2222","lx":" 网带出清筛","grsl":2,"hj":1},{"id":"2223","lx":" 圆筒出清筛","grsl":1,"hj":0.4}]}]}]},{"id":"3","lx":"合计","grsl":7,"hj":2.4}];
      },
    }
  }
</script>

<style scoped>
	.recordsDetailsTable{
	    background-color: #fff;
	    font-size: 14px;
	    padding-bottom: 20px;
	    table {
	      width: 100%;
	      margin-top: 10px;
	      font-size: 14px;
	    }
	    .tdsize{
	      font-size: 18px;
	      font-weight: 700;
	    }
	    td,
	    th {
	      text-align: center;
	      border: 1px solid #eaeaea;
	      word-wrap: break-word;
	      height: 40px;
	    }
	
	    span,
	    .el-input,
	    .el-textarea {
	      font-size: 12px;
	    }
	}
  .spanStyle {
    font-size: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .spanStyleRight {
    display: flex;
    justify-content: right;
    align-items: right;
  }
</style>
