<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="充值类型">
            <custom-select :value="filter.kind" :isAll="true" v-model="filter.kind" 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.empName" style="width: 180px;"/>
          </el-form-item> -->
					<!-- <el-form-item label="员工工号">
					  <el-input v-model="filter.userNo" clearable/>
					</el-form-item> -->
					<!-- <el-form-item label="设备号">
						<el-input v-model="filter.equipmentCode" clearable style="width: 180px;" 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">
        <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 { getRechargeRecord } from '../../../api/cost/report'
  import { tableOptions } from '../../../assets/options'
 // import departmentTree from '../../../components/department-tree/index'
  import util from '../../../libs/util'
  export default {
    name: 'attendance-insert',
    /* components:{
      departmentTree
    }, */
    data () {
      return {
				func:{
					select:false
				},
				isClear:'123',
        time: [],
        loading: false,
        tableOptions,
        currentPage:1,
        filter: {
          startTime: '',
          depId:'',
          endTime: '',
          kind:null,
          cardCode: '',
					userNo:'',
          pageSize: 10,
          pageNum: 1,
        },
        pagination: {
          total: 0
        },
        columns: [
          {
            title: '序号',
            key: 'index',
            minWidth: '50',
            formatter (row, column, cellValue, index) {
              return index+1
            }
          },
          {
            title: '卡号',
            key: 'cardCode',
            minWidth: '100'
          },
					/* {
						title: '工号',
						key: 'userNo',
						minWidth: '100'
					}, */
          {
            title: '姓名',
            key: 'userNm',
            minWidth: '80'
          },
          {
            title: '部门',
            key: 'depNm',
            minWidth: '110',
            showOverflowTooltip: true
          },
          {
            title: '设备号',
            key: 'eqpNo',
            minWidth: '70',
          },
          {
            title: '充值时间',
            key: 'opDate',
            minWidth: '130',
            showOverflowTooltip: true
          },
          {
            title: '卡次',
            key: 'addCardCount',
            minWidth: '70',
          },
					{
						title: '充值类型',
						key: 'paramName',
						minWidth: '90',
					},
          {
            title: '交易前金额',
            key: 'beforeAmountRecharge',
            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: 'chargeMoney',
            minWidth: '90',
						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: '90',
						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
        getRechargeRecord(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]
				}
      },
      clearDepartment(){
        this.filter.depId=''
      },
		/* 	 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/downAddmoneyByExcel?startTime=${this.filter.startTime}&endTime=${this.filter.endTime}&addType=${this.filter.addType || ''}&equipmentCode=${this.filter.equipmentCode}&cardCode=${this.filter.cardCode}&empName=${this.filter.empName}&orgId=${this.filter.orgId}&token=${token}`)
			}, */
			/* 	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.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,70)
      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 ||index === 6||index === 7) {
						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>
