<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-height2 fiexlay">
                <img v-if="companyList[0] && companyList[0].logo" class="logo" :src="companyList[0].logo"
                  alt />
                {{ companyList[0] && companyList[0].sysName ? companyList[0].sysName : '' }}
              </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="item.imgCarouselUrl" />
                </el-carousel-item>
              </el-carousel>
            </div>
            <div class="right-container">
              <div class="empty-height" />
              <div class="form-container">
                <div class="title">售修宝平台</div>
                <el-form-item prop="username">
                  <span class="svg-container">
                    <svg-icon icon-class="user" />
                  </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">
                  <span class="svg-container">
                    <svg-icon icon-class="password" />
                  </span>
                  <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
                    placeholder="请输入密码" name="password" tabindex="2" auto-complete="off"
                    />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </el-form-item>

                <el-form-item prop="codeValue">
                  <span class="svg-container">
                    <svg-icon icon-class="password" />
                  </span>
                  <el-input ref="codeValue" v-model="loginForm.codeValue" placeholder="请输入验证码" name="codeValue"
                    type="text" tabindex="3" auto-complete="off" />
                  <div class="code" @click="getCode"><img :src="'data:image/jpeg;base64,' + codeImage" alt="" /></div>
                </el-form-item>

                <el-checkbox v-model="isRemenberPw">记住密码</el-checkbox>

                <div class="button-container">
                  <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">登录</el-button>
                </div>
              </div>
            </div>
          </div>
        </el-form>
      </div>
      <div v-if="companyList && companyList.length" class="info">
        <div class="info-item">
          <el-link :href="companyList[0].icpRecordLink" :underline="false" target="_blank">{{
            companyList[0].icpRecord
          }}</el-link>
          <div v-if="companyList[0].companyName && companyList[0].icpRecordLink" style="margin: 0 20px">|</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')" />
            {{ companyList[0].pubSecurityRecord }}
          </el-link>
        </div>
		<div class="info-item">
			<el-link href="https://www.zfire.top/#/" :underline="false" target="_blank">
				{{ companyList[0] && companyList[0].companyName ? companyList[0].companyName : '' }}
			</el-link>
		</div>
		<div class="info-item">
			<el-link href="https://mp.weixin.qq.com" :underline="false" target="_blank">
			  微信公众号&小程序
			</el-link>
			<div style="margin: 0 20px">|</div>
			<el-link href="https://pay.weixin.qq.com/" :underline="false" target="_blank">
			  微信支付商户平台
			</el-link>
		</div>
        <div class="link_gs">
          <span v-if="dictList.length > 0" :underline="false" target="_blank">友链:</span>
          <el-link style="margin-left: 10px" v-for="(v, i) in dictList" :key="i" :href="v.amityUrl" :underline="false"
            target="_blank">
            <el-image style="width: 14px; height: 16px" :src=" v.amityUrlLogo"></el-image>
            {{ v.amityUrlName }}</el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import Cookies from 'js-cookie'
import { getCode, getList, getCompanyList, recordAmityList2 } from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length <= 0) {
        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()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        codeValue: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'change', validator: validateUsername }],
        password: [{ required: true, trigger: 'change', validator: validatePassword }],
        codeValue: [{ required: true, trigger: 'change', validator: validateCode }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      isRemenberPw: false,
      codeImage: '',
      banner: [],
      companyList: [],
      dictList: []
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 获取缓存信息
    if (localStorage.getItem('greemall_login')) {
      let storageData = JSON.parse(localStorage.getItem('greemall_login'))
      this.loginForm.username = storageData.username
      this.isRemenberPw = storageData.isRemenberPw
    }
    if (this.isRemenberPw) {
      this.getCookie()
    }

    this.getCode()
    this.getList()
    this.getCompanyList()
    this.getDictAmityPage()
  },
  methods: {
    // 获取轮播图
    getList() {
      getList().then(res => {
        this.banner = res.data
      })
    },
    //获取友情链接列表
    getDictAmityPage() {
      recordAmityList2().then(res => {
        this.dictList = res.data
      })
    },
    // 获取备案信息
    getCompanyList() {
      getCompanyList().then(res => {
        this.companyList = res.data
      })
    },
    // 获取验证码
    getCode() {
      getCode().then(res => {
        this.loginForm.code = res.data.code
        this.codeImage = res.data.pic
      })
    },

    // 显示隐藏密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.saveUnAndPw()
              this.loading = false
            })
            .catch(() => {
              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('greemall_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 = 'greemall_username' + '=' + c_name + ';path=/;expires=' + exdate.toGMTString()
      window.document.cookie = 'greemall_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] == 'greemall_username') {
            this.loginForm.username = arr2[1] //保存到保存数据的地方
          } else if (arr2[0] == 'greemall_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 {
    position: relative;
    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;

.flex {
  display: flex;
  align-items: center;
}

.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;
  z-index: 999;
}

.link_gs {
  font-size: 14px;
}

::v-deep .link_gs .el-link--inner {
  color: #fff;
  font-weight: initial;
}

.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 {
  font-size: 30px;
  font-weight: bolder;
  height: 46px;
  margin: 15px 0;
  color: #fff;
}

.empty-height2 {
  font-size: 30px;
  font-weight: bolder;
  height: 42px;
  margin: 15px 0;
  color: #fff;
}

.fiexlay {
  width: auto;
  height: 46px;
  display: flex;
  align-items: center;
}

.logo {
  height: 42px;
  margin-right: 5px;
}

.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;
    }
  }
}

.float_right {
  position: absolute;
  right: 0;
  font-size: 14px;
  cursor: pointer;
}

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

.el-link.el-link--default:hover {
  color: #4684f4;
}

@media only screen and (max-width: 600px) {
  .image-container {
    display: none;
  }

  .form-container {
    border-radius: 15px !important;
  }
}
</style>
