// 主菜单维护
import React from 'react';
import '../../assets/css/system.css';
import '../../assets/css/App.css';
import store from '../../store';
import {
    Table, Input, Row, Col, Button, Pagination, Spin 
  } from 'antd';
import { $http } from '../../containers/config/https';
import MenuDiscountConfigUpdata from './MenuDiscountConfigUpdata'

// 主菜单弹框
export default  class MenuDiscountConfigment extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            ListData:{},
            total: 0,
            page: 1,
            pageSize: 10,
            flag: '',
            title: '',
            // 是否显示loading
            showLoading: true,
            // 表格loading
            loading: true,
            selectrowid:'',
            divSubdisplay:'',
            divmentdisplay:'',
            onlytype:'',
        };
        this.columns = [
            {
                title: '序号',
                dataIndex: 'ID',
                width: 40,
                align: "center",
                fixed: 'left',
            },
            {
                title: '医院',
                dataIndex: 'HospDesc',
                width: 150,
                align: "center",
                fixed: 'left',
            },
            {
                title: '就诊类型',
                dataIndex: 'AdmTypeDesc',
                width: 60,
                align: "center"
            },
            {
                title: '费别',
                dataIndex: 'AdmReason',
                width: 90,
                align: "center"
            },
            {
                title: '折扣条件',
                dataIndex: 'DisCondition',
                width: 100,
                align: "center"
            },
            {
                title: '挂号优惠类型',
                dataIndex: 'RegPre',
                width: 100,
                align: "center"
            },
            {
                title: '优先级',
                dataIndex: 'PrioRity',
                width: 100,
                align: "center"
            },
            {
                title: '生效日期',
                dataIndex: 'StDate',
                width: 120,
                align: "center"
            },
            {
                title: '失效日期',
                dataIndex: 'StopDate',
                width: 120,
                align: "center"
            },
            {
                title: '创建日期',
                dataIndex: 'CreateDate',
                width: 120,
                align: "center"
            },
            {
                title: '创建时间',
                dataIndex: 'CreateTime',
                width: 120,
                align: "center"
            },
            {
                title: '创建人',
                dataIndex: 'CreateUser',
                width: 60,
                align: "center"
            },
            {
                title: '更新日期',
                dataIndex: 'UpdateDate',
                width: 120,
                align: "center"
            },
            {
                title: '更新时间',
                dataIndex: 'UpdateTime',
                width: 80,
                align: "center"
            },
            {
                title: '更新人',
                dataIndex: 'UpdateUser',
                align: "center"
            },
            {
                title: '特殊项目',
                dataIndex: 'Add',
                width: 80,
                align: "center",
                fixed: 'right',
                render: (text, record,index) =>
                {
                return (
                  <div>
                    { 
                      <a onClick={() => this.upDataonChangeSub(record)}>特殊项目</a>
                    }
                  </div>
                );}
            },
            
        ];
    }
    
    // 新增
    handleAdd = () => {
        // 显示弹框
        this.child.resetFields()
        this.setState({ListData: '',divSubdisplay:'',divmentdisplay:''});
        this.child.showModal()
        // 初始化数据
        this.setState({onlytype: 'add', title: '新增'})
    }

    // 查询
    handleQuery = () => {
        let {code, describe, page, pageSize,FindType,TypeID,Alias} = this.state;
        let data = {}
        if (code || describe) {
            data = {
                params: [{
                    Code: code,
                    Descripts: describe,
                    FindType: FindType,
                    TypeID: TypeID,
                    Alias: Alias
                }],
                pagination:[{
                    pageSize: 10,
                    currentPage: 1,
                    sortColumn: "Code",
                    sortOrder: "asc"
                }]
            }
        } else {
            data = {
                params: [{
                    Code: code,
                    Descripts: describe,
                    FindType: FindType,
                    TypeID: TypeID,
                    Alias: Alias

                }],
                pagination:[{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: "Code",
                    sortOrder: "asc"
                }]
            }
        }
        $http.post('urlS', this, {
        	code: '05100025',
          data: data,
          success: function (res) {
            if (+res.errorCode === 0 && res.result) {
              for(var i = 0; i < res.result.Data.length; i++) {
                res.result.Data[i].key = i
              }
              this.setState({data: res.result.Data, total: res.result.TotalCount, loading: false})
            } else {
                setTimeout(() => {
                    this.setState({data: [], total: 0, loading: false})
                }, 0)
            }
          }
        })
    }
    // 页码改变的回调，参数是改变后的页码及每页条数
    onChange = (page, pageSize)=> {
        this.setState({page: page, pageSize: pageSize}, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }
    //弹出子框
    upDataonChangeSub = (record) => {
        this.child.resetFields()
        this.setState({ ListData: record});
        this.setState({divmentdisplay: 'none',divSubdisplay:'', title: '折扣记账系数配置'})
        this.child.showModal()
        this.setState({
            selectrowid: record.ID,
        });
    }
    // 调用子组件的连接事件
    onRef = (ref) => {
        this.child = ref
    }
    // 重置userInfo
    resetUserInfo() {
        this.setState({userInfo: {}})
    }
    componentDidMount() {
        // 初始化列表数据
        this.handleQuery();
        // 初始化设置table的高度以及表体的高度
        document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 'px';
        // 数据异步请求，请求成功之后setState
		this.setState({
			showLoading: false
		})
    }
    // 设置行属性
    selectRow = (record) => {
        return {
            // 单击行选中
            onClick: () => {
                if(this.state.selectrowid===""){
                    this.setState({
                        selectrowid: record.ID,
                    });
                }else{
                    if(this.state.selectrowid!==record.ID){
                        this.setState({
                            selectrowid: record.ID,
                        });
                    }else{
                        this.setState({
                            selectrowid: "",
                        });
                    }
                }
            },
            // 双击行编辑
            onDoubleClick: () => {
                //console.log("UnitID:"+record.UnitID)
                this.child.resetFields()
                this.setState({ ListData: record});
                this.setState({divSubdisplay: 'none', title: '信息详情',divmentdisplay:'',})
                this.child.showModal()
                this.setState({
                    selectrowid: record.ID,
                });
            },
        }
    }
    setRowClassName = (record) => {
        return record.ID === this.state.selectrowid ? 'clickRowStyle' : '';
      }
    render(){
        return (
            <Spin tip="加载中..." spinning={this.state.showLoading}>
                <div>
                    <div style={{ padding: 24, marginBottom: 24 }}>
                        <div className="table-operations" style={{marginBottom: '30px'}}>
                            <Row>
                                <Col span={24}>
                                    代码：<Input placeholder="请输入" value={this.state.code} style={{width: '100px', marginRight: '10px'}} onChange={this.onChangeCode} />
                                    <Button style={{background: 'rgba(66,185,131,1)'}} className="margiRight16" onClick={this.handleQuery}>查询</Button>
                                    <Button style={{background: 'rgba(66,185,131,1)'}} onClick={this.handleAdd}>新增</Button>
                                </Col>
                            </Row>
                        </div>
                        <div>
                            <Table
                                style={{height: store.getState().tableHeight.y + 100 + 'px'}}
                                dataSource={this.state.data}
                                columns={this.columns}
                                rowClassName={this.setRowClassName}
                                pagination={false}
                                scroll={{ x: '150%', y: store.getState().tableHeight.y }}
                                bordered
                                loading={this.state.loading}
                                onRow={this.selectRow}
                            />
                            {/* 分页: total--总条数  defaultCurrent--默认的当前页 hideOnSinglePage--只有一页时隐藏分页器 */}
                            <Pagination
                                size="small"
                                showQuickJumper
                                showSizeChanger
                                defaultCurrent={1}
                                total={this.state.total}
                                onChange={this.onChange}
                                onShowSizeChange={this.onShowSizeChange}
                                style={{textAlign: "right", marginTop: '13px'}}
                            />
                        </div>
                    </div>
                    <MenuDiscountConfigUpdata
                        size="small"
                        onRef={this.onRef}
                        ListData={this.state.ListData}
                        handelThis={this}
                        divSubdisplay={this.state.divSubdisplay}
                        divmentdisplay={this.state.divmentdisplay}
                        title={this.state.title}
                        onlytype={this.state.onlytype}
                    />
                </div>
            </Spin>
        );
    }

}

