<template>
  <el-form
    ref="emailForm"
    v-loading="loading"
    :model="form"
    label-width="160px"
    size="small"
    :rules="rules"
  >
    <div class="base-dialog-body">
      <el-form-item label="Code: " prop="Code">
        <el-input :maxLength="$inputMax" v-model="form.Code" />
      </el-form-item>
      <el-form-item label="场景: " prop="ScenarioId">
        <el-select
          v-model="form.ScenarioId"
          style="width:100%;"
          size="small"
        >
          <el-option
            v-for="(key) of scenarioOption"
            :key="key"
            :label="key"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题: " prop="Title">
        <el-input :maxLength="$inputMax" v-model="form.Title" />
      </el-form-item>
      <el-form-item label="正文: " prop="Body">
        <el-input :maxLength="$inputMax" v-model="form.Body" type="textarea" rows="5" />
      </el-form-item>
      <el-form-item label="发件箱: " prop="FromEmail">
        <el-input :maxLength="$inputMax" v-model="form.FromEmail" />
      </el-form-item>
      <el-form-item label="发件箱授权码: " prop="AuthorizationCode">
        <el-input :maxLength="$inputMax" v-model="form.AuthorizationCode" />
      </el-form-item>
      <el-form-item label="收件人: " prop="ReceiveEmail">
        <el-input :maxLength="$inputMax" v-model="form.ReceiveEmail" />
      </el-form-item>
      <el-form-item label="抄送邮箱: " prop="CopyEmail">
        <el-input :maxLength="$inputMax" v-model="form.CopyEmail" />
      </el-form-item>
      <el-form-item label="是否需要回执: " prop="IsReturnRequired">
        <el-radio-group v-model="form.IsReturnRequired">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否加急: " prop="IsUrgent">
        <el-radio-group v-model="form.IsUrgent">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否自动发送: " prop="IsAutoSend">
        <el-radio-group v-model="form.IsUrgent">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="!!form.Id" label="是否启用: " prop="IsEnable">
        <el-radio-group v-model="form.IsEnable">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item style="text-align:right;">
        <el-button size="small" type="primary" :loading="saveBtnLoading" @click="handleSave">Save</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateEmailNoticeConfigList, getBasicDataSelect } from '@/api/dictionary'
export default {
  props: {
    data: {
      type: Object,
      default() { return {} }
    }
  },

  data() {
    return {
      form: {
        Id: '',
        Code: '',
        AuthorizationCode: '',
        ScenarioId: '',
        Title: '',
        Body: '',
        FromEmail: '',
        ReceiveEmail: '',
        CopyEmail: '',
        IsReturnRequired: true,
        IsUrgent: true,
        IsEnable: true,
        IsAutoSend: true
      },
      rules: {
        Code: [{ required: true, message: 'Please select', trigger: ['blur'] }],
        ScenarioId: [{ required: true, message: 'Please select', trigger: ['blur'] }]
      },
      scenarioOption: [],
      saveBtnLoading: false,
      loading: false
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    async initForm() {
      this.loading = true
      // await this.getScenarios()
      for (const k in this.form) {
        if (this.data.hasOwnProperty(k) && k !== 'ScenarioId') {
          this.form[k] = this.data[k]
        }
      }
      this.loading = false
    },
    // 获取场景下拉数据
    getScenarios() {
      getBasicDataSelect(['Email_Scenario']).then(res => {
        this.scenarioOption = res.Result
        if (this.form.Id) {
          this.form.ScenarioId = this.data.ScenarioId
        }
      }).catch(() => { this.loading = false })
    },
    // 保存
    handleSave() {
      this.$refs.emailForm.validate(valid => {
        if (!valid) return
        this.saveBtnLoading = true
        addOrUpdateEmailNoticeConfigList(this.form).then(res => {
          this.saveBtnLoading = false
          this.$emit('closeDialog')
          this.$emit('getList')
          this.$message.success('Saved successfully')
        }).catch(() => {
          this.saveBtnLoading = false
        })
      })
    }
  }
}
</script>
