<template>
  <div class="setting login-body">
    <div class="login-form">
      <h2 class="login-form-title">设置</h2>
      <el-form ref="setForm" :model="setModel" :rules="setRules" inline>
        <h3 class="mb-1">服务器IP地址：</h3>
        <el-form-item prop="ipCont1">
          <el-input-number
            style="width:60px;"
            v-model="setModel.ipCont1"
            :min="0"
            :max="255"
            :step="1"
            step-strictly
            :controls="false"
          ></el-input-number>
        </el-form-item>
        <span class="ip-split">—</span>
        <el-form-item prop="ipCont2">
          <el-input-number
            style="width:60px;"
            v-model="setModel.ipCont2"
            :min="0"
            :max="255"
            :step="1"
            step-strictly
            :controls="false"
          ></el-input-number>
        </el-form-item>
        <span class="ip-split">—</span>
        <el-form-item prop="ipCont3">
          <el-input-number
            style="width:60px;"
            v-model="setModel.ipCont3"
            :min="0"
            :max="255"
            :step="1"
            step-strictly
            :controls="false"
          ></el-input-number>
        </el-form-item>
        <span class="ip-split">—</span>
        <el-form-item prop="ipCont4">
          <el-input-number
            style="width:60px;"
            v-model="setModel.ipCont4"
            :min="0"
            :max="255"
            :step="1"
            step-strictly
            :controls="false"
          ></el-input-number>
        </el-form-item>
        <span class="ip-split">:</span>
        <el-form-item prop="port">
          <el-input-number
            style="width:60px;"
            v-model="setModel.port"
            :min="1"
            :max="1000000"
            :step="1"
            step-strictly
            :controls="false"
          ></el-input-number>
        </el-form-item>
        <br />
        <h3 class="mb-1">学校编号：</h3>
        <el-form-item prop="schoolCode">
          <el-input v-model="setModel.schoolCode" clearable></el-input>
        </el-form-item>

        <br />
        <el-form-item>
          <el-button
            type="primary"
            :disabled="loading"
            size="large"
            @click="submit"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import db from "@/plugins/db";

export default {
  name: "setting",
  data() {
    const contValidator = [
      {
        required: true,
        message: " ",
        trigger: "change"
      }
    ];
    return {
      setModel: {
        ipCont1: undefined,
        ipCont2: undefined,
        ipCont3: undefined,
        ipCont4: undefined,
        port: undefined,
        schoolCode: ""
      },
      setRules: {
        ipCont1: [...contValidator],
        ipCont2: [...contValidator],
        ipCont3: [...contValidator],
        ipCont4: [...contValidator],
        port: [...contValidator],
        schoolCode: [...contValidator]
      },
      loading: false
    };
  },
  computed: {
    domainEdited() {
      return `http://${this.setModel.ipCont1}.${this.setModel.ipCont2}.${this.setModel.ipCont3}.${this.setModel.ipCont4}:${this.setModel.port}`;
    },
    domain() {
      return this.$store.state.domain;
    },
    schoolCode() {
      return this.$store.state.schoolCode;
    }
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      if (!this.domain) {
        this.setModel = {
          ipCont1: undefined,
          ipCont2: undefined,
          ipCont3: undefined,
          ipCont4: undefined,
          port: undefined,
          schoolCode: this.schoolCode
        };
        return;
      }

      const [ipCont1, ipCont2, ipCont3, ipCont4, port] = this.domain
        .replace("http://", "")
        .split(/\.|:/);

      this.setModel = {
        ipCont1: Number(ipCont1),
        ipCont2: Number(ipCont2),
        ipCont3: Number(ipCont3),
        ipCont4: Number(ipCont4),
        port: Number(port),
        schoolCode: this.schoolCode
      };
    },
    async submit() {
      if (this.loading) return;
      this.loading = true;

      const valid = await this.$refs.setForm.validate().catch(() => {});
      if (!valid) {
        this.loading = false;
        return;
      }

      this.$store.commit("setDomain", this.domainEdited);
      let res1 = await db.setDict("domain", this.domainEdited).catch(() => {});
      let res2 = await db
        .setDict("schoolCode", this.setModel.schoolCode)
        .catch(() => {});

      if (res1 && res2) {
        this.$ls.set("domain", this.domainEdited);
        this.$store.commit("setDomain", this.domainEdited);
        this.$ls.set("schoolCode", this.setModel.schoolCode);
        this.$store.commit("setSchoolCode", this.setModel.schoolCode);
        this.$message.success("设置成功！");
        this.$router.go(-1);
      } else {
        this.$message.error("保存失败！");
      }
    }
  }
};
</script>
