<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px">
      <el-form-item label="坐席组编号" prop="code">
        <el-input v-model="ruleForm.code" placeholder="请输入坐席组编号"/>
      </el-form-item>
      <el-form-item label="坐席组名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入坐席组名称"/>
      </el-form-item>
      <el-form-item label="IVR按键" prop="ivrkey">
        <el-input v-model.number="ruleForm.ivrkey" placeholder="请输入IVR按键"/>
      </el-form-item>
      <el-form-item label="外呼本地前缀" prop="localPrefix">
        <el-input v-model="ruleForm.localPrefix" placeholder="请输入外呼本地前缀"/>
      </el-form-item>
      <el-form-item label="外呼外地前缀" prop="fieldPrefix">
        <el-input v-model="ruleForm.fieldPrefix" placeholder="请输入外呼外地前缀"/>
      </el-form-item>
      <el-form-item label="坐席所属地" prop="location">
        <el-input v-model="ruleForm.location" placeholder="请输入坐席所属地"/>
      </el-form-item>
      <el-form-item label="描述说明" prop="des">
        <el-input v-model="ruleForm.des" type="textarea" autosize placeholder="请输入描述说明"/>
      </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 { getSeatGroup, addSeatGroup, editSeatGroup } from '@/api/systemSetup/roleSetting/seatGroup'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        code: '',
        name: '',
        ivrkey: '',
        outcallnum: '',
        localPrefix: '',
        fieldPrefix: '',
        location: '',
        des: ''
      },
      rules: {
        code: [{
          required: true,
          trigger: 'blur',
          message: '请输入坐席组编号'
        }],
        name: [{
          required: true,
          trigger: 'blur',
          message: '请输入坐席组名称'
        }],
        localPrefix: [{
          required: true,
          trigger: 'blur',
          message: '请输入外呼本地前缀'
        }],
        fieldPrefix: [{
          required: true,
          trigger: 'blur',
          message: '请输入外呼外地前缀'
        }],
        location: [{
          required: true,
          trigger: 'blur',
          message: '请输入坐席所属地'
        }],
        ivrkey: [{
          required: true,
          trigger: 'blur',
          message: '请输入IVR按键'
        },{
          type: 'number',
          message: 'IVR按键必须为数字'
        }]
      },
      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 = {
            id: this.ruleForm.id,
            zxzcode: this.ruleForm.code,
            zxzname: this.ruleForm.name,
            ivrkey: this.ruleForm.ivrkey,
            whbdkey: this.ruleForm.localPrefix,
            whwdkey: this.ruleForm.fieldPrefix,
            zxatt: this.ruleForm.location,
            des: filterContent.delHtmlTag(this.ruleForm.des)
          }

          // 添加
          if (!this.rowid) {
            addSeatGroup(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
          }
          // 编辑
          editSeatGroup(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) {
      getSeatGroup(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.code = res.F_ZXZCode
          this.ruleForm.name = res.F_ZXZName
          this.ruleForm.ivrkey = res.F_IVRKey
          this.ruleForm.localPrefix = res.F_WHBDKey
          this.ruleForm.fieldPrefix = res.F_WHWDKey
          this.ruleForm.location = res.F_ZXAtt
          this.ruleForm.des = res.F_Des
        }
      })
    }
  }
}
</script>

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

</style>
