<template>
  <div class="d-page d-flex flex-column">
    <div class="d-logb">
      <img src="../../assets/images/chengdu-bank-logo.png" alt=""/>
      <div class="d-logb-wen">移动信贷展业平台</div>
    </div>
    <div class="d-login-box">
      <!--      <div class="d-login-box-top">个贷系统</div>-->
      <!--      <div class="d-login-box-top-r">→对公系统</div>-->
      <van-form style="width: 25vw" @submit="onSubmit">
        <van-field class="username" label-width="20px" v-model="username" name="username" placeholder="用户名"
                   :rules="[{ required: true, message: '请填写用户名' }]">
          <van-icon class="d-login-t1" name="manager" slot="label" props="center" size="25px"/>
        </van-field>
        <van-field class="password" label-width="20px" v-model="password" type="password" name="password"
                   placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]">
          <van-icon class="d-login-t1" name="lock" slot="label" size="25px"/>
        </van-field>
        <div class="d-bottom">
          <van-button round block type="info" class="d-login-box-bottom" native-type="submit">
            <span class="d-login-text">登录</span>
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>
<script lang="ts">
  import {Component, Vue} from "vue-property-decorator";
  import IF from "@/public/factory/InterFaceFactory";
  import nettyApi from '@/constants/api/ms-netty/netty.api';
  import {NativeUI} from "@/public/ts/NativeUI";
  import systemApi from '@/constants/api/ms-system/system.api';
  import DataDics from '@/services/dict.service.ts';

  @Component({
    name: "LoginView",
  })
  export default class LoginView extends Vue {
    // 用户名
    username: string | undefined = "";
    // 密码
    password: string | undefined = "";

    columns: any = []; //['个贷系统', '网贷系统', '信贷系统'],

    menu: string = "";

    mounted() {
      //初始化数据字典
      DataDics.init();
    }

    onSubmit(data: any) {
      // this.$router.push({
      //   path: "TYMHView",
      //   params: {},
      // });
      // this.$router.push({
      //   path: "/main",
      //   params: {},
      // });
      this.authenticationLogin();
    }

    //统一认证登录
    authenticationLogin() {
      const param = {
          tc: nettyApi.TRADE_CODE.authenticationLogin,
          accountType: '1',
          account: this.username,//liuwt
          password: this.password,//dev@1234
          // account: "liuwt",//liuwt
          // password: "dev@1234",//dev@1234
          sysCode: 'MT',
          isAppList: '0',
          authenType: '1',
          isReturnToken: '0'
        },
        that = this;
      NativeUI.showWaiting();
      IF.transferDataInter(nettyApi.unifiedLogin, param).then((res: any) => {
        NativeUI.closeWaiting();
        console.log(res);
        if (res.rc == '1') {
          that.selectRoleInfo(res.userData)
        } else {
          NativeUI.toast(res.msg)
        }
      });
    }

    selectRoleInfo(userData: any) {
      const parm = {
        tc: "MCEP",
        loginType: "1", //系统源 1-手机
        // hrCode: "86M1909695", //对公人力资源代码  //个人人力资源代码77F9600649
        hrCode: userData.hrEmployeeNo,
        idNum: '', //身份证号
      };
      NativeUI.showWaiting();
      IF.transferDataInter(systemApi.commonRq, parm).then((res: any) => {
        NativeUI.closeWaiting();
        console.log(parm);
        console.log("登录返回");
        console.log(res);
        if (res.code == 1) {
          //成功
          /**
           * 成功操作：
           * 1、存储人力资源编号
           * 2、跳转页面
           * */
          if (res.data.mobileSysUserInfo.sysRoleInfos != "") {
            this.menu = res.data.mobileSysUserInfo.sysRoleInfos;
            let roles = "";
            for (let obj of res.data.mobileSysUserInfo.sysRoleInfos) {
              this.columns.push(obj.roleSystmName);
              roles += obj.roleId + ",";
            }
            sessionStorage.setItem("workcode", res.data.mobileSysUserInfo.hrCode);
            // this.setUserInfo(res.data.mobileSysUserInfo);

            const token = res.data.token;
            for (const key in token) {
              sessionStorage.setItem(key, token[key]);
            }
            sessionStorage.setItem("hrCode", userData.hrEmployeeNo);
            sessionStorage.setItem("userName", res.data.mobileSysUserInfo.userName);
            sessionStorage.setItem("ORGNAME", res.data.mobileSysUserInfo.coreName);
            sessionStorage.setItem("roleNo", roles);
          }
          //TODO 获取接口返回的数据
          sessionStorage.setItem("branchNo", res.data.mobileSysUserInfo.orgId);
          this.$router.push({
            name: "TYMHView",
            params: {
              systemList: res.data.mobileSysUserInfo.sysRoleInfos,
              hrCode: userData.hrEmployeeNo
            },
          });
        } else {
          //失败
          /**
           * 失败操作：
           * 1、失败吐司
           * 2、清空数据
           * */
          sessionStorage.setItem("workcode", "");
          sessionStorage.setItem("branchNo", "");
          NativeUI.toast(res.msg);
        }
      });
    }
  }
</script>
<style lang="scss" scoped>
  .d-login-box {
    height: 64%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
  }

  .d-login-box-top {
    font-family: 思源黑体;
    font-weight: bold;
    font-size: 20px;
    color: #333333;
    margin-bottom: 30px;
    float: left;
  }

  .d-login-t1 {
    margin-top: 8px;
  }

  .d-login-box-top-r {
    float: right;
    line-height: 30px;
    color: #808080;
    font-size: 16px;
  }

  .d-login-box-bottom {
    border: 0;
    background: linear-gradient(270deg, rgba(255, 53, 65, 1) 1%, rgba(255, 141, 155, 1) 98%);
    height: 48px;
  }

  .d-page {
    background-image: url("../../assets/images/login-background.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 100%;
    height: 100%;
  }

  img {
    width: 20%;
    float: left;
  }

  .d-logb {
    margin-left: 50px;
    margin-top: 30px;
    display: flex;
    justify-content: flex-start;
    align-items: flex-end;
  }

  .d-logb-wen {
    float: left;
    margin-top: 40px;
    margin-left: 20px;
    font-size: 18px;
    color: #666666;
  }

  .username {
    width: 100%;
    height: 62px;
    margin-bottom: 15px;
    line-height: 42px;
  }

  .password {
    width: 100%;
    height: 62px;
    margin-bottom: 35px;
    line-height: 42px;
  }

  .u16_div {
    width: 326px;
    height: 135px;
    background-color: white;
    border: 1px solid rgb(204, 204, 204);
    margin: 30px auto auto;
    text-align: center;
    padding-top: 10px;
    color: #666666;
  }

  .u16_div_text {
    margin-right: 60px;
    color: black;
  }

  .d-login-text {
    font-size: 20px;
  }
</style>
