<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="部门名称:">
        <span>{{this.depName}}</span>
      </el-form-item>
      <el-form-item label="团队名称:" prop="position">
        <el-input v-model="ruleForm.position" type="text" autosize placeholder="请输入团队名称" />
      </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 label="任务额:" prop="targetmoney">
        <el-input v-model.number="ruleForm.targetmoney" 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 { addTeam, editTeam, getTeamDetail } from '@/api/systemSetup/sysSetting/teamManagement'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    },
    depId: {
      type: Number,
      default: 0
    },
    depName: {
      type: String,
      default: ''
    },
  },
  data () {
    return {
      props: {
        value: 'id',
        label: 'text',
        children: "children"
      },
      ruleForm: {
        id: '',
        position: '',
        department: [],
        des: '',
        targetmoney: '',
      },
      rules: {
        position: [{
          required: true,
          message: '请输入团队名称!',
          trigger: 'blur'
        }],
        targetmoney: [
          { type: 'number', message: '任务额必须为数字!' }
        ]
      },
      positionData: [],//职位数据
      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,
            name: this.ruleForm.position,
            deptid: this.depId,
            targetmoney: this.ruleForm.targetmoney,
            des: filterContent.delHtmlTag(this.ruleForm.des)
          }

          // 添加
          if (!this.rowid) {
            addTeam(datas).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$parent.getTreeLists() // 重新加载父级数据
                this.$parent.depId = 0
                this.$message.success('恭喜你，团队管理添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editTeam(datas).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$parent.getTreeLists() // 重新加载父级数据
              this.$parent.depId = 0
              this.$message.success('恭喜你，团队管理编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm () {
      this.$refs.ruleForm.resetFields()
    },
    // 获取详情
    getDetail (rid) {
      getTeamDetail(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.position = res.F_Name
          this.ruleForm.des = res.F_Des
          this.ruleForm.targetmoney = res.F_TargetMoney
        }
      })
    },
  }
}
</script>

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