import { Component } from 'react'
import Taro from '@tarojs/taro'
import { connect } from 'react-redux'
import {View, Input} from '@tarojs/components'
import {login} from '../../utils/servers/servers';
import {setOpenId} from '../../utils/servers/servers';

import { add, minus, asyncAdd } from '../../actions/counter'

import { AtButton,AtIcon,AtModal  } from 'taro-ui'

import NavBar from '../../components/common/NavBar';

import 'taro-ui/dist/style/components/button.scss';
import 'taro-ui/dist/style/components/loading.scss';
import 'taro-ui/dist/style/components/icon.scss';
import 'taro-ui/dist/style/components/modal.scss';

import './index.less'

const list = [

]

@connect(({ counter }) => ({
  counter
}), (dispatch) => ({
  add () {
    dispatch(add())
  },
  dec () {
    dispatch(minus())
  },
  asyncAdd () {
    dispatch(asyncAdd())
  }
}))
class Login extends Component {
  constructor(props) {
    super(props);
    this.state={
      username:'',
      password:'',
      isOpened:false,
      openedType:0
    }
    this.login = this.login.bind(this);
    this.authorization = this.authorization.bind(this);
  }

  componentDidShow () {

  }

  componentDidMount() {

  }

  authorization(){
    this.setState({
      loading:true,
    })
    let _this= this;
    Taro.getSetting({
      withSubscriptions: true,
      success:(res)=>{
        console.log(res.subscriptionsSetting,'res.subscriptionsSetting')
        if(res.subscriptionsSetting.mainSwitch){
          if(res.subscriptionsSetting['j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I'] === "reject" || res.subscriptionsSetting['pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI'] === "reject" || res.subscriptionsSetting['vo28uzT2sLS-9ioroNyZbMSvu0mMvf6le2pDZVN891U'] === "reject"){
            this.setState({
              isOpened:true,
              loading:false,
              openedType:1
            })
            return;
          }
          Taro.requestSubscribeMessage({ //获取下发权限
            tmplIds: [
              'pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI',
              'j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I',
              'vo28uzT2sLS-9ioroNyZbMSvu0mMvf6le2pDZVN891U'
            ], //此处写在后台获取的模板ID，可以写多个模板ID，看自己的需求
             success: function (s) {
                _this.setState({
                  loading:false,
                })
               _this.login();
            },
            fail: function (err) {
              _this.setState({
                loading:false,
              })
              console.log(err)
            }
          })
        }else{
          this.setState({
            isOpened:true,
            loading:false,
          })
        }
      },
      fail:()=>{
        this.setState({
          loading:false,
        })
      }
    })
  }

  login(){
    if(!this.state.username){
      Taro.showToast({
        title: '请输入用户名',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!this.state.password){
      Taro.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.setState({
      loading:true,
    })
    login({
      username:this.state.username,
      password:this.state.password
    }).then((msg)=>{
      this.setState({
        loading:false
      });
      if(!msg.data.openId){
        Taro.login({
          success: (res)=>{
            setOpenId({
              code:res.code,
            }).then(v=>{
              if(v.error.length === 0){
                Taro.switchTab({
                  url: '/pages/examine/index',
                })
              }else{
                Taro.showToast({title:v.error[0].message})
              }
            })
          },
          fail:()=>{
            Taro.showToast({title:'系统错误,请稍后重试'})
          }
        });
      }else{
        Taro.switchTab({
          url: '/pages/applyDepart/index',
        })
      }
    }).catch((err)=>{
      console.log(err)
      this.setState({
        loading:false
      })
    })
  }

  render () {
    return (
      <View className='login'>
        <NavBar
          extClass='NavBar'
          title='科德集团'
          background='#0000'
          color='#FFF'
          onHome={()=>{}}
        />
        <View className='welcome'>
          <View className='welcomeTitle'>科德信息管理</View>
          <View className='welcomeTitle'>欢迎您!</View>
        </View>
        <View className='loginContent'>
          <View className='loginItem'>
            <AtIcon value='user' size='20' color='#999999'/>
            <Input
              style={{marginLeft:'15px'}}
              type='text'
              placeholder='请输入您的系统账户'
              value={this.state.username}
              onInput={(v)=>{
                this.setState({
                  username: v.detail.value
                })
              }}
            />
          </View>
          <View className='loginItem'>
            <AtIcon value='lock' size='20' color='#999999'/>
            <Input
              style={{marginLeft:'15px'}}
              type='password'
              placeholder='请输入密码'
              value={this.state.password}
              onInput={(v)=>{
                this.setState({
                  password: v.detail.value
                })
              }}
            />
          </View>
          <AtButton type='primary' 	circle loading={this.state.loading} onClick={this.authorization}>登录</AtButton>
        </View>
        <AtModal
          isOpened={this.state.isOpened}
          title='提醒'
          cancelText='取消'
          confirmText='确认'
          onClose={()=>{
            this.setState({
              isOpened:false,
              openedType:0,
            })
          }}
          onCancel={()=>{
            this.setState({
              isOpened:false,
              openedType:0
            })
          }}
          onConfirm={()=>{
            this.setState({
              isOpened:false,
              openedType:0
            },()=>{
              Taro.openSetting({
                success:(value)=> {

                }
              })
            })
          }}
          content={this.state.openedType === 1 ? '您存在拒绝接收的消息类型,请点击确认按钮,前往设置页->通知管理,点击不接受的消息通知类型,调整为允许' : '您关闭了接收通知权限,将不能收到订阅通知,请点击确认按钮,前往设置页->通知管理->接收通知,打开接收通知权限'}
        />
      </View>
    )
  }
}

export default Login

