//错误日志
import React from 'react';
import {
    Card, Row, Col, Button, Pagination, Table,DatePicker ,Input
} from 'antd';
import { $http } from '../../containers/config/https';
import '../../assets/css/App.css'
import PubilcTablePagination from 'pages/common/PubilcTablePagination';
import store from '../../store';
import moment from 'moment'
import 'moment/locale/zh-cn';
moment.locale('zh-cn');
import { $loadTemplate } from'../lodop/Common/Print.js';

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}


class ErrorLog extends React.Component {
    constructor() {
        super();
        // 表格的表头数据
		this.columns = [
			{
                key: 'ErrDate',
                title: '日期',
                dataIndex: 'ErrDate',
                width: "100px",
                align: 'center'
            },{
                key: 'ErrTime',
                title: '时间',
                dataIndex: 'ErrTime',
                width: "100px",
                align: 'center'
            },{
                key: 'AppCode',
                title: '功能模块代码',
                dataIndex: 'AppCode',
                width: "100px",
                align: 'center'
            },{
                key: 'ErrValue',
                title: '错误信息',
                dataIndex: 'ErrValue',
                width: "600px",
                align: 'center'
            },{
                key: 'ErrInfo',
                title: '错误信息来源',
                dataIndex: 'ErrInfo',
                width: "1800px",
                align: 'center'
            }];
        this.state = {
            flag: '',
            title: '',
            userInfo: {},
            // 主菜单列表数据
            data: [],
            startDate: getNowFormatDate(),
            endDate: getNowFormatDate(),
            total: 0,
            page: 1,
            pageSize: 10,
            columns:this.columns,
            code:'',
        };
    }
    componentDidMount() {
		// 初始化列表数据
		this.handleQuery()
    }
    // 提供修改page和pageSize的回调函数
    compilePage(page, pageSize) {
        // 修改page，或者pageSize
        this.setState({page: page, pageSize: pageSize}, () => {
            // 重新调用查询方法
            this.handleQuery()
        })
    }

    // 页码改变的回调，参数是改变后的页码及每页条数
    PageChange = (page, pageSize)=> {
        this.setState({page: page, pageSize: pageSize}, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }

    // 条数改变的回调
    onShowSizeChange = (current, size) => {
    this.setState({page: current, pageSize: size}, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }

    onChangeStartDate = (date, dateString)=> {
        this.setState({
			startDate: dateString,
        })
    }

    onChangeEndDate = (date, dateString)=>{
        this.setState({
			endDate: dateString,
        })
    }
    
    // 查询
	handleAllQuery = () => {
        this.setState({
            page:1,
        },()=>{
            this.handleQuery();
        })
    }
    // 查询
	handleQuery = () => {
        let data = {}
        data = {
            params: [{
                startDate: this.state.startDate,
                endDate: this.state.endDate,
                code:this.state.code,
            }],
            pagination:[{
                pageSize: this.state.pageSize,
                currentPage: this.state.page,
                sort: "ID",
                dir: "desc"
            }]
        }
		$http.post('urlS', this, {
			code: '02010003',
			data: data,
			success: function (res) {
				if (+res.errorCode === 0 && res.result) {
					for(var i = 0; i < res.result.rows.length; i++) {
						res.result.rows[i].key = i
                    }
					this.setState({data: res.result.rows, total: res.result.total})
				} else {
                    
                }
                // 数据加载成功关闭loading
				this.child.handleLoading();
			}
		})
    }

    //打印测试
    printTest =() => {
        //字段赋值
        LODOP.SET_PRINT_PAGESIZE(2, 0, 0,"A6");
        LODOP.SET_PRINT_STYLEA("patName","CONTENT","李刚S");
        LODOP.SET_PRINT_STYLEA("patSex","CONTENT","妖");
        LODOP.SET_PRINT_STYLEA("patAge","CONTENT","38岁");
        LODOP.SET_PRINT_STYLEA("wow","CONTENT","200kg");
        LODOP.PRINT();
    }

    // 显示分页总条数
    showTotal(total) {
        return `共 ${total} 条`;
    }

    onRef = (ref) => {
        this.child = ref
    }

    //处理多个input的公共方法
    onChangeInput(name, e){
        this.setState({
            [name]: e.target.value
        },()=>{
            
        })
        if (e.target.value==="") {
            this.handleAllQuery();
        }
    }

    render() {
        let {columns, data, total, page} = this.state;
        // 组件所需参数
		const param = {
			// 表头配置
			columns,
			// 表格数据
			data: data,
			// 数据总条数
			total,
			// 当前页数
			page,
			// 表格的宽度
			x: '2700px',
			y: store.getState().tableHeight.y + 70 + 'px',
			height: store.getState().tableHeight.y + 80 + 'px',
        }
        return (
            <div>
                <Row>
                    <Card  
                        size="small"
                        title="错误日志查询"
                        headStyle={{background: 'rgba(100,169,130,1)'}}
                    >
                        <Row>
                            开始日期：<DatePicker onChange={this.onChangeStartDate}  format="YYYY-MM-DD" defaultValue={moment(new Date(), 'YYYY-MM-DD')} style={{width: '150px', marginRight: '20px'}}/>
                            截止日期：<DatePicker onChange={this.onChangeEndDate}  format="YYYY-MM-DD" defaultValue={moment(new Date(), 'YYYY-MM-DD')} style={{width: '150px', marginRight: '20px'}}/>
                            模块代码：<Input allowClear={true} onChange={this.onChangeInput.bind(this,"code")} style={{width: '150px'}} onPressEnter={() => this.handleAllQuery()}
                            />
                            <Button style={{ marginLeft: '40px', background: 'rgba(100,169,130,1)',color:'#ffffff'}}  onClick={this.handleAllQuery}>查询</Button>
                        </Row>
                        <Row style={{marginTop:'8px',overflow: 'auto'}}>
                            <div style={{height: store.getState().tableHeight.y + 120  + 'px', overflow: 'auto'}}>
                                <PubilcTablePagination param={param} handelThis={this} onRef={this.onRef} />
                            </div>
                        </Row>
                    </Card>
                </Row>
            </div>
        )
    }
}export default ErrorLog;