<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入电话号码"/>
      </el-form-item>
      <el-form-item label="设置时间段" prop="rangTimes">
        <el-date-picker
          v-model="ruleForm.rangTimes"
          class="date_picker"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="left"
          unlink-panels/>
      </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 { getBlack, addBlack, editBlack } from '@/api/telCall/blackList'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'

const validateTelRule = (rule, value, callback) => {
  if (!validateTel(value)) {
    callback(new Error('请输入有效的电话号码!'))
  } else {
    callback()
  }
}

export default {
  name: 'AddOrEditBlackList',
  props: {
    rowid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        phone: '',
        rangTimes: [],
        des: ''
      },
      rules: {
        phone: [{
          required: true,
          trigger: 'blur',
          validator: validateTelRule
        }],
        rangTimes: [{
          required: true,
          message: '请设置时间段!',
          trigger: 'blur'
        }]
      },
      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,
            telphone: this.ruleForm.phone,
            settime: this.ruleForm.rangTimes && this.ruleForm.rangTimes[0],
            removetime: this.ruleForm.rangTimes && this.ruleForm.rangTimes[1],
            describe: filterContent.delHtmlTag(this.ruleForm.des)
          }

          // 添加
          if (!this.rowid) {
            addBlack(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
          }
          // 编辑
          editBlack(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) {
      getBlack(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.phone = res.F_TelPhone
          this.ruleForm.rangTimes = [res.F_SetTime, res.F_RemoveTime]
          this.ruleForm.des = res.F_Describe
        }
      })
    }
  }
}
</script>

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