<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card v-loading="loading" class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ __('邮件账号设置') }}</span>
          </div>
          <div>
            <el-form ref="form" label-width="100px" :model="form" size="medium">
              <el-form-item
                :label="__('发信邮箱')"
                label-width="200px"
                prop="email_addr"
              >
                <el-input
                  v-model="form.email_addr"
                  clearable
                  :placeholder="
                    __('使用SMTP协议发送的邮件地址，如 shop@163.com')
                  "
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('发信人')"
                label-width="200px"
                prop="email_fromname"
              >
                <el-input
                  v-model="form.email_fromname"
                  clearable
                  :placeholder="__('请输入发信人')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('SMTP 服务器')"
                label-width="200px"
                prop="email_host"
              >
                <el-input
                  v-model="form.email_host"
                  clearable
                  :placeholder="__('设置 SMTP 服务器的地址，如 smtp.163.com')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('SMTP 端口号')"
                label-width="200px"
                prop="email_port"
              >
                <el-input
                  v-model="form.email_port"
                  clearable
                  :placeholder="__('设置 SMTP 服务器的端口，默认为 25')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('单行SMTP 身份验证用户名')"
                label-width="200px"
                prop="email_id"
              >
                <el-input
                  v-model="form.email_id"
                  clearable
                  :placeholder="__('如 shop')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('SMTP 身份验证密码')"
                label-width="200px"
                prop="email_pass"
              >
                <el-input
                  v-model="form.email_pass"
                  clearable
                  :placeholder="__('shop@163.com邮件的密码，如 123456')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('是否加密')"
                label-width="200px"
                prop="email_secure"
              >
                <el-radio-group v-model="form.email_secure" size="medium">
                  <el-radio
                    v-for="(item, index) in email_secureOptions"
                    :key="index"
                    :disabled="item.disabled"
                    :label="__(item.value)"
                  >
                    {{ __(item.label) }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item
                :label="__('调试级别')"
                label-width="200px"
                prop="email_debug"
              >
                <el-radio-group v-model="form.email_debug" size="medium">
                  <el-radio
                    v-for="(item, index) in email_debugOptions"
                    :key="index"
                    :disabled="item.disabled"
                    :label="item.value"
                  >
                    {{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item
                :label="__('测试接收的邮件地址')"
                label-width="200px"
                prop="email_test"
              >
                <el-input
                  v-model="form.email_test"
                  clearable
                  :placeholder="__('请输入测试接收的邮件地址')"
                  :style="{ width: '100%' }"
                >
                  <el-button slot="append" @click="testEmail()">
                    {{ __('测试') }}
                  </el-button>
                </el-input>
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-button
              class="form-sub-btn"
              icon="el-icon-s-tools"
              type="primary"
              @click="handleEdit"
            >
              {{ __('修改') }}
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doEdit, testEmail } from '@/api/base/config'
  export default {
    name: 'Mall',
    components: {},
    data() {
      return {
        loading: false,
        form: {
          email_addr: undefined,
          email_fromname: undefined,
          email_host: '',
          email_port: undefined,
          email_id: undefined,
          email_pass: undefined,
          email_debug: undefined,
          email_test: undefined,
        },
        email_secureOptions: [
          {
            label: this.__('不加密'),
            value: '1',
          },
          {
            label: 'ssl',
            value: '2',
          },
          {
            label: 'tls',
            value: '3',
          },
        ],
        email_debugOptions: [
          {
            label: this.__('关闭'),
            value: '1',
          },
          {
            label: this.__('一般'),
            value: '2',
          },
          {
            label: this.__('较高'),
            value: '3',
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      async testEmail() {
        this.loading = true
        const { msg, status } = await testEmail()
        this.loading = false
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
      },
      __,
      async fetchData() {
        const { data } = await getList({ config_key: 'email' })
        this.form = {
          email_fromname: data.email_fromname,
          email_addr: data.email_addr,
          email_host: data.email_host,
          email_port: data.email_port,
          email_id: data.email_id,
          email_pass: data.email_pass,
          email_debug: data.email_debug,
          email_test: data.email_test,
          email_secure: data.email_secure,
        }
      },
      handleEdit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.form),
            })

            if (status == 200) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
