
<template>
    <el-dialog
      :visible.sync="showDialog"
      :title="title"
      :modal-append-to-body="false"
      style="text-align: left;"
      @close="closeDialog"
      :close-on-click-modal="false"
    >
      <div class="user-panel" v-loading="loading">
        <el-form
          ref="form"
          :model="formModel"
          :rules="ruleValidate"
          :label-width="'130px'"
          :inline="true"
        >
        <el-form-item label="所属年份" prop="year">
            <el-date-picker
              v-model="formModel.year"
              type="year"
              placeholder="选择年"
              style="width: 300px"
              value-format="yyyy"
              format="yyyy"
              disabled
              >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="formModel.title" placeholder="请输入标题" style="width: 300px" disabled></el-input>
          </el-form-item>
          <el-form-item label="填写起止月份" prop="dateRangeArr" >
            <el-date-picker
            style="width: 300px"
              v-model="formModel.dateRangeArr"
              type="monthrange"
              range-separator="至"
              start-placeholder="开始月份"
              end-placeholder="结束月份"
              value-format="yyyy-MM"
              format="yyyy-MM">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitting"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </template>
  <script>
  import Constant from "@/constant";
  import { getToken } from "@/utils/auth"; // get token from cookie
  import templateInfoApi from "@/api/base/templateInfo";
  
  export default {
    props: ["businessKey", "title"],
    data() {
       // 自定义验证函数
      const validateDateRange = (rule, value, callback) => {
        var year = this.formModel.year;
        var startStr = year+"-01"+"-01";
        var endStr = year+"-12"+"-31";
        const start = new Date(startStr);
        const end = new Date(endStr);
        const date1 = new Date(value[0]);
        const date2 = new Date(value[1]);
        if(date1 > start||date2 < end){    
          callback(new Error('所选月份范围不能小于所属年份范围'));
        } else {
          callback();
        }
      };
      return {
          showPwd: true,
          ruleValidate: {
              year: [
              { required: true, message: "所属年份不能为空", trigger: "blur" },
              ],
              title: [
              { required: true, message: "标题不能为空", trigger: "blur" },
              ],
              dateRangeArr:[
                { validator: validateDateRange, trigger: 'blur' }
              ]
          },
          roleList: [],
          formModel: {},
          showDialog: true,
          loading: false,
          submitting: false,
          typeList:[
            {
                "name":"收入",
                "value":"1",
            },
            {
                "name":"支出",
                "value":"2",
            }
          ]
      };
    },
    created() {
    },
    methods: {
      closeDialog() {
        this.$emit("close", false);
      },
      handleSubmit() {
        var self = this;
  
        this.$refs["form"].validate((valid) => {
          if (valid) {
            (function () {
              var id = self.formModel.id;

              return templateInfoApi.updateDate(self.formModel); 
            })().then(function (response) {
              var jsonData = response.data;
  
              if (jsonData.result) {
                self.$message({
                  message: "保存成功!",
                  type: "success",
                });
  
                self.$emit("close", true);
              } else {
                self.$message({
                  message: jsonData.message + "",
                  type: "warning",
                });
  
                self.$emit("close", false);
              }
            });
          }
        });
      },
    },
    async mounted() {
      var self = this;
      self.loading = true;
  
      (function () {
        if (self.businessKey != null && self.businessKey.length > 0) {
          return templateInfoApi.edit(self.businessKey);
        } else {
          return templateInfoApi.create();
        }
      })()
        .then((response) => {
          var jsonData = response.data;
  
          if (jsonData.result) {
              self.formModel = jsonData.data;
  
              let photo = self.formModel.photo;
              if (photo != null) {
                  self.fileUrl = photo 
              }
  
              self.showModal = true;
          } else {
            self.$message.error(jsonData.message + "");
          }
  
          self.loading = false;
        })
        .catch((error) => {
          self.$message.error(error + "");
          self.loading = false;
        });
    },
    components: {
    },
  };
  </script>
  <style scoped>
  .user-panel {
    margin: 10px auto;
  }
  .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  </style>