<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="职位名称" prop="position">
          <el-input v-model="ruleForm.name" placeholder="请输入职位名称"/>
      </el-form-item>
      <el-form-item label="任务时间">
        <el-date-picker
          v-model="ruleForm.taskDate"
          :picker-options="pickerOptions"
          class="filter-item"
          type="daterange"
          format="yyyy年MM月dd日"
          value-format="yyyy-MM-dd"
          align="left"
          unlink-panels
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </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="type">
         <el-select v-model="taskTypeId" placeholder="请选择任务类型">
          <el-option
            v-for="item in taskType"
            :key="item.id"
            :label="item.label"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务部门" prop="department">
         <el-select v-model="depid" placeholder="请选择任务部门">
          <el-option
            v-for="item in depData"
            :key="item.id"
            :label="item.label"
            :value="item.id">
          </el-option>
        </el-select>
      </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 { getposition, addPosition, editPosition } from '@/api/systemSetup/sysSetting/positionManagement'
import { filterContent,pickerOptions } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        taskDate: [],//任务时间
        name: '',
        content: ''
      },
      rules: {
        name: [{
          required: true,
          message: '请输入任务名称!',
          trigger: 'blur'
        }]
      },
      taskTypeId:[
        {
          id:0,
          label:'日'
        },
        {
          id:1,
          label:'周'
        },
        {
          id:2,
          label:'月'
        },
      ],
      depData:[],//部门数据
      pickerOptions,//日期数据
      loading: false
    }
  },
  created() {
    this.getDepartment()
    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,
            name:this.ruleForm.position,
            des: filterContent.delHtmlTag(this.ruleForm.des)
          }

          // 添加
          if (!this.rowid) {
            addPosition(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
          }
          // 编辑
          editPosition(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) {
      getposition(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.position = res.F_Name
          this.ruleForm.des = res.F_Des
        }
      })
    },
    //获取任务部门
    getDepartment(){

    }
  }
}
</script>

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