<script>
const { ipcRenderer } = require("electron");
import { mapState } from "vuex";

// 状态更新时发送
export default {
  data() {
    return {
      rules: {
        factoryCode: [
          { message: "请输入工厂编码", required: true, trigger: "blur" },
        ],
        account: [{ message: "请输入账号", required: true, trigger: "blur" }],
        password: [{ message: "请输入密码", required: true, trigger: "blur" }],
      },
      userList: [],
      authorityVisible: false,

      remember: true,
      authorityForm: {},
      form: {
        factoryCode: "",
        account: "",
        password: "",
      },
      desktoVersion: "print",
    };
  },
  computed: {
    ...mapState(["grid"]),
  },
  created() {
    this.$store.commit("getNewGrid");
    this.userList = this.$dataStore.get("userList") || [];
    ipcRenderer.on("window-size", (event, size) => {
      const { width, height } = size;
      console.log(`接收到窗口尺寸：${width}x${height}`);
      let proportion;
      if (height > 1000) {
        proportion = 1.4;
      } else {
        proportion = 0.8;
      }
      this.$store.commit("setWindows", { width, height });
      this.$store.commit("setWHproportion", proportion);
      this.$store.commit("setGrid");
    });
  },
  mounted() {
    document.addEventListener("keyup", this.keyUp);
  },
  beforeDestroy() {
    document.removeEventListener("keyup", this.keyUp);
    console.log(333);
  },
  methods: {
    keyUp(e) {
      if (e.keyCode === 13) {
        this.login();
      }
    },

    async login() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          let f = JSON.parse(JSON.stringify(this.form));
          let { data } = await this.$api.post("/factory/login", f);
          this.$dataStore.set("user", {
            ...data.sysUser,
            ...{ token: data.token },
          });
          // console.log(this.$dataStore);
          if (this.remember) {
            let userList = this.$dataStore.get("userList");

            if (userList) {
              if (
                !userList.find(
                  (el) =>
                    el.account === f.account &&
                    el.factoryCode === f.factoryCode,
                )
              ) {
                userList.push(f);
              }
            } else {
              userList = [f];
            }
            this.$dataStore.set("userList", userList);
          }

          await this.$router.push("/design");
          this.$store.commit("setDefaultSetting");
        }
      });
    },
    userChange(v) {
      this.form.account = this.userList[v].account;
      this.form.factoryCode = this.userList[v].factoryCode;
      this.form.password = this.userList[v].password;
    },
  },
};
</script>

<template>
  <div class="login">
    <div class="form-content">
      <el-form ref="formRef" :rules="rules" size="small" :model="form">
        <el-form-item v-if="userList.length > 0">
          <el-select
            @change="userChange"
            filterable
            style="width: 100%"
            placeholder="登录记录"
            v-model="form.user"
            clearable
          >
            <template slot="prefix">
              <i class="el-icon-monitor"></i>
            </template>
            <el-option
              v-for="(it, i) in userList"
              :key="i"
              :label="`${it.account}(${it.factoryCode})`"
              :value="i"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="factoryCode">
          <el-input
            placeholder="工厂编码"
            prefix-icon="el-icon-s-shop"
            v-model="form.factoryCode"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="account">
          <el-input
            placeholder="账号"
            prefix-icon="el-icon-user"
            v-model="form.account"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            show-password
            prefix-icon="el-icon-lock"
            type="password"
            v-model="form.password"
            clearable
          ></el-input>
        </el-form-item>
        <div class="check">
          <el-checkbox v-model="remember">记住账号</el-checkbox>
        </div>
        <el-form-item>
          <div style="display: flex; justify-content: center">
            <el-button
              @keyup.enter.native="login"
              @click="login"
              type="primary"
              style="width: 60%"
              >登录</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="less" scoped>
.check {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 15px;
}

.login {
  width: 100%;
  height: 100%;
  background-image: url("../../assets/logo-bg.jpg");
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-content {
  width: 300px;
  padding-top: 50px;
}

.el-icon-monitor {
  position: relative;
  left: 6px;
}
</style>
