<template>
  <d2-container>
    <div class="d2-table-view-full" style="overflow: hidden">
      <div class="d2-table-view-header">
        <el-form label-width="70px" labelPosition="center" 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="工号/学号" label-width="90px">
					  <el-input v-model="filter.userNo" clearable/>
					</el-form-item>
          <el-form-item label="人员类型">
            <custom-select :value="filter.userTyp" :isAll="true" v-model="filter.userTyp" typeName="人员类型" style="width: 180px;"/>
          </el-form-item>
          <el-form-item label="卡号">
            <el-input v-model="filter.cardCode" style="width: 180px;" clearable/>
          </el-form-item>
          <el-form-item label="部门">
            <department-tree @clearInput="clearDepartment" v-model="filter.depId" :isClear="isClear"/>
          </el-form-item>
         <el-form-item label="员工姓名" label-width="90px">
            <el-input v-model="filter.userNm" style="width: 180px;" clearable/>
          </el-form-item>
					
					<!-- <el-form-item label="设备号">
						<el-input v-model="filter.eqpNo" clearable style="width: 180px;"/>
					</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"
                :loading="loading"
                :columns="columns"
                :data="data"
                :options="tableOptions">
					<el-button v-if="func.out" slot="headerButton" type="primary" size="small" @click="exportFile()">导出</el-button>			
				</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 { personalConsumStatisticsPage } from '../../../api/cost/report'
  import { tableOptions } from '../../../assets/options'
  import util from '../../../libs/util'

  export default {
    name: 'attendance-cost',
    /* components:{
      departmentTree
    }, */
    data () {
      return {
				func:{
					select:false,
					out:false
				},
        time: '',
        loading: false,
				isClear:'123',
        currentPage:1,
        tableOptions,
        filter: {
          startTime: '',
          endTime: '',
          userTyp:null,
          cardCode: '',
          userNo:'',
          //empName: '',
          pageSize: 10,
          pageNum: 1,
					userNm:'',
					depId:''
					//empCode:''
        },
        pagination: {
          total: 0
        },
        columns: [
          {
            title: '序号',
            key: 'index',
            minWidth: '50',
            formatter (row, column, cellValue, index) {
              return index+1
            }
          },
          {
            title: '工号/学号',
            key: 'userNo',
            minWidth: '100',
						showOverflowTooltip: true
          },
			/* 		{
						title: '工号',
						key: 'empCode',
						minWidth: '100'
					}, */
          {
            title: '姓名',
            key: 'userNm',
            minWidth: '90'
          },
          {
            title: '人员类型',
            key: 'paramName',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '部门/班级',
            key: 'depNm',
            minWidth: '90',
          },
          {
            title: '卡号',
            key: 'cardCode',
            minWidth: '90',
          },
          {
            title: '消费前金额',
            key: 'beforeAmountConsumption',
            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: '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: '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: {
      handleList (val) {
         if(val){this.filter.pageNum=1
        this.currentPage = 1}
      this.loading = true
        personalConsumStatisticsPage(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]
				}
      },
			 exportFile () {
			  if (!this.data.length){
			    this.$message({
			      message: '页面无数据,无法导出',
			      type: 'error'
			    })
			    return false
			  }
			  const token = util.cookies.get('token')
			  window.open(`${process.env.VUE_APP_API}/cons/downConsumeByExcel?startTime=${this.filter.startTime}&endTime=${this.filter.endTime}&consumeType=${this.filter.consumeType || ''}&equipmentCode=${this.filter.equipmentCode}&cardCode=${this.filter.cardCode}&empName=${this.filter.empName}&orgId=${this.filter.orgId}&token=${token}`)
			},
      clearDepartment(){
        this.filter.depId=''
      },
				/* 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
			        }
			      } else if (item.functionType === 'out') {
			        if (item.remarks === 'true') {
			          this.func.out = true
			        } else {
			          this.func.out = false
			        }
			      }  
			    })
			  })
			} */
    },
    mounted () {
			//this.getFunction ()
      this.time = this.$setInitTime()
			this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions,80)
      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 || index === 4 || index === 5 ) {
						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>
