<template>
  <d2-container>
    <div v-if="isExpend" class="d2-fl d2-tree-view" >
      <el-tree highlight-current :data="treeData" default-expand-all :props="defaultProps" @node-click="handleNodeClick" >
				<span class="icon-tree" slot-scope="{ node, data }">
						 			<div v-if="data.alias == 'top'">
						 				<i class="fa fa-university"></i>
						 				<span>&nbsp;{{node.label}}</span>
						 			</div>
						 			<div v-if="data.alias == '4'">
						 				<i class="fa fa-building"></i>
						 				<span>&nbsp;{{node.label}}</span>
						 			</div>
							  <div v-if="data.alias == 'son'">
								<i class="fa fa-user-circle"></i>
								<span>&nbsp;{{node.label}}</span>
							</div>
						 </span>
      </el-tree>
    </div>
   <div @click="isExpend = !isExpend" :class="[{'angle-view':isExpend},{'angle-view-none':!isExpend}]">
   	<i :class="['fa', {'fa-angle-left':isExpend},{'fa-angle-right':!isExpend}]"></i>
   </div>
   <div :class="[{'d2-fl':isExpend},{'d2-table-view':isExpend},{'d2-table-view-full':!isExpend}]" style="overflow: hidden">
      <div class="d2-table-view-header">
      <el-form size="small" :inline="true" :model="filter" @keyup.enter.native="handleList" class="demo-form-inline">
		  <el-form-item label="商户名称">
			  <el-input v-model.trim="filter.orgNm" placeholder="商户名称" clearable/>
		  </el-form-item>
				<el-form-item label="设备号">
					<el-input v-model.trim="filter.eqpNo" placeholder="设备号"/>
				</el-form-item>
        <el-form-item>
          <el-button   type="primary" @click="handleList('1')">查询</el-button>
        </el-form-item>
      </el-form>
      </div>
      <div class="d2-table-view-content">
      <d2-crud
              title="商户消费统计列表"
              ref="d2Crud"
              :options="tableOptions"
              :loading="loading"
              :columns="columns"
              :data="data">
      </d2-crud>
      </div>
      <div class="d2-table-view-footer">
      <el-pagination
      				@current-change="handleCurrentChange"
      				@size-change="handleSizeChange"
					    :current-page.sync="currentPage"
      				:page-size="tableOptions.pageList[0]"
      				:page-sizes="tableOptions.pageList"
      				layout="total,sizes, prev, pager, next,jumper"
      				:total="pagination.total">
      </el-pagination>
      </div>
    </div>
  </d2-container>
</template>

<script>
  import { consumStatisticsPage,TreeNodeMt} from '../../../api/cost/report'
  import {tableOptions} from '../../../assets/options'
  export default {
    name: 'attendance-mtCostCount',
    data () {
      return {
				func:{
					select:false
				},
				time: '',
				isExpend:true,
        tableOptions,
        loading:false,
        currentPage:1,      
        systemRoles:[],
        filter:{
					selectedId:'',
          eqpNo:'',
          orgNm:"",
          pageSize: 10,
          pageNum:1
        },
        pagination:{
          total: 0
        },
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
					{
						title: '序号',
						key: 'index',
						minWidth: '50',
						formatter (row, column, cellValue, index) {
							return index+1
						}
					},
          {
            title: '商户名称',
            key: 'orgNm',
            minWidth: '90'
          },
          {
            title: '设备号',
            key: 'eqpNo',
            minWidth: '90'
          },
          {
            title: '设备名称',
            key: 'eqpNm',
            minWidth: '90'
          },
          {
            title: '消费金额',
            key: 'cardConsume',
            minWidth: '100',
						align: 'right',
						formatter (row, column, cellValue, index) {
							let str = /^\d+$/
							let str_1 =  /^\d+(\.\d)?$/
							if(str.test(cellValue)){
								cellValue=cellValue+".00"
							}
							if(str_1.test(cellValue)){
								cellValue=cellValue+"0"
							}
							return cellValue
						}
          },
          {
            title: '退款金额',
            key: 'refundAmount',
            minWidth: '100',
						align: 'right',
						formatter (row, column, cellValue, index) {
							let str = /^\d+$/
							let str_1 =  /^\d+(\.\d)?$/
							if(str.test(cellValue)){
								cellValue=cellValue+".00"
							}
							if(str_1.test(cellValue)){
								cellValue=cellValue+"0"
							}
							return cellValue
						}
          }
        ],
        data: []
      }
    },
    methods: {
      handleNodeClick(data,node,com) {
        this.filter.selectedId = data.id
        this.handleList()
      },
      handleTreeNode () {
        TreeNodeMt({
          isCompany:0
        }).then(res=>{
          this.treeData = JSON.parse(res.msgDat)
          this.handleList()
        })
      },
      handleList (val) {
         if(val){this.filter.pageNum=1
        this.currentPage = 1}
      this.loading = true
        consumStatisticsPage(this.filter).then(res=>{
          this.loading = false
          this.data = res.pageList.list
          this.pagination.total = res.pageList.total
        }).catch(err=>{
          this.loading = false
        })
      },

      handleCurrentChange (val) {
        this.filter.pageNum = val
        this.handleList()
      },
			handleSizeChange (val) {
			this.filter.pageNum = 1
			this.filter.pageSize = val
			this.handleList()
			},
			dateChange (val) {
			if(val == null){
				this.filter.startTime = ''
				this.filter.endTime = ''
			}else{
				this.filter.startTime = val[0] 
				this.filter.endTime = val[1]
			}
			},
				/* 	getFunction () {
			  this.$GetFunction({
			    menuUrl:this.$route.path
			  }).then(res=>{
			    res.data.forEach(item=>{
			      if (item.functionType === 'select') {
			        if (item.remarks === 'true') {
			          this.func.select = true
			        } else {
			          this.func.select = false
			        }
			      }  
			    })
			  })
			} */
    },
    mounted () {
			//this.getFunction()
			function 	setDate(date){
				y=date.getFullYear();
				m=date.getMonth()+1;
				d=date.getDate();
				m=m<10?"0"+m:m;
				d=d<10?"0"+d:d;
			return y+"-"+m+"-"+d;
			}
			this.time = this.$setInitTime()
			this.filter.startTime = this.$timestampToTime(this.time[0].setDate(1))
			this.filter.endTime = this.time[1].format('yyyy-MM-dd hh:mm:ss')
			this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions,20)
      this.handleTreeNode()
			this.handleList()
			this.tableOptions.showSummary = true
			this.tableOptions.summaryMethod = function (param) {
				const { columns, data } = param
				const sums = []
				columns.forEach((column, index) => {
					if (index === 0) {
						sums[index] = '合计'
						return
					}
					if (index === 2 || index === 3 || index === 1 ) {
						sums[index] = ''
						return
					}
					const values = data.map(item => Number(item[column.property]))
					if (!values.every(value => isNaN(value))) {
						sums[index] = values.reduce((prev, curr) => {
							const value = Number(curr)
							if (!isNaN(value)) {
								let real = this.NP.plus(prev,curr)
								return this.NP.round(real,2)
								//return prev+curr
							} else {
								return prev
							}
						}, 0)
						sums[index] += ''
					} else {
						sums[index] = ''
					}
				})

				return sums
			}
    }
  }
</script>
