/*
 * Creator:     ML
 * CreatDate:   2020-07-27
 * descripts：  个人公告
*/
import React from 'react';
import { Form, Row, Col, Input,Button,Table, message,Popconfirm, Spin } from 'antd';
import { $http } from '../../../containers/config/https';
import '../css/index.less';
const FormItem = Form.Item;
const PersonalAnnouncement = Form.create()(
    class PersonalAnnouncement extends React.Component {
        cacheOriginData = {};
        constructor(props) {
            super(props)
            let userData = sessionStorage.getItem("userData") ? JSON.parse(sessionStorage.getItem("userData")) : {};
            this.state = {
                userData,
                rightData: [],
                loadingDom:false,
                initRored:{}
            }
        }
        componentDidMount() {
            let userData = sessionStorage.getItem("userData") ? JSON.parse(sessionStorage.getItem("userData")) : {};
            this.setState({
                userData,
            })
            this.props.onPersonalAnnouncement(this)
        }
        getData = () => {
            let { userData } = this.state;
            this.handleCanel()
            let data = {}
            data = {
                "params": [{
                    hospID: userData && userData.hospID || "",
                    locID: userData && userData.locID || "",
                    docID: userData && userData.userID || "",
                }]
            };
            $http.post('urlDeault', this, {
                code: "03100145",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        res.result && res.result.map((item, i) => {
                            item.key = item.ID
                        })
                        this.setState({
                            rightData: res.result
                        })
                    }
                }
            });
        }
        //国际化
        setMessage(name, message) {
            return this.props.localeProvider ? this.props.localeProvider[name][message] : ''
        }
         //点击提醒
         handleTips = (desc) => {
            this.props.form.setFieldsValue({ title: undefined })
            this.props.form.setFieldsValue({ title: desc })
        }
        //点击右侧表格行
        onClickRightRow = (record) => {
            return {
                onClick: () => {
                    if (this.state.selectrow === "") {
                        this.setState({
                            selectrow: record.ID,
                            initRored: record
                        }, () => {
                        });
                    } else {
                        if (this.state.selectrow !== record.ID) {
                            this.setState({
                                selectrow: record.ID,
                                initRored: record
                            }, () => {
                            });
                        } else {
                            this.handleCanel()
                        }
                    }
                },
            }
        }
        setRowClassNames = (record) => {
            return record.ID === this.state.selectrow ? 'clickRowStyle' : '';
        }
        //取消发布 发布 
        handleCancelTo = (e,record) => {
            let { userData } = this.state;
            let data = {}
            data = {
                params: [{
                    userID: userData && userData.userID || "",
                    ID:record.ID&&record.ID||"",
                    status:record.status=="N"||record.status=="C"?"Y":"C"
                }],
            };
            $http.post('urlS', this, {
                code: "03100147",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        if(record.status=="N"||record.status=="C"){
                            message.success("发布成功")
                        }else{
                            message.success("已取消")
                        }
                        this.setState({
                        },()=>{
                            this.getData()
                        })
                        
                    }
                }
            });
        }
        //删除公告
        handleDetele = (e,id) => {
            let { userData } = this.state;
            let data = {}
            data = {
                params: [{
                    userID: userData && userData.userID || "",
                    ID:id&&id||""
                }],
            };
            $http.post('urlS', this, {
                code: "03100146",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        message.success("删除成功")
                        this.setState({
                        },()=>{
                            this.getData()
                        })
                        
                    }
                }
            });
        }
       
        //取消 状态初始
        handleCanel = () => {
            this.props.form.resetFields();
            this.setState({
                loadingDom:false,
                initRored:{},
                selectrow:""
            })
        }
        //保存发布
        handleSaveTo = (e,Y) => {
            this.handleSave(e,Y)
        }
        //保存
        handleSave = (e,flag) => {
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    this.setState({
                        loadingDom:true
                    })
                    let { userData,selectrow } = this.state;
                    let data = {}
                    data = {
                        params: [{
                            ID:selectrow&&selectrow||"",
                            hospID: userData && userData.hospID || "",
                            locID: userData && userData.locID || "",
                            docID: userData && userData.userID || "",
                            userID: userData && userData.userID || "",
                            title: values&&values.title||"",
                            flag: flag&&flag?flag:"N",
                            noticeContent: values&&values.noticeContent||"",
                        }],
                    };
                    $http.post('urlS', this, {
                        code: "03100148",
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                this.setState({
                                   loadingDom:false
                                },()=>{
                                    this.getData()
                                })
                                
                            }else{
                                this.setState({
                                    loadingDom:false
                                })
                            }
                        },error:function(){
                            this.setState({
                                loadingDom:false
                            })
                        }
                    });
                }else{
                    this.setState({
                        loadingDom:false
                    })
                }
            })

        }
        render() {
            const {initRored,rightData} = this.state
            const { getFieldDecorator } = this.props.form;
            const Columns = [
                {
                    width: 50,
                    align: "center",
                    title: '序号',
                    dataIndex: 'seqNo',
                    key: 'seqNo',
                    render: (text, record, index) => {
                        if (record.seqNo) {
                            return (
                                <span>{record.seqNo}</span>
                            )
                        }
                        return text;
                    },
                },
                {
                    align: "center",
                    title: '公告标题',
                    dataIndex: 'title',
                    key: 'title',
                    editable: false,
                    width: 80
                },
                {
                    align: "center",
                    title: '公告内容',
                    dataIndex: 'noticeContent',
                    key: 'noticeContent',
                    editable: false,
                },
                {
                    align: "center",
                    title: '是否发布',
                    dataIndex: 'statusDesc',
                    key: 'statusDesc',
                    editable: false,
                    width:80
                },
                {
                    title: '操作',
                    dataIndex: 'operation',
                    // key: 'action',
                    align: "center",
                    key: 'operation',
                    width: 80,
                    render: (text, record, index) => {
                        return (
                            <div>
                                <Popconfirm title="删除不可恢复，您确定要删除吗?" onConfirm={(e) => this.handleDetele(e, record.ID)}>
                                <span style={{ textAlign: "center", marginLeft: "4px",float:"right",marginRight:"20px" }}>
                                    <a style={{ color: "red" }}>删除</a>
                                </span>
                               </Popconfirm>
                            </div>
                        )

                    }
                }
            ];

            return (
                <Spin spinning={this.state.loadingDom} tip="加载中...">
                <div className="PersonalAnnouncement">
                    <Row style={{ margin: "12px 0",color:"#333" }} className="title">
                        <span className="line"></span>添加公告
                    </Row>
                    <Row>
                        <Col span={7}>
                            <FormItem label={"公告标题"} wrapperCol={{ span: 16 }} labelCol={{ span: 8 }}>
                                {getFieldDecorator('title', {
                                    initialValue: initRored&&initRored?initRored.title:undefined,
                                    rules: [{ required: true, message: "请输入公告标题" }],
                                })(
                                    <Input
                                        placeHolder={"请输入"}
                                    />
                                )}
                            </FormItem>
                            <div className="tips">
                                <span onClick={() => this.handleTips("温馨提醒")}>温馨提醒</span>
                                <span onClick={() => this.handleTips("敬告提醒")}>敬告提醒</span>
                            </div>
                        </Col>
                        <Col span={17}>
                            <FormItem label={"公告内容"} wrapperCol={{ span: 20 }} labelCol={{ span: 4 }}>
                                {getFieldDecorator('noticeContent', {
                                    initialValue:  initRored&&initRored?initRored.noticeContent:undefined,
                                    rules: [{ required: true, message: "请输入公告内容" }],
                                })(
                                    <Input
                                        placeHolder={"请输入"}
                                    />
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row style={{ float: "right" }}>
                        <Button type="" onClick={this.handleCanel} >取消</Button>
                        <Button style={{ margin: "0 8px", background: "#40a9ff" }} type="primary" onClick={(e)=>initRored&&initRored&&initRored.status?this.handleCancelTo(e, initRored):this.handleSaveTo(e,"Y")} >
                           {initRored&&initRored&&String(initRored.status)=="N"?"发布":String(initRored.status)=="C"?"重新发布":String(initRored.status)=="Y"?"取消发布":"保存并发布"} 
                        </Button>
                        <Button type="primary" onClick={(e)=>this.handleSave(e)} >保存</Button>
                    </Row>
                    <Row style={{ margin: "42px 0 12px 0" ,color:"#333"}} className="title">
                        <span className="line"></span>公告列表
                    </Row>
                    <Table
                        bordered
                        columns={Columns}
                        pagination={false}
                        rowClassName={this.setRowClassNames}
                        onRow={this.onClickRightRow}
                        dataSource={rightData}
                        rowKey={record => record.ID}
                        className="clearBackground"
                    />
                </div>
                </Spin>
            )
        }
    })
export default PersonalAnnouncement    