<template>
  <el-container>
    <el-main class="el-main-padding">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="200px"
        class="demo-ruleForm"
        :inline-message="true"
      >
        <el-form-item v-if="isSuperAdmin" label="学校">
          <el-select
            v-model="ruleForm.orgId"
            placeholder="请选择"
            style="width: 180px"
          >
            <el-option
              v-for="item in rootOrgList"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="是否使用第三方登录"
          prop="STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY"
        >
          <el-checkbox
            v-model="ruleForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY"
          ></el-checkbox>
        </el-form-item>
        <el-form-item
          label="第三方登录页地址"
          prop="STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL"
        >
          <el-input
            :disabled="!ruleForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY"
            v-model="ruleForm.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL"
            placeholder="请输入第三方登录页地址,以http://或https://开头"
            class="input-width"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否展示我司logo" prop="SHOW_QMTH_LOGO">
          <el-checkbox v-model="ruleForm.SHOW_QMTH_LOGO"></el-checkbox>
        </el-form-item>
        <el-form-item
          label="是否展示APP下载二维码"
          prop="SHOW_STUDENT_CLIENT_APP_QRCODE"
        >
          <el-checkbox
            v-model="ruleForm.SHOW_STUDENT_CLIENT_APP_QRCODE"
          ></el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            :disabled="btnSaveDiabled"
            type="primary"
            @click="submitForm('ruleForm')"
            >保 存</el-button
          >
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>
<script>
import { mapState } from "vuex";
import { CORE_API } from "@/constants/constants.js";
export default {
  data() {
    var validateUrl = (rule, value, callback) => {
      var ass = this.ruleForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY;
      var reg = /^(http:\/\/|https:\/\/){1}.*$/;
      if (ass && !value) {
        return callback(new Error("请输入第三方登录页地址"));
      } else if (ass && !value.match(reg)) {
        return callback(new Error("url以http://或https://开头"));
      } else {
        callback();
      }
    };
    return {
      rootOrgList: [],
      propertyGroupId: "",
      formDataChanged: false,
      originalRuleForm: {},
      ruleForm: {
        relatedPropertyGroupIdList: [],
        orgId: null,
        STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY: false,
        SHOW_STUDENT_CLIENT_APP_QRCODE: false,
        STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL: "",
        SHOW_QMTH_LOGO: false,
        properties: {
          STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY: false,
          SHOW_STUDENT_CLIENT_APP_QRCODE: false,
          STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL: "",
          SHOW_QMTH_LOGO: false
        }
      },
      rules: {
        STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL: [
          {
            validator: validateUrl,
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.ruleForm.properties.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY = this.ruleForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY;
          this.ruleForm.properties.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL = this.ruleForm.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL;
          this.ruleForm.properties.SHOW_QMTH_LOGO = this.ruleForm.SHOW_QMTH_LOGO;
          this.ruleForm.properties.SHOW_STUDENT_CLIENT_APP_QRCODE = this.ruleForm.SHOW_STUDENT_CLIENT_APP_QRCODE;

          this.$httpWithMsg
            .put(CORE_API + "/org/saveOrgProperties", this.ruleForm)
            .then(
              () => {
                this.$notify({
                  message: "保存成功",
                  type: "success"
                });
                this.originalRuleForm = Object.assign({}, this.ruleForm);
                this.formDataChanged = false;
              },
              () => {}
            );
        } else {
          return false;
        }
      });
    },

    initForm() {
      this.ruleForm.relatedPropertyGroupIdList = ["studentClientConfig"];
      var url =
        CORE_API +
        "/org/getOrgPropertiesByGroupWithoutCache/" +
        this.ruleForm.orgId +
        "/" +
        this.propertyGroupId;
      this.$httpWithMsg.get(url).then(response => {
        if (response) {
          this.ruleForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY =
            response.data.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY == "true";
          this.ruleForm.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL =
            response.data.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL;
          this.ruleForm.SHOW_QMTH_LOGO = response.data.SHOW_QMTH_LOGO == "true";
          this.ruleForm.SHOW_STUDENT_CLIENT_APP_QRCODE =
            response.data.SHOW_STUDENT_CLIENT_APP_QRCODE == "true";

          this.originalRuleForm = Object.assign({}, this.ruleForm);
        } else {
          this.$notify({
            message: "学校设置信息暂未初始化，请立即初始化",
            type: "warning"
          });
        }
      });
    }
  },
  created() {
    this.ruleForm.orgId = this.user.rootOrgId;
    this.propertyGroupId = "studentClientConfig4Edit";
    if (this.isSuperAdmin) {
      this.$httpWithMsg.get(CORE_API + "/org/getRootOrgList").then(response => {
        this.rootOrgList = response.data;
      });
    }
    this.initForm();
  },
  watch: {
    "ruleForm.orgId": {
      handler: function() {
        this.initForm();
      }
    },
    "ruleForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY": {
      handler: function(val) {
        this.$refs["ruleForm"].validate();
        if (!val) {
          this.ruleForm.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL = null;
        }
      }
    },
    ruleForm: {
      deep: true,
      handler: function(newForm) {
        if (Object.keys(this.originalRuleForm).length > 0) {
          this.formDataChanged = !(
            newForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY ==
              this.originalRuleForm.STUDENT_CLIENT_ACCESS_FROM_THIRD_PARTY &&
            newForm.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL ==
              this.originalRuleForm.STUDENT_CLIENT_THIRD_PARTY_LOGIN_URL &&
            newForm.SHOW_QMTH_LOGO == this.originalRuleForm.SHOW_QMTH_LOGO &&
            newForm.SHOW_STUDENT_CLIENT_APP_QRCODE ==
              this.originalRuleForm.SHOW_STUDENT_CLIENT_APP_QRCODE
          );
        } else {
          this.formDataChanged = false;
        }
      }
    }
  },
  computed: {
    ...mapState({ user: state => state.user }),
    btnSaveDiabled() {
      console.log(this.formDataChanged);
      return !this.formDataChanged;
    },
    isSuperAdmin() {
      return this.user.roleList.some(role => role.roleCode == "SUPER_ADMIN");
    }
  }
};
</script>
<style scoped>
.input-width {
  width: 638px;
}
</style>
