<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="100px" label-position="left">
      <el-form-item label="报表名称" prop="bb_name">
        <el-input v-model="dialogForm.bb_name"></el-input>
      </el-form-item>
      <el-form-item label="选择年份" prop="bb_year">
        <el-select v-model="year" filterable multiple placeholder="请选择" style="width:100%" @change="handleYearChange">
          <el-option v-for="item in yearData" :key="item.value" :label="item.label" :value="item.value">
          </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 label="全部" value="0"></el-option>
          <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="bb_type">
        <el-select v-model="dialogForm.bb_type" placeholder="请选择" style="width:100%">
          <el-option label="全部" value="0"></el-option>
          <el-option v-for="item in selectObj.bb_type.option" :key="item.v" :label="item.n" :value="item.v"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { orgList, tjbbAdd, tjbbEdit, tjbbDetail } from "./api";
import { useUserStore } from "@/stores/user";
const userInfo = useUserStore();
export default {
  data() {
    return {
      year: [],
      dialogForm: {
        bb_name: "",
        bb_year: "",
        department_id: "",
        department_name: "",
        bb_type: "",
        create_user_name: userInfo.real_name,
      },
      rules_check: {
        bb_name: [{ required: true, message: '请输入报表名称', trigger: 'blur' }],
        bb_year: [{ required: true, message: '请选择年份', trigger: 'change' }],
        department_id: [{ required: true, message: '请选择科室', trigger: 'change' }],
        bb_type: [{ required: true, message: '请选择项目类别', trigger: 'change' }],
      },
      departmentData: [],
    }
  },
  props: ["currentId", "selectObj", "yearData"],
  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;
    },
    handleChange() {
      if (this.dialogForm.department_id == 0) {
        this.dialogForm.department_name = "全部";
        return
      }
      this.departmentData.map((item) => {
        if (item.uo_id == this.dialogForm.department_id) {
          this.dialogForm.department_name = item.uo_name;
        }
      })
    },
    handleYearChange() {
      this.dialogForm.bb_year = this.year.join(",");
    },
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();

      this.year = [];
    },
    async initDetailInfo(id) {
      let res = await tjbbDetail({ bb_id: id });
      for (let key in this.dialogForm) {
        this.$set(this.dialogForm, key, res.one_info[key]);
      }
      this.year = this.dialogForm.bb_year.split(",");
    },
    commitData() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          let data = {
            issubmit: '1',
            jdbg_xmgl_tjbb: this.dialogForm,
          };
          let obj = {};
          if (this.currentId == "") {
            obj = await tjbbAdd(data);
          } else {
            data = Object.assign({ bb_id: this.currentId }, data);
            obj = await tjbbEdit(data);
          }
          if (obj) {
            this.$message.success("保存成功！");
            this.$emit("close", 1);
          }
        }
      })
    }
  },
  created() {
    this.initDepartmentData();
  }
}
</script>

<style>
</style>