<template>
  <d2-container>
    <div v-if="isExpend" class="d2-fl d2-tree-view" >
        <pubilc-tree ref="Tree" :type="type"    @action="action" ></pubilc-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-date-picker
									v-model="time"
									type="datetimerange"
									value-format="yyyy-MM-dd HH:mm:ss"
									:default-time="['00:00:00', '23:59:59']"
									@change="dateChange"
									range-separator="至"
									start-placeholder="开始日期"
									end-placeholder="结束日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="卡号">
					<el-input v-model="filter.cardId" placeholder="卡号" clearable/>
				</el-form-item>
				<el-form-item label="员工姓名">
					<el-input v-model="filter.acctName" placeholder="员工姓名" clearable/>
				</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">
				<div class="d2-table-view-handle">
					<el-button  @click="exportOperation" size="small" type="success"  >导出</el-button>
				</div>
      <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 {getBalance} from '../../../api/cost/meal'
  import {tableOptions} from '../../../assets/options'
	import util from '../../../libs/util'
  export default {
    name: 'attendance-accountCount',
    data () {
      return {
				func:{
					select:false
				},
				type:'equ',
				time: '',
				isExpend:true,
        tableOptions,
        loading:false,
        currentPage:1,      
        systemRoles:[],
        filter:{
					acctName:'',
					cardId:'',
          startTime:'',
          endTime:'',
					depId:'',
          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: 'userId',
            minWidth: '90',
						 showOverflowTooltip: true
          },
          {
            title: '员工姓名',
            key: 'acctName',
            minWidth: '90'
          },
          {
            title: '部门',
            key: 'depNm',
            minWidth: '90'
          },
          {
            title: '卡号',
            key: 'cardCode',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '账号',
            key: 'acctCode',
            minWidth: '90'
          },
          {
            title: '余额',
            key: 'cardBalance',
            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: {
        action(data){
      	//this.falg = false
      	if(data.alias === 'top'){
      		this.filter.depId = ''
      	}else{
      		this.filter.depId = data.id
      	}
      	 this.handleList()
      },
      handleList (val) {
        if(val){this.filter.pageNum=1
        this.currentPage = 1}
      this.loading = true
        getBalance(this.filter).then(res=>{
          this.loading = false
          this.data = res.pageList.list
          this.pagination.total = res.pageList.total
        }).catch(err=>{
          this.loading = false
        })
      },
      //导出
     exportOperation(){
				if (!this.data.length){
				 this.$message({
					 message: '页面无数据,无法导出',
					 type: 'error'
				 })
				 return false
			 }
			 const TOKENID = util.cookies.get('TOKENID')
			 //const TOKENID = '20190607000000ongaognaon'
			 const REQTM = '2019-06-14 18:00:00'
			 //const USERID = util.cookies.get('USERID')
			 window.open(`${process.env.VUE_APP_API}/consumDetail/downBalanceExcel?acctName=${this.filter.acctName}&cardId=${this.filter.cardId}&REQTM=${REQTM}&TOKENID=${TOKENID}`)
     },
      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()
			this.time = this.$setInitTime()
			this.filter.startTime = this.time[0].format('yyyy-MM-dd hh:mm:ss')
			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,102)
      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 === 1 || index === 4 ||index === 5 || index === 2) {
						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)
							} else {
								return prev
							}
						}, 0)
						sums[index] += ''
					} else {
						sums[index] = ''
					}
				})

				return sums
			} */
    }
  }
</script>
