<template>
  <div class="systemConfig" v-loading="loading">
    <div ref="leftContainer" class="left" style="padding: 20px;">
      <el-form :inline="true" :model="form" :rules="rules" ref="systemConfigForm"
        style="display: flex;align-items: center;justify-content: space-between;flex-wrap: wrap;">
        <h3>{{ $t("system:config:BasicSystemConfig") }}</h3>
        <el-form-item :label="$t('system:config:QcRiskControl')" style="width: 45%;">
          <el-select v-model="form.BasicSystemConfig.QCRiskControl" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:OpenUserComplexPassword')" style="width: 45%;">
          <el-select v-model="form.BasicSystemConfig.OpenUserComplexPassword" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:OpenSignDocumentBeforeWork')" style="width: 45%;">
          <el-select v-model="form.BasicSystemConfig.OpenSignDocumentBeforeWork" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:OpenTrialRelationDelete')" style="width: 45%;">
          <el-select v-model="form.BasicSystemConfig.OpenTrialRelationDelete" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:OpenLoginLimit')" style="width: 45%;">
          <el-select v-model="form.BasicSystemConfig.OpenLoginLimit" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:LoginMaxFailCount')" style="width: 45%;"
          prop="BasicSystemConfig.LoginMaxFailCount">
          <el-input v-model="form.BasicSystemConfig.LoginMaxFailCount" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:LoginFailLockMinutes')" style="width: 45%;"
          prop="BasicSystemConfig.LoginFailLockMinutes">
          <el-input v-model="form.BasicSystemConfig.LoginFailLockMinutes" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:AutoLoginOutMinutes')" style="width: 45%;"
          prop="BasicSystemConfig.AutoLoginOutMinutes">
          <el-input v-model="form.BasicSystemConfig.AutoLoginOutMinutes" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:OpenLoginMFA')" style="width: 45%;">
          <el-select v-model="form.BasicSystemConfig.OpenLoginMFA" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:ContinuousReadingTimeMin')" style="width: 45%;"
          prop="BasicSystemConfig.ContinuousReadingTimeMin">
          <el-input v-model="form.BasicSystemConfig.ContinuousReadingTimeMin" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:ReadingRestTimeMin')" style="width: 45%;"
          prop="BasicSystemConfig.ReadingRestTimeMin">
          <el-input v-model="form.BasicSystemConfig.ReadingRestTimeMin" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:IsNeedChangePassWord')" style="width: 45%;">
          <el-select v-model="form.BasicSystemConfig.IsNeedChangePassWord" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:ChangePassWordDays')" style="width: 45%;"
          prop="BasicSystemConfig.ChangePassWordDays">
          <el-input v-model="form.BasicSystemConfig.ChangePassWordDays" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:TemplateType')" style="width: 45%;"
          prop="BasicSystemConfig.TemplateType">
          <el-select v-model="form.BasicSystemConfig.TemplateType" placeholder="">
            <el-option v-for="item in $d.SysTemplateType" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:ThirdPdfUrl')" style="width: 45%;" prop="BasicSystemConfig.ThirdPdfUrl">
          <el-input v-model="form.BasicSystemConfig.ThirdPdfUrl" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:UserMFAVerifyMinutes')" style="width: 45%;"
          prop="BasicSystemConfig.UserMFAVerifyMinutes">
          <el-input v-model="form.BasicSystemConfig.UserMFAVerifyMinutes" clearable type="number" />
        </el-form-item>
        <h3>{{ $t("system:config:SystemEmailSendConfig") }}</h3>
        <el-form-item :label="$t('system:config:Host')" style="width: 45%;" prop="SystemEmailSendConfig.Host">
          <el-input v-model="form.SystemEmailSendConfig.Host" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:Port')" style="width: 45%;" prop="SystemEmailSendConfig.Port">
          <el-input v-model="form.SystemEmailSendConfig.Port" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:Imap')" style="width: 45%;" prop="SystemEmailSendConfig.Imap">
          <el-input v-model="form.SystemEmailSendConfig.Imap" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:ImapPort')" style="width: 45%;" prop="SystemEmailSendConfig.ImapPort">
          <el-input v-model="form.SystemEmailSendConfig.ImapPort" clearable type="number" />
        </el-form-item>
        <el-form-item :label="$t('system:config:FromEmail')" style="width: 45%;" prop="SystemEmailSendConfig.FromEmail">
          <el-input v-model="form.SystemEmailSendConfig.FromEmail" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:FromName')" style="width: 45%;" prop="SystemEmailSendConfig.FromName">
          <el-input v-model="form.SystemEmailSendConfig.FromName" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:AuthorizationCode')" style="width: 45%;"
          prop="SystemEmailSendConfig.AuthorizationCode">
          <el-input v-model="form.SystemEmailSendConfig.AuthorizationCode" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:SiteUrl')" style="width: 45%;" prop="SystemEmailSendConfig.SiteUrl">
          <el-input v-model="form.SystemEmailSendConfig.SiteUrl" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:SystemShortName')" style="width: 45%;"
          prop="SystemEmailSendConfig.SystemShortName">
          <el-input v-model="form.SystemEmailSendConfig.SystemShortName" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:OrganizationName')" style="width: 45%;"
          prop="SystemEmailSendConfig.OrganizationName">
          <el-input v-model="form.SystemEmailSendConfig.OrganizationName" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:OrganizationNameCN')" style="width: 45%;"
          prop="SystemEmailSendConfig.OrganizationNameCN">
          <el-input v-model="form.SystemEmailSendConfig.OrganizationNameCN" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:PlatformName')" style="width: 45%;"
          prop="SystemEmailSendConfig.PlatformName">
          <el-input v-model="form.SystemEmailSendConfig.PlatformName" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:PlatformNameCN')" style="width: 45%;"
          prop="SystemEmailSendConfig.PlatformNameCN">
          <el-input v-model="form.SystemEmailSendConfig.PlatformNameCN" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:CompanyName')" style="width: 45%;"
          prop="SystemEmailSendConfig.CompanyName">
          <el-input v-model="form.SystemEmailSendConfig.CompanyName" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:CompanyNameCN')" style="width: 45%;"
          prop="SystemEmailSendConfig.CompanyNameCN">
          <el-input v-model="form.SystemEmailSendConfig.CompanyNameCN" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:CompanyShortName')" style="width: 45%;"
          prop="SystemEmailSendConfig.CompanyShortName">
          <el-input v-model="form.SystemEmailSendConfig.CompanyShortName" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:CompanyShortNameCN')" style="width: 45%;"
          prop="SystemEmailSendConfig.CompanyShortNameCN">
          <el-input v-model="form.SystemEmailSendConfig.CompanyShortNameCN" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:config:IsEnv_US')" style="width: 45%;">
          <el-select v-model="form.SystemEmailSendConfig.IsEnv_US" placeholder="">
            <el-option v-for="item in $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:config:EmailRegexStr')" style="width: 45%;"
          prop="SystemEmailSendConfig.EmailRegexStr">
          <el-input v-model="form.SystemEmailSendConfig.EmailRegexStr" clearable />
        </el-form-item>
        <el-form-item style="width: 100%;text-align: right;">
          <el-button type="primary" size="small" @click="handleSave">
            {{ $t('common:button:save') }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { getSystemBasicConfigInfo, getEmailConfigInfo, updateSystemBasicConfig, updateSystemEmailConfig } from '@/api/admin'
export default {
  name: "systemConfig",
  data() {
    return {
      rules: {
        'BasicSystemConfig.LoginMaxFailCount': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.LoginFailLockMinutes': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.AutoLoginOutMinutes': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.ContinuousReadingTimeMin': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.ReadingRestTimeMin': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.ChangePassWordDays': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.TemplateType': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.UserMFAVerifyMinutes': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'BasicSystemConfig.ThirdPdfUrl': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],

        'SystemEmailSendConfig.Host': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.Port': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'SystemEmailSendConfig.Imap': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.ImapPort': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { type: 'number', message: this.$t('systemConfig:ruleMessage:mustNumber'), trigger: 'blur' },
        ],
        'SystemEmailSendConfig.FromEmail': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.FromName': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.AuthorizationCode': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.SiteUrl': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.SystemShortName': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.OrganizationName': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.OrganizationNameCN': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.PlatformName': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.PlatformNameCN': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.CompanyName': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.CompanyNameCN': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.CompanyShortName': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.CompanyShortNameCN': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        'SystemEmailSendConfig.EmailRegexStr': [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
      },
      form: {
        "BasicSystemConfig": {
          // 启用质控风险控制功能
          "QCRiskControl": true,
          // 打开用户复杂密码
          "OpenUserComplexPassword": false,
          // 是否在开始工作前强制签署电子知情同意书
          "OpenSignDocumentBeforeWork": false,
          // 是否打开项目关联删除
          "OpenTrialRelationDelete": false,
          //是否启用登录失败次数限制(防暴力破解)
          "OpenLoginLimit": false,
          // 连续登录失败多少次后触发锁定
          "LoginMaxFailCount": 5,
          //触发锁定后账号锁定时长(分钟)
          "LoginFailLockMinutes": 1,
          //无操作自动登出的超时时间(分钟)
          "AutoLoginOutMinutes": 10,
          // 是否启用多因子登录认证(MFA)
          "OpenLoginMFA": false,
          // 连续阅片的最长工作时间(分钟)，超时后强制休息
          "ContinuousReadingTimeMin": 120,
          //强制休息时长(分钟)
          "ReadingRestTimeMin": 10,
          // 是否强制用户定期修改密码
          "IsNeedChangePassWord": true,
          // 密码有效期(天)，到期后必须修改
          "ChangePassWordDays": 90,
          //模板类型1Elevate(LiLi)2Extensive(展影)
          "TemplateType": 2,
          // 转换PDF服务配置
          "ThirdPdfUrl": "http://106.14.89.110:30088/api/v1/convert/file/pdf",
          //MFA免验证发送天数
          "UserMFAVerifyMinutes": 1440
        },
        // 邮件服务配置(用于系统通知、找回密码、错误报警等'
        "SystemEmailSendConfig": {
          //企业邮箱sMTP服务器地址
          "Host": "smtp.qiye.aliyun.com",
          //SMTP端口
          "Port": 465,
          "Imap": "imap.qiye.aliyun.com",
          "ImapPort": 993,
          // 发件人邮箱地址
          "FromEmail": "test@extimaging.com",
          // 发件人显示名称
          "FromName": "Test IRC Imaging System",
          //SMTP授权码
          "AuthorizationCode": "SHzyyl2021",
          //系统对外访问地址
          "SiteUrl": "http://irc.test.extimaging.com/login",
          //系统简称-致性核查使用
          "SystemShortName": "IRC",
          // 组织英文名称-添加用户默认值
          "OrganizationName": "ExtImaging",
          // 组织中文名称
          "OrganizationNameCN": "ExtImaging",
          "PlatformName": "EICS",
          "PlatformNameCN": "展影云平台",
          //公司英文全称
          "CompanyName": "Extensive Imaging",
          // 公司中文全称,
          "CompanyNameCN": "上海展影医疗科技有限公司",
          //公司英文简称
          "CompanyShortName": "Extensive Imaging",
          //公司中文简称
          "CompanyShortNameCN": "展影医疗",
          // 是否为国际版环境 方便前端区分，用于区分1i1li还是irc 做一些事情"
          "IsEnv_US": false,
          // 邮箱格式校验正则表达式
          "EmailRegexStr": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
          //后端周期性邮件默认语言
          // "CronEmailDefaultCulture": "zh-CN"
        }
      },
      loading: false
    }
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    handleSave() {
      this.loading = true
      Promise.all([this.updateSystemBasicConfig(), this.updateSystemEmailConfig()]).then(res => {
        this.loading = false
        this.getInfo()
      })
    },
    getInfo() {
      this.loading = true
      Promise.all([this.getSystemBasicConfigInfo(), this.getEmailConfigInfo()]).then(res => {
        this.loading = false
      })
    },
    async updateSystemBasicConfig() {
      try {
        let validate = await this.$refs.systemConfigForm.validate()
        if (!validate) return false
        let data = {}
        Object.keys(this.form.BasicSystemConfig).forEach(key => {
          data[key] = this.form.BasicSystemConfig[key]
        })
        let res = await updateSystemBasicConfig(data)
        if (res.IsSuccess) {

        }
      } catch (err) {
        console.log(err)
      }
    },
    async updateSystemEmailConfig() {
      try {
        let validate = await this.$refs.systemConfigForm.validate()
        if (!validate) return false
        let data = {}
        Object.keys(this.form.SystemEmailSendConfig).forEach(key => {
          data[key] = this.form.SystemEmailSendConfig[key]
        })
        let res = await updateSystemEmailConfig(data)
        if (res.IsSuccess) {

        }
      } catch (err) {
        console.log(err)
      }
    },
    async getSystemBasicConfigInfo() {
      try {
        let res = await getSystemBasicConfigInfo()
        if (res.IsSuccess) {
          Object.keys(this.form.BasicSystemConfig).forEach(key => {
            this.form.BasicSystemConfig[key] = res.Result[key]
          })
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    async getEmailConfigInfo() {
      try {
        let res = await getEmailConfigInfo()
        if (res.IsSuccess) {
          Object.keys(this.form.SystemEmailSendConfig).forEach(key => {
            this.form.SystemEmailSendConfig[key] = res.Result[key]
          })
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.systemConfig {
  overflow-y: auto;

  h3 {
    width: 100%;
  }
}
</style>