<template>
  <div class="login-container">
    <div class="flexBox">
      <div>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
          @submit.native.prevent="handleLogin"
        >
          <div class="flex">
            <div class="image-container">
              <div class="empty-height">
                <img class="logo" src="@/assets/login/logo.png" alt="" />
              </div>
              <el-carousel :interval="5000" arrow="always" height="440px" class="carousel">
                <el-carousel-item v-for="item in banner" :key="item.id">
                  <el-image :z-index="1" :src="imageURL + item.imgCarouselUrl"></el-image>
                </el-carousel-item>
              </el-carousel>
            </div>
            <div class="right-container">
              <div class="empty-height" />
              <div class="form-container">
                <div class="flex title">
                  <div class="title-item" :class="acitve == 'pas' ? 'acitve' : ''" @click="hanleTabs('pas', 'account')">
                    密码登录
                  </div>
                  <div class="title-item" :class="acitve == 'wei' ? 'acitve' : ''" @click="hanleTabs('wei', 'mobile')">
                    手机号登录
                  </div>
                </div>
                <transition name="slide">
                  <!-- 密码登录 -->
                  <div v-if="acitve == 'pas'">
                    <el-form-item prop="username" class="input-box">
                      <span class="svg-container">
                        <el-image
                          style="width: 20px; height: 20px"
                          :src="require('@/assets/login/icon_001.png')"
                          fit="contain "
                        ></el-image>
                      </span>
                      <el-input
                        ref="username"
                        v-model="loginForm.username"
                        placeholder="请输入用户名"
                        name="username"
                        type="text"
                        tabindex="1"
                        auto-complete="on"
                      />
                    </el-form-item>

                    <el-form-item prop="password" class="input-box">
                      <span class="svg-container">
                        <el-image
                          style="width: 20px; height: 20px"
                          :src="require('@/assets/login/icon_002.png')"
                          fit="contain "
                        ></el-image>
                      </span>
                      <el-input
                        :key="passwordType"
                        ref="password"
                        v-model="loginForm.password"
                        :type="passwordType"
                        placeholder="请输入密码"
                        name="password"
                        tabindex="2"
                        auto-complete="off"
                        @keyup.enter.native="handleLogin"
                      />
                      <span class="show-pwd" @click="showPwd">
                        <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                      </span>
                    </el-form-item>

                    <div class="input-box">
                      <el-form-item prop="codeValue">
                        <span class="svg-container">
                          <el-image
                            style="width: 20px; height: 20px"
                            :src="require('@/assets/login/icon_003.png')"
                            fit="contain "
                          ></el-image>
                        </span>
                        <el-input
                          ref="codeValue"
                          v-model="loginForm.codeValue"
                          placeholder="请输入验证码"
                          name="codeValue"
                          type="text"
                          tabindex="3"
                          auto-complete="off"
                          @keyup.enter.native="handleLogin"
                        />
                      </el-form-item>
                      <div class="code" @click.stop="getCode">
                        <img :src="'data:image/jpeg;base64,' + codeImage" alt="" />
                      </div>
                    </div>

                    <div class="checkbox">
                      <div class="check-yes">
                        <el-image
                          style="width: 16px; height: 16px"
                          :src="require('@/assets/login/icon_004.png')"
                          fit="contain "
                        ></el-image>
                        <el-image
                          v-if="isRemenberPw"
                          class="yes"
                          style="width: 11px; height: 11px"
                          :src="require('@/assets/login/icon_005.png')"
                          fit="contain "
                        ></el-image>
                      </div>
                      <el-checkbox v-model="isRemenberPw">记住账号密码</el-checkbox>
                    </div>

                    <div class="button-container">
                      <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">登录</el-button>
                    </div>
                  </div>
                  <!-- 手机号登录 -->
                  <div v-else class="pas">
                    <el-form-item prop="username" class="input-box">
                      <span class="svg-container">
                        <el-image
                          style="width: 20px; height: 20px"
                          :src="require('@/assets/login/icon_001.png')"
                          fit="contain "
                        ></el-image>
                      </span>
                      <el-input
                        ref="username"
                        v-model="loginForm.username"
                        placeholder="请输入手机号"
                        name="username"
                        type="text"
                        tabindex="1"
                        auto-complete="on"
                      />
                    </el-form-item>
                    <div class="input-box">
                      <el-form-item prop="codeValue">
                        <span class="svg-container">
                          <el-image
                            style="width: 20px; height: 20px"
                            :src="require('@/assets/login/icon_003.png')"
                            fit="contain "
                          ></el-image>
                        </span>
                        <el-input
                          ref="codeValue"
                          v-model="loginForm.codeValue"
                          placeholder="请输入验证码"
                          name="codeValue"
                          type="text"
                          tabindex="3"
                          auto-complete="off"
                          @keyup.enter.native="handleLogin"
                        />
                      </el-form-item>
                      <div class="code" @click.stop="getCode">
                        <img :src="'data:image/jpeg;base64,' + codeImage" alt="" />
                      </div>
                    </div>
                    <div class="input-box">
                      <el-form-item prop="smsCode">
                        <span class="svg-container">
                          <el-image
                            style="width: 20px; height: 20px"
                            :src="require('@/assets/login/icon_003.png')"
                            fit="contain "
                          ></el-image>
                        </span>
                        <el-input
                          ref="smsCode"
                          v-model="loginForm.smsCode"
                          placeholder="请输入短信验证码"
                          name="smsCode"
                          type="text"
                          tabindex="3"
                          auto-complete="off"
                        />
                      </el-form-item>
                      <div class="code2" @click.stop="getSmsCode">
                        <el-button :disabled="countDown != 60">{{
                          countDown == 60 ? getCodeText : '重新获取(' + countDown + 's)'
                        }}</el-button>
                      </div>
                    </div>
                    <div class="button-container">
                      <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">登录</el-button>
                    </div>
                  </div>
                </transition>
              </div>
            </div>
          </div>
        </el-form>
      </div>
      <div class="info">
        <div class="info-item">
          <el-link :href="companyList[0].icpRecordLink" :underline="false" target="_blank">{{
            companyList[0].icpRecord
          }}</el-link>
          <div style="margin: 0 20px" v-if="companyList[0].companyName && companyList[0].icpRecordLink">|</div>
          <el-link :href="companyList[0].pubSecurityRecordLink" :underline="false" target="_blank">
            <el-image style="width: 14px; height: 16px" :src="require('@/assets/login/icon_0001.png')"></el-image>
            {{ companyList[0].pubSecurityRecord }}
          </el-link>
        </div>
        <div>{{ companyList[0].companyName }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { getAdminUserSmsCode, getCode, getCompanyList, getList } from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      console.log(this.loginForm.loginType)
      if (value.length <= 0 && this.loginForm.loginType == 'account') {
        callback(new Error('请输入用户名'))
      } else if (value.length <= 0 && this.loginForm.loginType == 'mobile') {
        callback(new Error('请输入手机号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    const validatesmsCode = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error('请输入短信验证码'))
      } else {
        callback()
      }
    }
    return {
      getCodeText: '获取验证码',
      countDown: 60,
      timer: null,
      imageURL: this.$imageUrl,
      loginForm: {
        username: '',
        password: '',
        code: '',
        codeValue: '',
        smsCode: '',
        loginType: 'account'
      },
      loginRules: {
        username: [{ required: true, trigger: 'change', validator: validateUsername }],
        password: [{ required: true, trigger: 'change', validator: validatePassword }],
        codeValue: [{ required: true, trigger: 'change', validator: validateCode }],
        smsCode: [{ required: true, trigger: 'change', validator: validatesmsCode }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      isRemenberPw: false,
      codeImage: '',
      acitve: 'pas',
      banner: [],
      companyList: []
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 获取缓存信息
    if (localStorage.getItem('supply_login')) {
      let storageData = JSON.parse(localStorage.getItem('supply_login'))
      this.loginForm.username = storageData.username
      this.isRemenberPw = storageData.isRemenberPw
    }
    if (this.isRemenberPw) {
      this.getCookie()
    }

    this.getCode()
    this.getList()
    this.getCompanyList()
  },
  methods: {
    //获取短信验证码
    async getSmsCode() {
      await getAdminUserSmsCode({
        mobile: this.loginForm.username,
        codeValue: this.loginForm.codeValue,
        code: this.loginForm.code
      })
      this.$successMsg('短信已发送')
      this.isDisabled = true
      this.countDown--
      this.timer = setInterval(() => {
        this.countDown--
        if (this.countDown == 0) {
          this.countDown = 60
          this.getCodeText = '重新获取'
          clearInterval(this.timer)
        }
      }, 1000)
    },
    // 获取验证码
    getCode() {
      getCode().then(res => {
        console.log(res)
        this.loginForm.code = res.data.code
        this.codeImage = res.data.pic
      })
    },
    getCompanyList() {
      getCompanyList().then(res => {
        this.companyList = res.data
      })
    },
    getList() {
      getList().then(res => {
        this.banner = res.data
      })
    },
    async hanleTabs(val, type) {
      this.acitve = val
      this.loginForm.loginType = type
      this.$refs.loginForm.resetFields()
      this.loginForm.password = ''
      this.loginForm.username = ''
    },
    // 显示隐藏密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 登录
    handleLogin() {
      // console.log(this.loginForm, "lll");
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              // console.log(this.redirect);
              this.$router.push({ path: this.redirect || '/' })
              console.log(444)
              this.saveUnAndPw()
              this.$store.commit('user/showMessage', 'yes')
              // this.$message.success("登录成功");
              this.loading = false
            })
            .catch(() => {
              console.log(555)
              this.getCode()
              this.loginForm.codeValue = ''
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // 处理账号密码的储存
    saveUnAndPw() {
      let storageData = {
        username: this.loginForm.username,
        isRemenberPw: this.isRemenberPw
      }
      localStorage.setItem('supply_login', JSON.stringify(storageData))

      if (this.isRemenberPw) {
        this.setCookie(this.loginForm.username, this.loginForm.password, 7)
      }
    },

    //设置cookie
    setCookie(c_name, c_pwd, exdays) {
      var exdate = new Date() //获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) //保存的天数
      //字符串拼接cookie
      window.document.cookie = 'supply_username' + '=' + c_name + ';path=/;expires=' + exdate.toGMTString()
      window.document.cookie = 'supply_password' + '=' + c_pwd + ';path=/;expires=' + exdate.toGMTString()
    },

    //读取cookie
    getCookie: function () {
      if (document.cookie.length > 0) {
        var arr = document.cookie.split('; ') //这里显示的格式需要切割一下自己可输出看下
        for (var i = 0; i < arr.length; i++) {
          var arr2 = arr[i].split('=') //再次切割
          //判断查找相对应的值
          if (arr2[0] == 'supply_username') {
            this.loginForm.username = arr2[1] //保存到保存数据的地方
          } else if (arr2[0] == 'supply_password') {
            this.loginForm.password = arr2[1]
          }
        }
      }
    },

    //清除cookie
    clearCookie: function () {
      this.setCookie('', '', -1) //修改2值都为空，天数为负1天就好了
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
$back: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.info-item .el-link--inner {
  color: #ffffff;
}
/* reset element-ui css */
.login-container {
  background: url('~@/assets/login/background.png') center center;
  background-size: cover;
  height: 100vh;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $back;
      height: 47px;
      caret-color: $back;
      border-bottom: none !important;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $cursor inset !important;
        -webkit-text-fill-color: $back !important;
      }
    }
  }
  .el-carousel__arrow--left,
  .el-carousel__arrow--right {
    display: none;
  }
  .carousel {
    border-top-left-radius: 15px;
    border-bottom-left-radius: 15px;
    overflow: hidden;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #454545;
    margin-bottom: 25px !important;
  }

  .el-checkbox__input {
    display: none;
  }
  .el-form-item__error {
    left: 30px;
  }
  .el-form-item__content {
    display: flex;
    align-items: center;
    line-height: 0;
  }
  .show-pwd {
    line-height: 40px;
  }
  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #4684f4db;
  }
  .checkbox {
    display: flex;
    align-items: center;
    margin-left: 1px;
    .check-yes {
      position: relative;
      line-height: 0;
    }
    .yes {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    .el-checkbox {
      margin: 0 !important;
    }
  }
  /* 可以设置不同的进入和离开动画 */
  /* 设置持续时间和动画函数 */
  .slide-fade-enter-active {
    transition: all 0.3s ease;
  }
  .slide-fade-leave-active {
    transition: all 0.8s cubic-bezier(1, 0.5, 0.8, 1);
  }

  .slide-fade-leave-active {
    transform: translateX(10px);
    opacity: 0;
  }
  .wei {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: 30px;
    text-align: center;
    text-align-last: center;
    &-item-tip {
      height: 19px;
      font-size: 20px;
      margin-top: 17px;
    }
    &-item-text {
      height: 12px;
      font-size: 14px;
      margin-top: 17px;
      color: #999;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.info {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  width: 1080px;
  margin-top: 50px;
  line-height: 30px;
  text-align: center;
  text-align-last: center;
  color: #fff;
}
.info-item {
  display: flex;
  align-items: center;
  justify-content: center;
}
.title-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin: 0 30px;
  cursor: pointer;
  font-size: 22px;
  color: #666666;
  line-height: 40px;
  border-bottom: 3px solid #ffffff;
}
.acitve {
  font-weight: bold;
  color: #4684f4;
  border-bottom: 3px solid #4684f4;
}
.flexBox {
  display: flex;
  padding-top: calc(50vh - 230px);
  justify-content: space-between;
  flex-direction: column;
  align-items: center;
}
.input-box {
  position: relative;
  width: 360px;
  height: 50px;
  margin-bottom: 25px;
  background: #ffffff;
  border: 1px solid #e6e6e6 !important;
  border-radius: 4px;
}
.input-box:last-child {
  margin-bottom: 10px;
}
.flex {
  display: flex;
  flex-direction: row;
}
.empty-height {
  height: 46px;
  margin: 15px 0;
}
.logo {
  height: 46px;
  width: 460px;
}
.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.login-container {
  min-height: 100%;
  width: 100%;
  height: 100vh;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 1920px;
    max-width: 100%;
    height: 100%;

    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .image-container {
    width: 700px;
    height: 516px;
    //  overflow: hidden;
  }
  .right-container {
    width: 440px;
    height: 516px;
    .form-container {
      height: 440px;
      padding: 20px 30px 27px 40px;
      background: #fff;
      border-radius: 0 15px 15px 0;
      .title {
        justify-content: center;
        letter-spacing: 4px;
        text-align: center;
        margin-bottom: 35px;
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    margin: 0 0 0 14px;
    // padding: 6px 5px 6px 5px;
    color: #33aef7;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    border-radius: 15px 15px 0 0;
    overflow: hidden;
    img {
      width: 100%;
      display: block;
    }
  }

  .show-pwd {
    position: absolute;
    right: 30px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .code {
    position: absolute;
    right: 30px;
    top: 7px;
    z-index: 99;
    cursor: pointer;
    img {
      height: 30px;
    }
  }
  .code2 {
    position: absolute;
    right: 0;
    top: -1px;
    .el-button {
      height: 50px;
    }
  }

  .button-container {
    text-align: center;
    margin-top: 20px;
    button {
      font-size: 16px;
      width: 100%;
      height: 45px;
      border-radius: 4px;
      background: #4684f4;
      box-shadow: 2px 3px 8px 0px #4684f46b;
    }
  }
}

::v-deep .el-form-item__error {
  color: #f56c6c;
  font-size: 12px;
  line-height: 1;
  padding-top: 6px;
  position: absolute;
  top: 100%;
  left: 0;
}

@media only screen and (max-width: 600px) {
  .image-container {
    display: none;
  }
  .form-container {
    border-radius: 15px !important;
  }
}
</style>
