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

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

import './index.less'


@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:''
    }
    this.login = this.login.bind(this);
  }

  componentDidShow () {

  }

  componentDidMount() {

  }

  login(){
    // Taro.getSetting({
    //   withSubscriptions: true,
    //   success:(res)=>{
    //     console.log(res.subscriptionsSetting,'res.subscriptionsSetting')
    //     if(res.subscriptionsSetting.mainSwitch){
    //       // res.subscriptionsSetting = {
    //       //   mainSwitch: true, // 订阅消息总开关
    //       //   itemSettings: {   // 每一项开关
    //       //     SYS_MSG_TYPE_RANK: 'accept',
    //       //     'pWia-KJPFwDM8ReDu_BOa2_42G7W2hXB16sUDbSxpcM': 'accept',
    //       //     'j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I': 'accept',
    //       //     'vo28uzT2sLS-9ioroNyZbMSvu0mMvf6le2pDZVN891U': 'accept',
    //       //   }
    //       // }
    //       Taro.requestSubscribeMessage({ //获取下发权限
    //         tmplIds: [
    //           'pWia-KJPFwDM8ReDu_BOa2_42G7W2hXB16sUDbSxpcM',
    //           'j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I',
    //           'vo28uzT2sLS-9ioroNyZbMSvu0mMvf6le2pDZVN891U'
    //         ], //此处写在后台获取的模板ID，可以写多个模板ID，看自己的需求
    //          success: function (s) {
    //           console.log(s)
    //         },
    //         fail: function (err) {
    //           console.log(err)
    //         }
    //       })
    //     }else if(res.subscriptionsSetting.mainSwitch.itemSettings){
    //       console.log(res)
    //     }else{
    //       Taro.requestSubscribeMessage({
    //         tmplIds: ['SLb0P5wDArFZU05iMrrnNEsXZ7H2wArqdY8__4M1NUUM'],
    //         success : (res) => {
    //
    //         }
    //       })
    //     }
    //   }
    // })
    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/applyDepart/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>
        <View>
          <View>用户名</View>
          <Input
            type='text'
            placeholder='请输入用户名'
            value={this.state.username}
            onInput={(v)=>{
              this.setState({
                username: v.detail.value
              })
            }}
          />
        </View>
        <View>
          <View>密码</View>
          <Input
            type='password'
            password
            placeholder='请输入密码'
            value={this.state.password}
            onInput={(v)=>{
              this.setState({
                password: v.detail.value
              })
            }}
          />
        </View>
        <Button type='primary' loading={this.state.loading} onClick={this.login}>登录</Button>
      </View>
    )
  }
}

export default Login

