<style>
  .form-signin {
    max-width: 500px;
    padding: 15px;
    margin: 0 auto;
  }
  .form-signin .form-signin-heading,
  .form-signin .checkbox {
    margin-bottom: 10px;
  }
  .form-signin .checkbox {
    font-weight: normal;
  }
  .form-signin .form-control {
    position: relative;
    height: auto;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    padding: 10px;
    margin: 5px 0px 5px 0px;
    font-size: 16px;
  }
  .form-signin .form-control:focus {
    z-index: 2;
  }
  .form-signin input {
    margin-bottom: -1px;
    border-bottom-right-radius: 0;
    border-bottom-left-radius: 0;
  }
  .form-signin input[type="password"] {
    margin-bottom: 10px;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
  }
</style>

<template>
  <div class="container" style="background: #4C637B;">
    <validator name='v'>
      <form class="form-signin" novalidate @submit.prevent="signIn()">
        <div>
          <!-- <img src="../../assets/logo1.png" class="center-block img-responsive"></img> -->
        </div>
        <h2 class="form-signin-heading text-center" style="color: #fff;">安检系统</h2>
        <h4 class="form-signin-heading text-center" style="color: #fff;"><a @dblclick ="backset()">欢迎使用移动安检系统</a></h4>
        <!-- <h5 class="form-signin-heading text-center" @click="backupDatabase">当前版本：0.{{version}}</h5> -->
        <input id="ename" style="margin-top: 100px;" class="form-control" placeholder="请输入用户名" autofocus v-model="model.ename" v-validate:ename='{ required: true }'>
        <input type="password"  id="inputPassword" class="form-control" placeholder="请输入密码" v-model="model.password" v-validate:password='{ required: true }'>
        <button :disabled="!$v.valid" class="btn btn-lg btn-success btn-block" type="submit" style="color: #fff;background-color: #67BA2F; border-color: #67BA2F;">登录</button>
      </form>
    </validator>
    <div class="login-footer login-footer_fixed-bottom">
      <p class="login-footer__links">
        <a href="javascript:home();" class="login-footer__link"></a>
      </p>
      <p class="login-footer__text">Copyright @ v1.0.0 安检</p>
    </div>
  </div>
</template>
<script>
  import Vue from 'vue'
  import co from 'co'
  import * as Util from '../Util'
  import JSEncrypt from 'jsencrypt'
  import AppData from '../../stores/AppData'

  //获取json配置文件
  let asyncReady = async function(self,url) {
    // 获取配置信息
    try {
      let res = await Vue.resetget(`${url}/phone/rs/vue`,{data: {}},{resolveMsg: null, rejectMsg: null})
      //tag
      Vue.config = res.data
    } catch (error) {
      // 忽略704,文件找不到异常R
      if (error.status !== 704) {
        throw error
      }
    }
  }

  export default {
    title: '登录系统',
    data() {
      return {
        model: {
          ename: '',
          password: ''
        }
      }
    },
    ready () {
      if(Vue.android) {
        this.backupDatabase()
        let version = HostApp.getAppVersion().data
        this.$set('version', version)
      }

    },
    methods: {
      backupDatabase () {
        HostApp.backupDatabase()
      },

      load () {
        let getServiceUrl = this.$androidUtil.getProxyUrl()
        //tag

        let data = {username: this.model.ename, password: this.model.password}
        //tag
        //tag

        // rsa加密
        let encrypt = new JSEncrypt();
        encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqPvovSfXcwBbW8cKMCgwqNpsYuzF8RPAPFb7LGsnVo44JhM/xxzDyzoYtdfNmtbIuKVi9PzIsyp6rg+09gbuI6UGwBZ5DWBDBMqv5MPdOF5dCQkB2Bbr5yPfURPENypUz+pBFBg41d+BC+rwRiXELwKy7Y9caD/MtJyHydj8OUwIDAQAB');
        data = encrypt.encrypt(JSON.stringify(data))
        //tag

        this.$resetpost(`${this.$androidUtil.getProxyUrl()}/rs/user/userLogin/手机服务`, data, {
          resolveMsg: null,
          rejectMsg: '资源服务数据获取异常'
        }).then((ret) => {
          //tag )
        //tag
        // 设置登录人
        Vue.user = Util.f = ret.data
        //tag )
        if (Vue.android){
          let userStr = `{'username':'${this.ename}','password':'${this.password}'}`
          //tag
          this.$androidUtil.setPreference('login_user_cache', userStr)
          //tag
          this.$androidUtil.setPreference('f_repairman_id', Util.f.id)
          this.$androidUtil.setPreference('f_repairman_name', Util.f.name)

        }
        Vue.$login = {jwt: ret.data.id}
        // 获取参数
        //tag
        let uri  = ''
        if(Vue.andoird){
        uri =   this,this.$androidUtil.getProxyUrl()
        }
        asyncReady(this,uri)
        //tag
        this.$goto('nav-bottom')
      }).catch((error) => {
          // console.warn(JSON.stringify(error))
          //tag)
        if (Vue.android)
          this.$showMessage('登录失败！')
      })
      },
      signIn () {
        this.login = true
        // 模拟手机，定时提取
        // this.logins()
        // 获取登录加载数据
        this.load()
      },
      logins(){
        let getServiceUrl = this.$androidUtil.getProxyUrl()
        if (this.model.ename === '' || this.model.password === '') {
          this.$showMessage('登录失败, 用户名或密码不能为空！')
          // HostApp.alert('登录失败, 用户名或密码不能为空！')
          return
        }
        this.$resetpost(`${getServiceUrl}/rs/logic/phonelogin`, {data: this.model}, {resolveMsg: null, rejectMsg: null}).then((ret) => {
          //tag
          if (ret.data.state === 0) {
            this.$goto('nav-bottom')
            Vue.user = ret.data.data
          } else {
            this.$showMessage('登录失败 ' + ret.data.msg)
          }
        })
      },
      backset(){
        this.$showMessage('确认回到设置页面吗？', ['cancel', 'confirm']).then(
          (response) => {
            if (response === 'confirm') {
              HostApp.toSet()
                }
        })
      },
      isJSON(str) {
        if (typeof str == 'string') {
          try {
            var obj=JSON.parse(str);
            if(typeof obj == 'object' && obj ){
              return true;
            }else{
              return false;
            }} catch(e) {
            //tag;
            return false;
          }
        }
        //tag
      }
    }
  }
</script>
<style scoped>
  .login-footer_fixed-bottom {
    height: auto;
    position: fixed;
    bottom: .52em;
    left: 0;
    right: 0;
  }
  .login-footer {
    color: #999;
    font-size: 14px;
    text-align: center;
  }
  .login-footer__link {
    color: #586C94;
  }
</style>
