<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('smstemplate.add') : $t('smstemplate.update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="模板类型" prop="templateType">
        <el-select v-model="dataForm.templateType" placeholder="模板类型" @change="selectModel($event)">
          <el-option v-for="item in paramNameArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue" >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="启用标识" prop="enableFlag">
        <el-select v-model="dataForm.enableFlag" placeholder="启用标识">
          <el-option v-for="item in enableFlagArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue" >
          </el-option>
        </el-select>
      </el-form-item>
          <el-form-item label="短信签名" prop="signName">
          <el-input v-model="dataForm.signName" placeholder="短信签名" maxlength="2000"></el-input>
      </el-form-item>
      <el-form-item label="短信模板" prop="template">
          <el-input v-model="dataForm.template" placeholder="短信模板" maxlength="255"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注" type="textarea" :rows="2" maxlength="200"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        templateType: '',
        templateTypeName: '',
        signName: '',
        template: '',
        remark: '',
        enableFlag: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        delFlag: ''
      },
      paramNameArr: [],
      enableFlagArr: [
        { dictValue: '1', dictName: '是' },
        { dictValue: '0', dictName: '否' }
      ]
    }
  },
  computed: {
    dataRule () {
      return {
        templateType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        templateTypeName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        template: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        remark: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        enableFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        revision: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        delFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getParamListInfo('sms_template_type')
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 参数名称取值变化事件
    selectModel (id) {
      this.paramNameArr.find((item) => {
        if (item.dictValue === id) {
          this.dataForm.templateTypeName = item.dictName
        }
      })
    },
    // 获取参数名称code下拉信息
    getParamListInfo (dictType) {
      this.$http.get(`/sys/dict/listSimple/` + dictType).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (dictType === 'sms_template_type') {
          this.paramNameArr = res.data
        }
      }).catch(() => {})
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/message/smstemplate/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/message/smstemplate/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
