<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="50%">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="所属农场" style="width: 100%">
        <!--<el-input v-model="form.projectid" style="width: 370px;"/>-->
        <el-select v-model="form.projectid" :disabled="isAdd ? false : true" placeholder="请选择" >
          <el-option v-for="project in projects" :key="project.id" :label="project.proname" :value="project.id"/>
        </el-select>
        <el-button :disabled="isAdd ? false : true" type="primary" @click="createDialog">新增项目</el-button>
      </el-form-item>
      <el-form-item label="人员结构名称" >
        <el-input v-model="form.name" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="男人人数" >
        <el-input v-model="form.man" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="女人人数" >
        <el-input v-model="form.woman" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="孤寡老人人数" >
        <el-input v-model="form.oldman" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="留守儿童人数" >
        <el-input v-model="form.leftoverhildren" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="贫困人口" >
        <el-input v-model="form.poor" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="残疾人口" >
        <el-input v-model="form.disabled" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="35岁一下人数" >
        <el-input v-model="form.thirtyfivedown" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="35-45岁人数" >
        <el-input v-model="form.fortyfive" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="45-55岁人数" >
        <el-input v-model="form.fiftyfive" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="大于55岁人数" >
        <el-input v-model="form.fiftyfiveup" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="备注" >
        <el-input v-model="form.nt" style="width: 100%;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
    <el-dialog :visible.sync="createProjectDialog" width="40%" title="新增项目" center append-to-body>
      <el-form ref="form" :model="projectForm" :rules="rules" size="small" label-width="80px">
        <el-form-item label="项目名">
          <el-input v-model="projectForm.proname" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="createProjectDialog = false">取消</el-button>
        <el-button :loading="loading" type="primary" @click="submitProject">确认</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/sPersonnel'
import { addProject, findProjectByUserid } from '@/api/sProject'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false, projects: '', createProjectDialog: false,
      form: {
        id: '',
        projectid: '',
        name: '',
        man: '',
        woman: '',
        oldman: '',
        leftoverhildren: '',
        poor: '',
        disabled: '',
        thirtyfivedown: '',
        fortyfive: '',
        fiftyfive: '',
        fiftyfiveup: '',
        nt: ''
      },
      projectForm: {
        proname: '新建项目'
      },
      rules: {
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    createDialog() {
      this.createProjectDialog = true
    },
    submitProject() {
      addProject(this.projectForm).then(response => {
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.createProjectDialog = false
        this.initProjectUser()
      })
    },
    initProjectUser() {
      findProjectByUserid().then(response => {
        if (response.length === 0) {
          this.createProjectDialog = true
        } else {
          this.projects = response
          if (this.isAdd) {
            this.form.projectid = this.projects[0].id
          } else {
            this.form.projectid = parseInt(this.form.nt)
          }
        }
      })
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        projectid: '',
        name: '',
        man: '',
        woman: '',
        oldman: '',
        leftoverhildren: '',
        poor: '',
        disabled: '',
        thirtyfivedown: '',
        fortyfive: '',
        fiftyfive: '',
        fiftyfiveup: '',
        nt: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
