<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="100px" label-position="left">
      <el-form-item label="报表名称" prop="jdtj_name">
        <el-input v-model="dialogForm.jdtj_name"></el-input>
      </el-form-item>
      <el-form-item label="选择年份" prop="jdtj_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>
  </div>
</template>

<script>
import { dddxtjbbAdd, dddxtjbbEdit, dddxtjbbDetail } from "./api";
import { useUserStore } from "@/stores/user";
import { dealWithDate } from "@/utils/common-util.js";
const userInfo = useUserStore();
export default {
  data() {
    return {
      year: [],
      dialogForm: {
        jdtj_name: "",
        jdtj_year: "",
        jdtj_date: dealWithDate(new Date()),
        jdtj_cjr: userInfo.real_name,
      },
      rules_check: {
        jdtj_name: [{ required: true, message: '请输入报表名称', trigger: 'blur' }],
        jdtj_year: [{ required: true, message: '请选择年份', trigger: 'change' }],
      },
      departmentData: [],
    }
  },
  props: ["currentId", "yearData"],
  watch: {
    currentId: {
      immediate: true,
      handler(val) {
        if (val == "") {
          this.resetForm();
        } else {
          this.initDetailInfo(val);
        }
      }
    }
  },
  methods: {
    handleYearChange() {
      this.dialogForm.jdtj_year = this.year.join(",");
    },
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();

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

<style>
</style>