<template>
  <div v-loading="loading" class="addOrEditDuty">
    <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="date">
          <el-date-picker
          v-model="ruleForm.date"
          type="datetime"
          value-format='yyyy-MM-dd HH:mm:ss'
          placeholder="选择值班时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属坐席组" prop="groupid">
        <el-select
          v-model="ruleForm.groupid"
          class="select_picker"
          filterable
          clearable
          placeholder="请选择所属坐席组">
          <el-option
            v-for="item in seatOptions"
            :key="item.F_ZXZID"
            :label="item.F_ZXZName"
            :value="item.F_ZXZID"/>
        </el-select>
      </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 { getDuty, addDuty, editDuty, deleteDuty} from '@/api/telCall/duty'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'
import { getSeatGroup } from '@/api/commonAPI'

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

export default {
  name: 'AddOrEditDuty',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      ruleForm: {
        id: '',
        phone: '',
        date: '',//值班时间
        groupid: '', //	是	string	坐席组id
        des: ''
      },
      seatOptions:[],//坐席数据
      rules: {
        phone: [{
          required: true,
          trigger: 'blur',
          validator: validateTelRule
        }],
        groupid: [{
          required: true,
          trigger: 'change',
          message: '您还没有选择所属坐席组！'
        }],
        date: [{
          required: true,
          trigger: 'blur',
          message: '您还没有选择值班时间！'
        }]
      },
      loading: false
    }
  },
  created() {
    if (this.rowid) {
      this.getSeatGroupSelects().then(() => {
        this.getDetail()
      })
    } else {
      this.getSeatGroupSelects()
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const datas = {
            id: this.rowid,
            dutynumber: this.ruleForm.phone,
            dutytime: this.ruleForm.date,
            groupid: this.ruleForm.groupid,
            remark: filterContent.delHtmlTag(this.ruleForm.des)
          }

          // 添加
          if (!this.rowid) {
            addDuty(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
          }
          // 编辑
          editDuty(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() {
      getDuty(this.rowid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.phone = res.F_DutyNumber
          this.ruleForm.groupid = res.F_Groupid
          this.ruleForm.des = res.F_Remark
          this.ruleForm.date = res.F_ZBTime
        }
      })
    },
    // 获取坐席组下拉数据
    getSeatGroupSelects() {
      return new Promise(resolve => {
        getSeatGroup().then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.seatOptions = response.data
          }
        })
        resolve()
      })
    },
    //删除功能
    btn_delete(editId) {
      this.$confirm('您确定要将此值班信息删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDuty(editId).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.addOrEditDuty{
		.select_picker{
			width: 100%;
		}
	}
</style>
