/***
 * XYH
 * 选择科室的消息配置
 */
import React, { Component } from 'react';
import { MessageSocket } from 'tools/MessageSocket.js';
import { Modal, message, Input, Button, Select, Radio, Icon, Table, Col,Checkbox } from 'antd';
import fetchJsonp from 'fetch-jsonp';
import { $http } from '../../containers/config/https';
import './CSS/message.less';
import CommonPagination from 'components/pagination/index';
import RangeDatePicker from 'components/rangePicker/index';
import ColumnAuthoritysModel from '../columnAuthority/ColumnAuthoritysModel'
import moment from 'moment';
import { Util, dayFormat } from 'tools/index';

let user;
const { Search } = Input;
const { Option } = Select;
const confirm = Modal.confirm;
const { TextArea } = Input;
class SelectLocRecive extends Component{
    constructor(props) {
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        super(props);
        var date = new Date()
        this.state = {
            column:[],
            dataSource:[],
            diagColumns:[],
            ids:[],
        }
    }

    componentDidMount() {
        this.loadColumn()
        this.loadDate()
      }
    
    componentWillUnmount() {
       
    }
    
    componentWillReceiveProps(nextProps, nextState) {

    }
    
    //查询列头数据
    loadColumn = () => {
        let data = {
            params: [{
                compontName: 'SelectLocRecvie',
                type: "C",
                reactCode: ['SelectLocRecvie'],
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {    
                    let columns = res.result.C;
                    if (columns) {
                        for (var ci = 0; ci < columns.length; ci++) {
                            if (columns[ci].dataIndex == 'receiveAuthority') {
                                var diagColumns = [];
                                diagColumns.push({
                                    title: user.language != 'EN' ? user.language != 'EN' ? columns[ci].title : columns[ci].enDescripts : columns[ci].enDescripts,
                                    dataIndex: columns[ci].dataIndex,
                                   //width: width,
                                    align: 'center',
                                    render: (text, data, index) => {
                                        return  <Checkbox  defaultChecked={data.receiveAuthority =="Y"?true:false} onChange={this.changeState.bind(this,'receiveAuthority',data.roleID)}  > </Checkbox>
                                    }
                                })
                                columns[ci] = diagColumns[0]
                            }
                            if (columns[ci].dataIndex == 'emergentAutoAlert') {
                                var diagColumns = [];
                                diagColumns.push({
                                    title: user.language != 'EN' ? user.language != 'EN' ? columns[ci].title : columns[ci].enDescripts : columns[ci].enDescripts,
                                    dataIndex: columns[ci].dataIndex,
                                   //width: width,
                                    align: 'center',
                                    render: (text, data, index) => {
                                        return  <Checkbox  defaultChecked={data.emergentAutoAlert =="Y"?true:false} onChange={this.changeState.bind(this,'emergentAutoAlert',data.roleID)}  defaultValue={['Y']} > </Checkbox>
                                    }
                                })
                                columns[ci] = diagColumns[0]
                            }
                            if (columns[ci].dataIndex == 'palyVoice') {
                                var diagColumns = [];
                                diagColumns.push({
                                    title: user.language != 'EN' ? user.language != 'EN' ? columns[ci].title : columns[ci].enDescripts : columns[ci].enDescripts,
                                    dataIndex: columns[ci].dataIndex,
                                   //width: width,
                                    align: 'center',
                                    render: (text, data, index) => {
                                        return  <Checkbox  defaultChecked={data.palyVoice =="Y"?true:false} onChange={this.changeState.bind(this,'palyVoice',data.roleID)}  defaultValue={['Y']} > </Checkbox>
                                    }
                                })
                                columns[ci] = diagColumns[0]
                            }
                        }
                    }
                    this.setState({
                        column:columns,
                    })
                }
            }
        })
    }

    loadDate = () => {
        let data = {
            params: [{
                hospID:user.hospID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "04180009",
            data: data,
            success: (res) => {
               this.setState({
                dataSource:res.result.MessageLocArr,
               })
            }
        })
    }
    onDiagRowClick (record, index, e) {
    }

    changeState (Flag,roleID,e) {
        console.log(e)
        var ids = this.state.ids
        var idObj = {}
        var mark = false
        if(ids.length>0){
            for(var i=0;i<ids.length-1;i++){
                if(roleID==ids[i].roleID){
                    mark = true;
                    if(Flag == "receiveAuthority"){
                        ids[i].receiveAuthority = e.target.checked?"Y":"N"
                    }
                    if(Flag == "emergentAutoAlert"){
                        ids[i].emergentAutoAlert = e.target.checked?"Y":"N"
                    }
                    if(Flag == "palyVoice"){
                        ids[i].palyVoice = e.target.checked?"Y":"N"
                    }
                   break; 
                }
            }
        }
        if(!mark){
            idObj.roleID=roleID
            if(Flag == "receiveAuthority"){
                idObj.receiveAuthority = e.target.checked?"Y":"N"
            }
            if(Flag == "emergentAutoAlert"){
                idObj.emergentAutoAlert = e.target.checked?"Y":"N"
            }
            if(Flag == "palyVoice"){
                idObj.palyVoice = e.target.checked?"Y":"N"
            }
            ids.push(idObj)
        }
        this.setState({
            ids:ids
        })
    }

    SaveMessageLoc(){
        if(this.state.ids.length == 0){
            message.warn("没有要修改的数据");
            return;
        }
        let data = {
            params: [{
                ids:this.state.ids
            }]
        }
        $http.post('urlDeault', this, {
            code: "04180010",
            data: data,
            success: (res) => {
                this.setState({
                    ids:[],
                },()=>{
                    this.loadDate()
                })
                message.warn(res.errorMessage); 
            }
        })
    }

    render(){
        return (
        <div style={{overflow:'hidden'}}>
            <Input placeholder="输入科室名称" /><Button type="primary">查询</Button>
            <Button type="primary" onClick={this.SaveMessageLoc.bind(this)}>保存</Button>
            <Table
            pagination={false}
            style={{ height: 'calc(100% - 38px)', padding: '0 5px' }}
            scroll={{ y: 'calc(100% - 46px)' }}
            bordered
            onRow={(record, index) => ({ onClick: (e) => { this.onDiagRowClick(record, index, e) } })}
            columns={this.state.column}
            dataSource={this.state.dataSource}
            rowKey={(row) => {
              return row.id;
            }}
          >
          </Table>
        </div>
        )
    }

}
export default  SelectLocRecive;