import React from 'react'
import 'antd-mobile/dist/antd-mobile.css'; 
import { Card, WingBlank, WhiteSpace,Button, List, InputItem,Modal,Toast ,Badge } from 'antd-mobile';
import {Auth} from '../../Utils/help';
import { browserHistory } from 'react-router'; 
import { Http } from '../../Utils/help';

const prompt = Modal.prompt
export class My extends React.Component {
    constructor(props) {
        super(props);
        this.showpass = this.showpass.bind(this);
        this.resetPass = this.resetPass.bind(this);
        this.settingUrl = this.settingUrl.bind(this);
        this.messageCenter = this.messageCenter.bind(this);
        this.myallbills=this.myallbills.bind(this)
        this.msgSubscription = this.msgSubscription.bind(this);
        this.getUserEntity = this.getUserEntity.bind(this);
          this.state= {
              userpassword:'',
              isShowpas:false,
              ip:[],
              user:{},
              unread:''//判断是否有未读消息
          };    
    }
    componentWillMount(){
        this.setState({
            userpassword:localStorage.userpassword
        })
        this.getUserEntity()
        var unread
        this.dataAjax = Http.getJSON2("showmessage", {},data=>{
            console.log(data)
            data.map((item,index) =>{
                if(item.ExtraDatas.unreadnum > 0){
                    unread=true
                    return
                }   
                else
                  unread=false 
            })

            this.setState({unread})
            console.log(this.state.unread) 
        }
        )

    }
    getUserEntity=()=>{
        this.dataAjax = Http.getJSON2("user",{"code":localStorage.username})
        .done((user)=>{
            console.log("getUser",user.data[0],user.data[0]!=undefined && user.data[0] != null)
            this.setState({
                user:user.data[0],
            })
        })
    }
    //对每个topid进行的处理
    onClick =()=>{
        localStorage.removeItem('access_token');
        localStorage.removeItem('refresh_token');
        localStorage.removeItem('expired_at');
        localStorage.removeItem('username');
        localStorage.removeItem('usercode');
        localStorage.removeItem('loginid'); 
        localStorage.removeItem('menu'); 
        browserHistory.push('/login');
        window.Android.userLogout();
    }
    settingUrl=()=>{
        window.Android.settingURL()
    } 
    messageCenter=()=>{
        browserHistory.push('alertlist');
    }
    myallbills=()=>{
        browserHistory.push('myallbills');
    }
    msgSubscription=()=>{
        browserHistory.push('subscription')
    }
    showpass=()=>{
        this.setState({
            isShowpas: !this.state.isShowpas
        })
        setTimeout(()=>{this.setState({
            isShowpas: false
        })},3000)
    }
    resetPass=(pass)=>{
        console.log('reset password',pass)
        pass = pass.replace(/(^\s*)|(\s*$)/g, "")
        if(!(pass&& pass.length>0))
        return 
        if(this.state.user != undefined && this.state.user!= null){
            let postuser = Object.assign({}, this.state.user, {Pwd: pass, status:'Modified'})
            this.dataAjax = Http.postJSON2("user", {...postuser}, this.succeed, this.error)
            .done((result)=>{
                console.log("post result", result)
                if(result&&result.Pwd.length>0){
                    console.log('密码修改成功了!')
                    localStorage['userpassword'] = pass
                    this.setState({
                        userpassword:localStorage.userpassword
                    })
                    this.getUserEntity()
                    Toast.info('修改密码成功!', 1);
                    window.Android.SaveUserInfo(localStorage.username, pass, localStorage.loginid)
                }
            })
            
        }
    }
    render() {        
        return (
            <div> 
                <WingBlank size="lg">
                    <WhiteSpace size="lg" />
                    <Card>
                    <Card.Header
                        title={Auth.UserName}
                        //thumb='dist/img/admin.png'
                        thumb='src/images/admin.png'
                        extra={<span>{Auth.UserCode}</span>}
                    />
                    <Card.Body>
                    </Card.Body>
                    <Card.Footer>
                    </Card.Footer>
                    </Card>
                    <WhiteSpace size="lg" />
                </WingBlank> 
                <WingBlank><List>
                <InputItem
                    type={this.state.isShowpas?"text":"password"}
                    editable="false"
                    value = {this.state.userpassword}
                    extra={ <img src={require('../../images/eye.png')} style={{ marginRight: '10px' }} />}
                    onExtraClick={this.showpass}
                >密码: </InputItem>
                </List></WingBlank>
                <br></br>
                <WingBlank><Button type="primary"  onClick={() => prompt('重置密码', '请输入新密码!', [
                { text: '取消' },
                { text: '确定', onPress: value =>this.resetPass(value) },
                ], 'default', '')}
                >重置密码 <Badge dot style={{background:'#108ee9'}}></Badge></Button></WingBlank>
                <br></br>
                <WingBlank><Button type="primary" onClick={this.msgSubscription}>消息订阅 <Badge dot style={{background:'#108ee9'}}></Badge></Button></WingBlank>
                <br></br>
                <WingBlank>               
                    <Button type="primary" onClick={this.messageCenter}>消息中心 <Badge dot style={{background:this.state.unread?'#FF0033':'#108ee9'}}></Badge></Button>
                </WingBlank>
                <br></br>
                <WingBlank><Button type="primary" onClick={this.myallbills}>我的单据 <Badge dot style={{background:'#108ee9'}}></Badge></Button></WingBlank>
                <br></br>               
                <WingBlank><Button type="primary" onClick={this.settingUrl}>重置网址 <Badge dot style={{background:'#108ee9'}}></Badge></Button></WingBlank>
                <br></br>
                <WingBlank><Button type="primary" onClick={this.onClick}>退出登录 <Badge dot style={{background:'#108ee9'}}></Badge></Button></WingBlank>
            </div>
        )
    }   
} 