<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.position" placeholder="请输入职位名称"/>
        <!-- <el-select
          v-model="ruleForm.position"
          class="select_picker"
          filterable
          clearable
          placeholder="请选择职位名称">
          <el-option
            v-for="item in positionData"
            :key="item.F_Id"
            :label="item.F_Name"
            :value="item.F_Name"/>
        </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 { getposition, addPosition, editPosition,getPositionData } from '@/api/systemSetup/sysSetting/positionManagement'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        position: '',
        des: ''
      },
      rules: {
        position: [{
          required: true,
          message: '请输入职位名称!',
          trigger: 'blur'
        }]
      },
      positionData:[],//职位数据
      loading: false
    }
  },
  created() {
      this.getPositionList()//获取职位数据
    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
        }
      })
    },
    //获取职位数据
    getPositionList(){
        getPositionData().then(response=>{
            if(response.state.toLowerCase()=='success'){
                this.positionData=response.data
            }
        })
    }
  }
}
</script>

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