<template>
  <div id="login">
    <div class="login-left">
      <img
        style="width: 100%; height: 100%"
        src="https://smart-1251449951.cos.ap-guangzhou.myqcloud.com/iot_breed/farm/2024/08/29fc08b88c50e6f459cbb082e03fa356b6.jpg"
        alt="logo"
      />
    </div>
    <div class="login-right">
      <div class="login-right-content">
        <div class="login-right-box">
          <div class="login-right-icon">
            <h1 class="title">基芯农分子育种检测平台</h1>
          </div>
          <div class="login-right-form">
            <div class="tab-box">
              <p id="box1" class="p1" @click="switchClick('accountLogin')">
                账号登录
              </p>
              <p id="box2" @click="switchClick('phoneLogin')">手机号登录</p>
            </div>
            <el-form
              :model="formData"
              status-icon
              ref="ruleForm"
              class="demo-ruleForm"
              :rules="rules"
            >
              <el-form-item prop="account">
                <el-input
                  placeholder="请输入账号"
                  prefix-icon="el-icon-user"
                  v-model="formData.account"
                  autocomplete="off"
                >
                </el-input>
              </el-form-item>
              <el-form-item
                prop="password"
                v-if="loginStatus === 'accountLogin'"
              >
                <el-input
                  type="password"
                  placeholder="请输入密码"
                  prefix-icon="el-icon-unlock"
                  v-model="formData.password"
                  show-password
                >
                </el-input>
              </el-form-item>
              <el-form-item
                prop=""
                class="code-box"
                v-if="loginStatus === 'phoneLogin'"
              >
                <!-- 3\\手机验证码登录 -->
                <el-input
                  placeholder="请输入验证码"
                  prefix-icon="el-icon-circle-check"
                  v-model="formData.code"
                >
                </el-input>
                <!-- 获取验证码 -->
                <el-button
                  class="diaojishi"
                  type="success"
                  size="mini"
                  style="width: 100px; color: '#91c780'"
                  :disabled="sendModel.countFlag"
                  @click="sendClick('3')"
                >
                  {{
                    sendModel.btnMsg == null
                      ? sendModel.countNum + 's后重新发送'
                      : sendModel.btnMsg
                  }}
                </el-button>
              </el-form-item>
              <div class="other mb20px">
                <p>
                  <el-checkbox
                    v-if="loginStatus === 'accountLogin'"
                    v-model="ISremember"
                    @change="rememberClick"
                    >自动登录</el-checkbox
                  >
                </p>
                <p>
                  <el-link @click="handlerClick('register')"
                    ><span style="color: black">注册账号</span></el-link
                  >/<el-link @click="handlerClick('forgotpwd')"
                    ><span style="color: black">忘记密码</span></el-link
                  >
                </p>
              </div>
              <el-form-item>
                <!-- 账号密码登录 -->
                <el-button
                  type="success"
                  :loading="flag"
                  @click="loginIn('ruleForm')"
                  v-if="loginStatus === 'accountLogin'"
                  >登录</el-button
                >
                <!-- 验证码登录 -->
                <el-button
                  type="success"
                  :loading="flag"
                  @click="loginIn('ruleForm')"
                  v-if="loginStatus === 'phoneLogin'"
                  >登录</el-button
                >
              </el-form-item>
              <div style="text-align: right">
                <el-link
                  @click="schemeClick"
                  style="color: #409eff; letter-spacing: 2px"
                >
                  <i
                    style="color: #409eff; font-size: 14px; font-size: 12px"
                    class="el-icon-question"
                  ></i>
                  <span style="color: #409eff">获取分子育种检测方案</span>
                </el-link>
              </div>
              <!-- 下面的绑定与联系我们 -->
            </el-form>
          </div>
        </div>
      </div>
      <div class="login-right-footer">
        <div class="login-right-footer-box">
          <span>隐私</span>
          <el-divider direction="vertical"></el-divider>
          <span>条款</span>
        </div>
      </div>
    </div>

    <!-- 注册账号与电信绑定与忘记密码 -->
    <el-dialog
      :title="
        status === 'register'
          ? '注册账号'
          : status === 'forgotpwd'
          ? '忘记密码'
          : '天翼账号绑定'
      "
      :visible.sync="commondialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form
        :model="commonForm"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <div
          v-if="status === 'skywing'"
          style="margin: 0 0 20px 30px; letter-spacing: 2px"
        >
          <el-alert
            title="电信用户首次登陆需要绑定手机号,绑定成功后可通过手机号登录系统"
            type="warning"
            :closable="false"
          >
          </el-alert>
        </div>
        <el-form-item label="天翼账号" v-if="status === 'skywing'">
          <el-input
            v-model="commonForm.aepLoginUser"
            placeholder="请输入天翼账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="commonForm.mobile"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="验证码"
          prop=""
          class="code-box"
          v-if="status === 'skywing' || status === 'register'"
        >
          <el-input placeholder="请输入验证码" v-model="commonForm.code">
          </el-input>
          <el-button
            class="diaojishi"
            type="success"
            size="mini"
            style="width: 100px; color: '#91c780'"
            :disabled="sendModel.countFlag"
            @click="sendClick('1')"
          >
            {{
              sendModel.btnMsg == null
                ? sendModel.countNum + 's后重新发送'
                : sendModel.btnMsg
            }}
          </el-button>
        </el-form-item>
        <el-form-item
          label="验证码"
          prop=""
          class="code-box"
          v-if="status === 'forgotpwd'"
        >
          <el-input placeholder="请输入验证码" v-model="commonForm.code">
          </el-input>
          <el-button
            class="diaojishi"
            type="success"
            size="mini"
            style="width: 100px; color: '#91c780'"
            :disabled="sendModel.countFlag"
            @click="sendClick('2')"
          >
            {{
              sendModel.btnMsg == null
                ? sendModel.countNum + 's后重新发送'
                : sendModel.btnMsg
            }}
          </el-button>
        </el-form-item>
        <!-- 注册 -->
        <el-form-item
          label="密码"
          v-if="status === 'forgotpwd' || status === 'register'"
        >
          <el-input
            placeholder="请输入密码"
            v-model="commonForm.password"
            show-password
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="commondialogVisible = false" size="mini"
          >取 消</el-button
        >
        <el-button
          v-if="status === 'register'"
          type="success"
          @click="commondialogVisible = false"
          size="mini"
          >确 定</el-button
        >
        <el-button
          v-if="status === 'forgotpwd'"
          type="success"
          @click="commondialogVisible = false"
          size="mini"
          >确 定</el-button
        >
        <el-button
          v-if="status === 'skywing'"
          type="primary"
          @click="commondialogVisible = false"
          size="mini"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 联系我们 -->
    <el-dialog
      title="了解分子育种检测方案"
      :visible.sync="dialogVisiblecontact"
      width="35%"
    >
      <el-form
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <div
          style="
            margin-bottom: 20px;
            padding-left: 20px;
            box-sizing: border-box;
          "
        >
          <el-alert
            title="我们可以提供完整的分子育种解决方案，如果您有智慧育种、选配、亲缘关系鉴定等需求或者合作意向，可以通过下方填写提交您的联系方式及备注信息，我们会第一时间联系您，为您提供相关业务方案和系统体验账号。"
            type="warning"
            :closable="false"
          >
          </el-alert>
        </div>
        <el-form-item label="您的称呼">
          <el-input
            placeholder="请输入您的称呼"
            v-model="ruleForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            placeholder="请输入手机号"
            v-model="ruleForm.mobile"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input
            placeholder="请输入邮箱(非必填)"
            v-model="ruleForm.email"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="ruleForm.comments"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblecontact = false" size="mini"
          >取 消</el-button
        >
        <el-button
          type="success"
          size="mini"
          @click="dialogVisiblecontact = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 选择主体 -->
    <!-- <el-dialog
      title="选择养殖场"
      :visible.sync="dialogVisible"
      width="25%"
      center
    >
      <div class="center-box">
        <el-select v-model="formData.farmInfo" placeholder="选择养殖场">
          <el-option
            v-for="item in farmList"
            :key="item.id"
            :label="item.org_name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="LoginSubmit" size="mini"
          >确 定</el-button
        >
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
import local from '../../utils/local' // local
import {
  Login,
  LoginEnter,
  sendsms,
  phoneLogin,
  register,
  tianyiregister,
  forgots
} from '../../api/consumer'

// 引入
const Base64 = require('js-base64').Base64
export default {
  data() {
    var contactPhone = (rule, value, callback) => {
      if (value) {
        // const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        const reg =
          /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    return {
      token: '', // token,
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { validator: contactPhone, required: true, trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      formData: {
        account: '18164041002',
        password: '123456',
        farmInfo: '',
        code: '214973'
      },
      flag: false, // 按钮加载动画
      dialogVisible: false, // 主体
      object: {}, // 拿到养殖场列表
      farmList: [], // 回显信息
      ISremember: false, // 记住密码
      shortMsg: '获取验证码',
      loginStatus: 'accountLogin', // 默认账号密码登录
      sendModel: {
        // 倒计时周期
        countNum: 60,
        // 用于倒计时标记，true-正在倒计时
        countFlag: false,
        // 定时器
        intervalBtn: {},
        // 默认按钮的值
        btnMsg: '获取验证码'
      },
      // 联系我们
      dialogVisiblecontact: false,
      ruleForm: {
        name: '',
        mobile: '',
        email: '',
        comments: ''
      },
      // 电信-账号-忘记密码
      commondialogVisible: false,
      commonForm: {
        mobile: '',
        code: '',
        password: '',
        cate: '2', // 1自有2养殖3保险4金融5政府6集团
        aepLoginUser: '' // 天翼账号
      },
      status: '', // 账号-密码-注册
      statusCode: ''
    }
  },
  created() {
    var account = local.get('account_sd')
    var password = Base64.decode(local.get('password'))
    // 如果存在赋值给表单，并且将记住密码勾选
    if (account) {
      this.formData.account = account
      this.formData.password = password
      this.ISremember = true
    }
  },
  methods: {
    // 账号与手机号切换
    switchClick(status) {
      // 样式调整
      if (status === 'accountLogin') {
        document.querySelector('#box1').classList.add('p1')
        document.querySelector('#box2').classList.remove('p1')
      } else if (status === 'phoneLogin') {
        document.querySelector('#box1').classList.remove('p1')
        document.querySelector('#box2').classList.add('p1')
      }
      this.loginStatus = status
    },
    // 发送验证码
    async sendClick(type) {
      // 定义一个变量
      var account = ''
      if (type === '3') {
        if (!this.formData.account) {
          this.$message('请输入手机号')
          return
        } else {
          account = this.formData.account
        }
      } else {
        if (!this.commonForm.mobile) {
          this.$message('请输入手机号')
          return
        } else {
          account = this.commonForm.mobile
        }
      }
      const res = await sendsms({
        mobile: account,
        type: type // 短信类型 1注册 2找回 3登录
      })
      if (res.status === 1) {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        // 设置btn倒计时时显示的信息
        this.sendModel.btnMsg = null
        // 更改btn状态
        this.sendModel.countFlag = !this.sendModel.countFlag
        // 设置倒计时
        this.sendModel.intervalBtn = setInterval(() => {
          if (this.sendModel.countNum <= 0) {
            // 重置btn提示信息
            this.sendModel.btnMsg = '获取验证码'
            // 清除定时器
            clearInterval(this.sendModel.intervalBtn)
            // 更改btn状态
            this.sendModel.countFlag = !this.sendModel.countFlag
            // 重置倒计时状态
            this.sendModel.countNum = 60
          }
          // 倒计时
          this.sendModel.countNum--
        }, 1000)
      } else {
        this.$message.error(res.msg)
      }
    },
    // 提交手机号登录
    sendLoginClick(formName) {
      this.flag = true
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (!this.formData.code) {
            this.$message.error('请输入验证码')
            return
          }
          const res = await phoneLogin({
            mobile: this.formData.account,
            code: this.formData.code
          })
          setTimeout(() => {
            this.flag = false
          }, 1000)
          if (res.status === 1) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.farmList = res.data.list // 数据
            this.dialogVisible = true
            this.$message('请选择主体信息')
          } else {
            this.$message.error(res.msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 第一次,登录
    loginIn() {
      let res = {
        data: {
          token: 'fe8a527b65629d250a3a02cf5cd57cb8'
        }
      }
      this.token = res.data.token // 暂时
      // 存储
      local.set('token_sd', this.token)
      local.set('account_sds', this.formData.account)
      local.set('farm_name', '爱农养殖端')
      this.$message({
        type: 'success',
        message: '登录成功!'
      })
      if (local.get('token_sd')) {
        setTimeout(() => {
          this.$router.push('/index/index')
        }, 1500)
      }
    },
    // 记住
    rememberClick(e) {
      this.ISremember = e
      if (!this.ISremember) {
        local.set('account', '')
        const passWord = Base64.encode('')
        local.set('password', passWord)
      }
    },
    // 联系我们
    schemeClick() {
      this.dialogVisiblecontact = true
    },
    // 联系我们-提交
    handlescheme() {
      if (!this.ruleForm.mobile) {
        this.$message.error('请输入手机号')
        return
      }
      var xhr = new XMLHttpRequest()
      xhr.open('post', 'https://www.aiotagro.com/?a=contact')
      xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
      xhr.send(
        `name=${this.ruleForm.name}&mobile=${this.ruleForm.mobile}&email=${this.ruleForm.email}&comments=${this.ruleForm.comments}`
      )
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          var res = JSON.parse(xhr.responseText)
          if (res.status === 1) {
            alert(res.message)
          } else {
            alert(res.message)
          }
        }
      }
      this.dialogVisiblecontact = false
    },
    // 电信与注册与忘记密码
    handlerClick(status) {
      this.status = status // 状态
      this.commondialogVisible = true
    },
    // 提交
    async commonsubmit(status) {
      if (!this.commonForm.mobile) {
        this.$message.error('请输入手机号')
        return
      } else if (!this.commonForm.code) {
        this.$message.error('请输入验证码')
        return
      }
      if (status === 'register') {
        if (!this.commonForm.password) {
          this.$message.error('请输入密码')
          return null
        }
        this.registerok()
      } else if (status === 'forgotpwd') {
        if (!this.commonForm.password) {
          this.$message.error('请输入密码')
          return null
        }
        this.forgotpwdok()
      } else if (status === 'skywing') {
        if (!this.commonForm.aepLoginUser) {
          this.$message.error('请输入天翼账号')
          return null
        }
        this.skywingok()
      }
    },
    // 注册电信
    async registerok() {
      const res = await register({
        mobile: this.commonForm.mobile,
        code: this.commonForm.code,
        password: this.commonForm.password,
        cate: '2'
      })
      if (res.status === 1) {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.commondialogVisible = false
      } else {
        this.$message.error(res.msg)
      }
    },
    // 绑定账号
    async skywingok() {
      const res = await tianyiregister({
        aepLoginUser: this.commonForm.aepLoginUser,
        mobile: this.commonForm.mobile,
        code: this.commonForm.code,
        cate: '2'
      })
      if (res.status === 1) {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.commondialogVisible = false
      } else {
        this.$message.error(res.msg)
      }
    },
    // 找回密码
    async forgotpwdok() {
      const res = await forgots({
        mobile: this.commonForm.mobile,
        code: this.commonForm.code,
        password: this.commonForm.password
      })
      if (res.status === 1) {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.commondialogVisible = false
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>
<style lang="less" scoped>
#login {
  height: 100%;
  width: 100%;
  display: flex;
  background-color: #fff;
  .login-left {
    width: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .login-right {
    width: 50%;
    min-width: 500px;
    height: 100%;
    display: flex;
    flex-direction: column;
    .login-right-content {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      .login-right-box {
        width: 50%;
        min-width: 500px;
        .login-right-icon {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          .title {
            margin-bottom: 30px;
            letter-spacing: 4px;
          }
        }
        .login-right-form {
          margin: 20px 0 30px 0;
          .other {
            display: flex;
            align-items: center;
            justify-content: space-between;
          }
          /deep/.el-button--success {
            width: 100%;
          }
        }
      }
    }

    // 页脚
    .login-right-footer {
      height: 200px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      letter-spacing: 2px;
      color: #949494;
      font-size: 13px;
      .login-right-footer-box {
        display: flex;
        align-items: center;
        justify-content: space-around;
        margin-bottom: 20px;
      }
    }
  }
}
.center-box {
  display: flex;
  align-content: center;
  justify-content: center;
}
// 弹层--header
/deep/.el-dialog__header {
  font-weight: bold;
  box-shadow: 0 0.2px 1px rgba(0, 0, 0, 0.12), 0 0 4px rgba(0, 0, 0, 0.04);
}
/deep/ .el-dialog__body {
  padding: 20px 31px 20px 4px;
}
.code-box {
  position: relative;
  overflow: hidden;
}
.diaojishi {
  position: absolute;
  right: 1px;
  top: 1px;
  height: 38px;
  z-index: 10;
}
.tab-box {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  letter-spacing: 2px;
  font-size: 19px;
  font-weight: bold;
  p {
    text-align: center;
    width: 50%;
    color: #000000;
    line-height: 60px;
    border-bottom: 2px solid transparent;
  }
  .p1 {
    border-bottom: 2px solid #90c221;
    color: #90c221;
  }
}
</style>
