<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="模板名称" prop="name" >
          <el-input v-model="ruleForm.name" placeholder="请输入模板名称"/>
      </el-form-item>
      <el-form-item label="400电话" prop="phone" >
          <el-input v-model.number="ruleForm.phone" placeholder="请输入400电话" maxlength='15'/>
      </el-form-item>
      <el-form-item label="模板内容" prop="content">
        <el-input v-model="ruleForm.content" type="textarea" autosize placeholder="请输入模板内容"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.number="ruleForm.sort"  placeholder="请输入排序" :maxlength="5"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addSMSTemplate,editSMSTemplate,getSMSTemplateList } from '@/api/noteManagement/noteList'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        sort: '',
        content: '',
        name: '',
        phone: '',
      },
      rules: {
        name: [{
          required: true,
          message: '请输入模板名称!',
        }],
        phone: [{
          required: true,
          message: '请输入400电话!'
        },
        {
          type: 'number',
          message: '400电话必须为数字'
        }],
        content: [{
          required: true,
          message: '请输入模板内容!',
        }],
        sort: [{
          required: true,
          message: '请输入排序!',
        },
        {
            type:'number',
            message:'排序必须为数字'
        }],
      },
      loading: false
    }
  },
  created() {
    if (this.rowid) {
      this.ruleForm.id = this.rowid
      this.getDetail(this.rowid)
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const datas = {
            F_Id: this.ruleForm.id,
            F_ModelName:this.ruleForm.name,
            F_Content: this.ruleForm.content,
            F_Tel: this.ruleForm.phone,
            F_Sort: this.ruleForm.sort,
          }

          // 添加
          if (!this.rowid) {
            addSMSTemplate(datas).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，短信模板添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editSMSTemplate(datas).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，短信模板编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    // 获取详情
    getDetail(rid) {
        const data={
            F_Id:rid
        }
      getSMSTemplateList(data).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data.Rows[0]
          this.ruleForm.content=res.F_Content;
          this.ruleForm.name=res.F_ModelName;
          this.ruleForm.phone=res.F_Tel-0;
          this.ruleForm.sort=res.F_Sort;
        }
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.date_picker{
		width: 100%;
	}
	div.date_picker input.el-range-input{
		width: 46%;
	}
</style>
