<template>
  <div style="box-sizing: border-box; padding: 20px">
    <zj-form-container
      ref="formRef"
      :formData="formData"
      :formRules="formRules"
      :formAttributes="{ 'label-position': 'top' }"
    >
      <zj-form-module title="邮箱配置" :formData="formData" :formItems="formItems" :column="1"> </zj-form-module>
    </zj-form-container>
    <div>
      <el-button size="mini" type="primary" @click="submit">提交</el-button>
      <el-button size="mini" type="success" @click="reset">重置</el-button>
    </div>
  </div>
</template>

<script>
import { insureEmailConfig, insureEmailDetail } from '@/api/InsuranceManagement.js'
export default {
  data() {
    return {
      formData: {
        sendEmail: '',
        sendName: '',
        sendPassword: '',
        corpId: '',
        secret: '',
        receComEmail: '',
        receYwxEmail: '',
        sendDailyTime: '',
        noticeDay: ''
      },
      formRules: {
        receComEmail: [{ required: true, message: '请输入', trigger: 'blur' }],
        receYwxEmail: [{ required: true, message: '请输入', trigger: 'blur' }],
        sendDailyTime: [{ required: true, message: '请输入', trigger: 'blur' }],
        noticeDay: [{ required: true, message: '请输入', trigger: 'blur' }],
        sendName: [{ required: true, message: '请输入', trigger: 'blur' }],
        sendEmail: [{ required: true, message: '请输入', trigger: 'blur' }],
        sendPassword: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    formItems() {
      return [
        {
          name: 'el-input',
          formItemAttributes: { label: '接收邮箱,公用', prop: 'receComEmail' }
        },
        {
          name: 'el-input',
          formItemAttributes: { label: '接收邮箱,意外险专用,多邮箱以英文逗号隔开', prop: 'receYwxEmail' }
        },
        {
          name: 'el-time-picker',
          md: 8,
          attributes: { style: 'width:100%', format: 'HH:mm', valueFormat: 'HH:mm' },
          formItemAttributes: { label: '每日发送时间', prop: 'sendDailyTime', 'label-position': 'left' }
        },
        {
          name: 'el-input',
          md: 8,
          formItemAttributes: { label: '到期提醒时间：保险到期前多少天', prop: 'noticeDay' }
        },
        {
          name: 'el-input',
          md: 8,
          formItemAttributes: { label: '企业id', prop: 'corpId' }
        },
        {
          name: 'el-input',
          md: 8,
          formItemAttributes: { label: '发件人昵称', prop: 'sendName' }
        },
        {
          name: 'el-input',
          md: 8,
          formItemAttributes: { label: '发件人邮箱', prop: 'sendEmail' }
        },
        {
          name: 'el-input',
          md: 8,
          formItemAttributes: { label: '发件人密码', prop: 'sendPassword' }
        },

        {
          name: 'el-input',
          md: 8,
          formItemAttributes: { label: '应用的凭证密钥', prop: 'secret' }
        }
      ]
    }
  },
  created() {
    // 获取详情
    insureEmailDetail().then(res => {
      for (var key in res.data) {
        if (key !== 'id') {
          this.formData[key] = res.data[key]
        }
      }
    })
  },
  methods: {
    submit() {
      this.$refs['formRef'].validate(valid => {
        if (valid) {
          insureEmailConfig({
            ...this.formData
          }).then(res => {
            this.$message({
              type: 'success',
              message: `保存成功!`
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    }
  }
}
</script>

<style lang="scss" scoped></style>
