<template>
  <el-form
    ref="loginForm"
    :rules="loginRules"
    :model="loginForm"
    class="login-form"
    status-icon
    label-width="0"
  >
    <el-form-item prop="username">
      <el-input
        v-model="loginForm.username"
        size="small"
        auto-complete="new-password"
        placeholder="请输入用户名"
        maxlength="11"
        @keyup.enter.native="handleLogin"
      >
        <i slot="prefix" style="color:#2671E2;" class="icon-yonghuming" />
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        :type="passwordType"
        v-model="loginForm.password"
        size="small"
      auto-complete="new-password"
        placeholder="请输入密码"
        @keyup.enter.native="handleLogin"
      >
        <i
          slot="suffix"
          style="color:#2671E2;"
          class="el-icon-view el-input__icon"
          @click="showPassword"
        />
        <i slot="prefix" style="color:#2671E2;" class="icon-mima"></i>
      </el-input>
    </el-form-item>
    <el-form-item prop="code">
      <Verify
        @success="verifySuccess"
        :mode="'pop'"
        :captchaType="'blockPuzzle'"
        :imgSize="{ width: '330px', height: '155px' }"
        ref="verify"
      />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        size="small"
        style="font-size:16px; background:#2671E2; border-color: #2671E2;"
        class="login-submit"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import { randomLenNum } from "@/util/util";
import { mapGetters } from "vuex";
import { getCode } from "@/api/code";
import Verify from "@/components/verifition/Verify";
import { getStore, setStore } from "@/util/store";
export default {
  name: "Userlogin",
  components: {
    Verify,
  },
  data() {
    return {
      socialForm: {
        code: "",
        state: "",
      },
      info:{},
      loginForm: {
        username: "admin",
        password: "123456",
        code: "",
        randomStr: "blockPuzzle",
      },
      checked: false,
      code: {
        src: undefined,
        len: 4,
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, message: "密码长度最少为6位", trigger: "blur" },
        ],
      },
      passwordType: "password",
    };
  },
  computed: {
    ...mapGetters(["tagWel"]),
  },
  methods: {
    showPassword() {
      this.passwordType == ""
        ? (this.passwordType = "password")
        : (this.passwordType = "");
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$refs.verify.show();
        }
      });
    },
    verifySuccess(params) {
      let that = this;
      this.loginForm.code = params.captchaVerification;
      this.$store.dispatch("LoginByUsername", this.loginForm).then(() => {
        // this.$router.push({ path: this.tagWel.value });
        this.info = getStore({ name: "userInfo" });
        localStorage.setItem('allClose',false)
        that.$api.api.getTab().then((res) => {
          // console.log(res)
          // 循环数组，给定各种标志
          if (res.data.length == 0) {
            // this.$message.warning("您已经被禁用");
            this.$message.warning("账号已禁止登录，请联系管理员恢复");
          } else if (res.data.length == 1) {
            let idx = res.data[0].orgType;
            localStorage.setItem("orgType", idx);
            // 给定一个判断是否可以跳转的东西
            localStorage.setItem("backFlag", false);
            // 当他只有质控后台时，不需要多余操作
            if (idx == 0) {
              // 质控管家后台
              localStorage.setItem("topName", "");
              localStorage.setItem("orgCode", "");
              localStorage.setItem("houseId", "");
              localStorage.setItem("agentId", "");
              that.$router.push({ path: "/wel" });
            }
            // 当他为其他时 先获取是否只有一个选项，然后在来进行判断
            if (idx == 1) {
              this.getAgentList();
              // 代理商
              // that.$router.push({ path: "/chose", query: { flag: 1 } });
            }
            if (idx == 2) {
              this.getCompanyList();
              // 公司后台
              // that.$router.push({ path: "/chose", query: { flag: 2 } });
            }
            if (idx == 3) {
              this.findByUserName();
              // 楼盘后台
              // that.$router.push({ path: "/chose", query: { flag: 3 } });
            }
          } else {
            that.$router.push({ path: "/check" });
          }
        });
      });
    },

        // 获取代理商
    getAgentList() {
      this.$api.api
        .zkagentPage({
          current: 1,
          pageSize: 10,
        })
        .then((res) => {
          // console.log(res);
          // this.list = res.data.records;
          // this.total = res.data.total;
          if (res.data.total == 0) {
            this.$message.warning("您当前并未绑定代理商");
            return;
          } else if (res.data.total > 1) {
            // 公司后台
            this.$router.push({ path: "/chose", query: { flag: 1 } });
          } else {
            if(res.data.records[0].lockFlag!=0){
              this.$message.warning("您已经被禁用");
              return
            }
            this.$router.push({ path: "/wel" });
            localStorage.setItem("topName", res.data.records[0].agentName);
            localStorage.setItem("agentId", res.data.records[0].id);
            localStorage.setItem("orgCode", "");
            localStorage.setItem("houseId", "");
          }
        });
    },
    getCompanyList() {
      this.$api.api
        .zkorg({
          current: 1,
          size: 10,
          name: this.companyName,
          orgType: localStorage.getItem("orgType"),
        })
        .then((res) => {
          // console.log(res);
          // this.list = res.data.records;
          // this.total = res.data.total;
          if (res.data.total == 0) {
            this.$message.warning("您当前并未绑定楼盘");
            return;
          } else if (res.data.total > 1) {
            // 公司后台
            this.$router.push({ path: "/chose", query: { flag: 2 } });
          } else {
            if(res.data.records[0].lockFlag!=0){
              this.$message.warning("您已经被禁用");
              return
            }
            this.$router.push({ path: "/wel" });
            localStorage.setItem("topName", res.data.records[0].name);
            localStorage.setItem("orgCode", res.data.records[0].orgCode);
            localStorage.setItem("agentId", "");
            localStorage.setItem("houseId", "");
          }
        });
    },
    findByUserName() {
      this.$api.api
        .findByUserName({
          username: this.info.username,
        })
        .then((res) => {
          // console.log(res);
          this.info.selectHouseType = res.selectHouseType;
          // 获取楼盘
          this.zkhousePage();
        });
    },
    zkhousePage() {
      let obj = {
        current: 1,
        size: 10,
        orgType: localStorage.getItem("orgType"),
      };
      if (this.info.selectHouseType == 1) {
        this.$api.api.zkhousePage(obj).then((res) => {
          // console.log(res)
          // this.list = res.data.records;
          // this.total = res.data.total;
          if (res.data.total == 0) {
            this.$message.warning("您当前并未绑定楼盘");
            return;
          } else if (res.data.total > 1) {
            // 公司后台
            this.$router.push({ path: "/chose", query: { flag: 3 } });
          } else {
            if(res.data.records[0].lockFlag!=0){
              this.$message.warning("您已经被禁用");
              return
            }
            this.$router.push({ path: "/wel" });
            localStorage.setItem("topName", res.data.records[0].propertyName);
            localStorage.setItem("orgCode", res.data.records[0].orgCode);
            localStorage.setItem("agentId", "");
            localStorage.setItem("houseId", res.data.records[0].id);
            this.addLoginCount(res.data.records[0].id)
          }
        });
      }
      if (this.info.selectHouseType == 2) {
        this.$api.api
          .findHouseByArea({
            id: null,
            ...obj,
          })
          .then((res) => {
            // console.log(res)
            // this.list = res.data.records;
            // this.total = res.data.total;
            if (res.data.total == 0) {
              this.$message.warning("您当前并未绑定楼盘");
              return;
            } else if (res.data.total > 1) {
              // 公司后台
              this.$router.push({ path: "/chose", query: { flag: 3 } });
            } else {
              if(res.data.records[0].lockFlag!=0){
              this.$message.warning("您已经被禁用");
              return
            }
              this.$router.push({ path: "/wel" });
              localStorage.setItem("topName", res.data.records[0].propertyName);
              localStorage.setItem("orgCode", res.data.records[0].orgCode);
              localStorage.setItem("houseId", res.data.records[0].id);
              localStorage.setItem("agentId", "");
              this.addLoginCount(res.data.records[0].id)
            }
          });
      }
    },
      addLoginCount(houseId){
      this.$api.api.addLoginCount({
        houseId
      }).then(res=>{
        console.log(res);
      })
    },
  },
};
</script>

<style>
</style>
