<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="短信模板表ID" prop="smsTemplateId">
          <el-input v-model="dataForm.smsTemplateId" placeholder="短信模板表ID"></el-input>
      </el-form-item>
          <el-form-item label="手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
          <el-form-item label="参数1" prop="params1">
          <el-input v-model="dataForm.params1" placeholder="参数1"></el-input>
      </el-form-item>
          <el-form-item label="参数2" prop="params2">
          <el-input v-model="dataForm.params2" placeholder="参数2"></el-input>
      </el-form-item>
          <el-form-item label="参数3" prop="params3">
          <el-input v-model="dataForm.params3" placeholder="参数3"></el-input>
      </el-form-item>
          <el-form-item label="参数4" prop="params4">
          <el-input v-model="dataForm.params4" placeholder="参数4"></el-input>
      </el-form-item>
      <el-form-item label="发送状态" prop="status">
          <el-select v-model="dataForm.status" placeholder="发送状态">
            <el-option v-for="item in statusArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue" >
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="平台类型" prop="platform">
          <el-select v-model="dataForm.platform" placeholder="平台类型">
            <el-option v-for="item in platformArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue" >
            </el-option>
          </el-select>
      </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: '',
        smsTemplateId: '',
        mobile: '',
        params1: '',
        params2: '',
        params3: '',
        params4: '',
        status: '',
        platform: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        delFlag: ''
      },
      statusArr: [
        { dictValue: 1, dictName: '成功' },
        { dictValue: 0, dictName: '失败' }
      ],
      platformArr: [
        { dictValue: 1, dictName: '阿里云' },
        { dictValue: 2, dictName: '腾讯云' }
      ]
    }
  },
  computed: {
    dataRule () {
      return {
        smsTemplateId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        params1: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        params2: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        params3: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        params4: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { 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' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/message/smslog/${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/smslog/', 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>
