<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="120px" label-position="left">
      <el-form-item label="项目名称" prop="xm_name">
        <el-input v-model="dialogForm.xm_name"></el-input>
      </el-form-item>
      <el-form-item label="项目类别" prop="xm_type">
        <el-select v-model="dialogForm.xm_type" placeholder="请选择" style="width:100%">
          <el-option v-for="item in selectObj.xm_type.option" :key="item.v" :label="item.n" :value="item.v"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="经办科室" prop="department_id">
        <el-select v-model="dialogForm.department_id" filterable placeholder="请选择" style="width:100%" @change="handleChange">
          <el-option v-for="item in departmentData" :key="item.uo_id" :label="item.uo_name" :value="item.uo_id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="经办人" prop="xm_zrr_id">
        <el-select v-model="dialogForm.xm_zrr_id" filterable placeholder="请选择" style="width:100%" @change="handleUserData">
          <el-option v-for="item in userData" :key="item.user_id" :label="item.user_realname" :value="item.user_id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目开始时间" prop="xm_start_time" style="width:100%">
        <el-date-picker v-model="dialogForm.xm_start_time" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width:100%"></el-date-picker>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { xmgl_xmzlkAdd, xmgl_xmzlkEdit, xmgl_xmzlkDetail, orgList, userList } from "./api";
export default {
  data() {
    return {
      departmentData: [],
      userData: [],
      dialogForm: {
        xm_name: "",
        xm_type: "",
        department_id: "",
        department_name: "",
        xm_zrr_id: "",
        xm_zrr: "",
        xm_start_time: "",
      },
      rules_check: {
        xm_name: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
        xm_type: [{ required: true, message: '请选择项目类别', trigger: 'change' }],
        department_id: [{ required: true, message: '请选择经办科室', trigger: 'change' }],
        xm_zrr_id: [{ required: true, message: '请选择经办人', trigger: 'change' }],
        xm_start_time: [{ required: true, message: '请选择项目开始时间', trigger: 'change' }]
      },
    }
  },
  props: ["currentId", "selectObj"],
  watch: {
    currentId: {
      immediate: true,
      handler(val) {
        if (val == "") {
          this.resetForm();
        } else {
          this.initDetailInfo(val);
        }
      }
    }
  },
  methods: {
    async initDepartmentData() {
      let data = await orgList();
      this.departmentData = data.page_data;
    },
    async initUserData() {
      let obj = {
        page: 1,
        limit: 99999,
        dept_id: this.dialogForm.department_id
      }
      let data = await userList(obj);
      this.userData = data.page_data;
    },
    handleChange() {
      this.departmentData.map((item) => {
        if (item.uo_id == this.dialogForm.department_id) {
          this.dialogForm.department_name = item.uo_name;
        }
      })
      this.userData = [];
      this.dialogForm.xm_zrr_id = "";
      this.dialogForm.xm_zrr = "";
      this.initUserData();
    },
    handleUserData() {
      this.userData.map((item) => {
        if (item.user_id == this.dialogForm.xm_zrr_id) {
          this.dialogForm.xm_zrr = item.user_realname;
        }
      })
    },
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();
    },
    async initDetailInfo(id) {
      let res = await xmgl_xmzlkDetail({ xm_id: id });
      for (let key in this.dialogForm) {
        this.$set(this.dialogForm, key, res.one_info[key]);
      }
    },
    commitData() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          let data = {
            issubmit: '1',
            jdbg_xmgl_xmzlk: this.dialogForm,
          };
          let obj = {};
          if (this.currentId == "") {
            obj = await xmgl_xmzlkAdd(data);
          } else {
            data = Object.assign({ xm_id: this.currentId }, data);
            obj = await xmgl_xmzlkEdit(data);
          }
          if (obj) {
            this.$message.success("保存成功！");
            this.$emit("close", 1);
          }
        }
      })
    }
  },
  created() {
    this.initDepartmentData();
    this.initUserData();
  }
}
</script>

<style>
</style>