<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="110px" label-position="left">
      <el-form-item label="指标名称" prop="jddr_name">
        <el-input v-model="dialogForm.jddr_name"></el-input>
      </el-form-item>
      <el-form-item label="指标内容" prop="jddr_content">
        <el-input v-model="dialogForm.jddr_content" type="textarea" rows="5"></el-input>
      </el-form-item>
      <el-form-item label="指标说明" prop="jddr_brief">
        <el-input v-model="dialogForm.jddr_brief" type="textarea" rows="5"></el-input>
      </el-form-item>
      <el-form-item label="指标分数" prop="jddr_score">
        <el-input v-model="dialogForm.jddr_score" type="number" :min="0"></el-input>
      </el-form-item>
      <el-form-item label="附件数量" prop="jddr_fjsl">
        <el-input v-model="dialogForm.jddr_fjsl" type="number" :min="0"></el-input>
      </el-form-item>
      <el-form-item label="计划起始时间" prop="jddr_start_date">
        <el-date-picker v-model="time" type="daterange" @change="handleTimeChange" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="责任部门" prop="jddr_uo_id">
        <el-select v-model="dialogForm.jddr_uo_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>
  </div>
</template>

<script>
import { rwzbAdd, rwzbEdit, rwzbDetail, orgList } from "./api";
import { dealWithDate } from "@/utils/common-util.js";
export default {
  data() {
    return {
      departmentData: [],
      dialogForm: {
        jddr_name: "",
        jddr_content: "",
        jddr_brief: "",
        jddr_score: "",
        jddr_fjsl: "",
        jddr_start_date: "",
        jddr_end_date: "",
        jddr_uo_id: "",
        jddr_uo_name: "",
        jdd_id: "",
      },
      time: [],
      rules_check: {
        jddr_name: [{ required: true, message: '请输入指标名称', trigger: 'blur' }],
        jddr_content: [{ required: true, message: '请输入指标内容', trigger: 'blur' }],
        jddr_brief: [{ required: true, message: '请输入指标说明', trigger: 'blur' }],
        jddr_score: [{ required: true, message: '请输入指标分数', trigger: 'blur' }],
        jddr_fjsl: [{ required: true, message: '请输入附件数量', trigger: 'blur' }],
        jddr_start_date: [{ required: true, message: '请选择计划起始时间', trigger: 'change' }],
        jddr_uo_id: [{ required: true, message: '请选择责任部门', trigger: 'change' }],
      },
    }
  },
  props: ["parentId", "currentId"],
  watch: {
    currentId: {
      immediate: true,
      handler(val) {
        if (val == "") {
          this.resetForm();
        } else {
          this.initDetailInfo(val);
        }
      }
    }
  },
  methods: {
    async initDepartmentData() {
      let data = await orgList({ uschool: "1" });
      this.departmentData = data.page_data;
    },
    handleChange() {
      this.departmentData.map((item) => {
        if (item.uo_id == this.dialogForm.jddr_uo_id) {
          this.dialogForm.jddr_uo_name = item.uo_name;
        }
      })
    },
    handleTimeChange(val) {
      if (val) {
        this.dialogForm.jddr_start_date = dealWithDate(val[0]);
        this.dialogForm.jddr_end_date = dealWithDate(val[1]);
      } else {
        this.dialogForm.jddr_start_date = "";
        this.dialogForm.jddr_end_date = "";
      }
    },
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();
      this.time = [];
    },
    async initDetailInfo(id) {
      let res = await rwzbDetail({ jddr_id: id });
      for (let key in this.dialogForm) {
        this.$set(this.dialogForm, key, res.one_info[key]);
      }
      this.time = [this.dialogForm.jddr_start_date, this.dialogForm.jddr_end_date];
    },
    commitData() {
      this.dialogForm.jdd_id = this.parentId;
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          let data = {
            issubmit: '1',
            jdbg_dddx_ddrw_rwzb: this.dialogForm,
          };
          let obj = {};
          if (this.currentId == "") {
            obj = await rwzbAdd(data);
          } else {
            data = Object.assign({ jddr_id: this.currentId }, data);
            obj = await rwzbEdit(data);
          }
          if (obj) {
            this.$message.success("保存成功！");
            this.$emit("close", 1);
          }
        }
      })
    },
  },
  created() {
    this.initDepartmentData();
  }
}
</script>

<style>
</style>