<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="任务人" prop="F_TaskUser">
        <el-select v-model="ruleForm.F_TaskUser" class="form_select" clearable filterable >
          <el-option
            v-for="item in roleOptions"
            :key="item.F_UserCode"
            :label="item.F_UserCode"
            :value="item.F_UserCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="F_State" >
        <el-select v-model="ruleForm.F_State" class="form_select" clearable filterable >
          <el-option label="未启动" value="0"></el-option>
          <el-option label="启动" value="1"></el-option>
          <el-option label="暂停" value="2"></el-option>
          <el-option label="终止" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务名称" prop="F_Name">
        <el-input v-model="ruleForm.F_Name" placeholder="请输入任务名称"/>
      </el-form-item>
      <el-form-item label="任务时间" prop="F_Time">
        <el-date-picker v-model="ruleForm.F_Time" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" class="form_date">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="外呼类型" prop="F_Type">
        <el-select v-model="ruleForm.F_Type" class="form_select" clearable filterable @change="handleChangeType" >
          <el-option label="语音外呼" value="1"></el-option>
          <el-option label="坐席外呼" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="外呼内容" prop="F_Content" v-show="displayContent">
        <el-input v-model="ruleForm.F_Content" placeholder="请输入外呼内容"/>
      </el-form-item>
      <el-form-item label="任务说明" prop="F_Remarks">
        <el-input v-model="ruleForm.F_Remarks" placeholder="请输入任务说明"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getTaskDetail, addTaskLists, editTaskLists } from '@/api/outbound/automaticOutbound'
import { getUserAccountLists } from '@/api/systemSetup/roleSetting/userManage'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: Number,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      roleOptions: [], //任务人
      typeDropDatas: [], // 下拉分类
      // 自定义菜单下拉数据的key值
      props: {
        value: 'id',
        label: 'text'
      },
      displayContent: true,
      ruleForm: {
        F_ID: '', //任务id
        F_TaskUser: '', //任务人
        F_Name: '', //任务名称
        F_Remarks: '', //任务说明
        F_Time: [], //时间
        F_StartTime: '', //开始时间
        F_EndTime: '', //结束时间
        F_State: '', //任务状态
        F_Type: '', //外呼类型
        F_Content: '', //外呼内容
      },
      rules: {
        F_TaskUser: [{
          required: true,
          trigger: 'change',
          message: '请选择任务人',
        }],
        F_State: [{
          required: true,
          trigger: 'change',
          message: '请选择任务状态',
        }],
        F_Name: [{
            required: true,
            trigger: "blur",
            message: "请输入任务名称",
        }],
        F_Type: [{
            required: true,
            trigger: "change",
            message: "请选择外呼类型",
        }],
      },
      loading: false
    }
  },
  created() {
    this.getRoleSelects()
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.ruleForm.F_ID = this.rowid
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.F_StartTime = this.ruleForm.F_Time[0]
          this.ruleForm.F_EndTime = this.ruleForm.F_Time[1]
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.loading = true
          // 添加
          if (!this.rowid) {
            addTaskLists(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getListTask() // 重新加载父级数据
                this.$message.success('恭喜你，添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editTaskLists(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getListTask() // 重新加载父级数据
              this.$message.success('恭喜你，编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    // 详情
    getDetail(rid) {
      const data = {
        id: rid
      }
      getTaskDetail(data).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.F_TaskUser = res.F_TaskUser //任务人
          this.ruleForm.F_Name = res.F_Name //任务名称
          this.ruleForm.F_Remarks = res.F_Remarks //任务说明
          this.ruleForm.F_Time.push(res.F_StartTime) //时间
          this.ruleForm.F_Time.push(res.F_EndTime) //时间
          this.ruleForm.F_State = res.F_State + "" //任务状态
          this.ruleForm.F_Type = res.F_Type + "" //外呼类型
          this.ruleForm.F_Content = res.F_Content //外呼内容
          if (res.F_Type == 2) {
            this.displayContent = false
          } else if (res.F_Type == 1) {
            this.displayContent = true
          }
        }
      })
    },
    handleChangeType(val) {
      if (val === "2") {
        this.displayContent = false
      } else if (val === "1") {
        this.displayContent = true
      }
    },
    //获取团队列表数据
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          key: this.keyword.trim(),
          deptid:this.depId
        }
        getTeamLists(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.dataLists = response.data.Rows
          }
        })
        resolve()
      })
    },
    // 获取任务人
    getRoleSelects () {
      getUserAccountLists().then(response => {
        if (response.state.toLowerCase() === 'success') {
          this.roleOptions = response.data.Rows
        }
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form {
  .form_select {
    width: 100%;
  }
  .form_date {
    width: 100%
  }
}
</style>