
<template>
    <el-dialog
      :visible.sync="showDialog"
      :title="title"
      :modal-append-to-body="true"
      style="text-align:left;"
      :close-on-click-modal="true"
      @close="closeDialog"
      :append-to-body="true"
      width="600px"
    >
      <div class="user-panel" v-loading="loading">
        <el-form
          ref="form"
          :model="formModel"
          :rules="ruleValidate"
          :label-width="'150px'"
          :inline="true"
        >
          <el-form-item label="本月到账金额" prop="currentAmount">
            <el-input-number v-model="formModel.currentAmount" placeholder="请输入本月到账金额" style="width: 300px"></el-input-number>
          </el-form-item>
          <el-form-item label="到账时间" prop="createTime">
            <el-date-picker
              style="width: 300px"
              v-model="formModel.createTime"
              type="datetime"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss">
            </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 incomeModifyRecordApi from "@/api/base/incomeModifyRecord";
  
  export default {
    props: ["businessKey2", "title"],
    data() {
      return {
          showPwd: true,
          ruleValidate: {
            currentAmount: [
              { required: true, message: "本月到账金额不能为空", trigger: "blur" },
              ],
              createTime: [
              { required: true, message: "到账时间不能为空", 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;
  
              if (id == null || id.length == 0) {
                return incomeModifyRecordApi.add(self.formModel);
              } else {
                return incomeModifyRecordApi.update(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);
              }
            });
          }
        });
      },
      handleAvatarSuccess(res, file) {
          var self = this;
          self.formModel.photo = res.data;
  
          self.fileUrl =
            res.data + "?x-oss-process=image/resize,m_lfit,h_300,w_300";
      },
      beforeAvatarUpload(file) {
          const isLt2M = file.size / 1024 / 1024 < 2;
          if (!isLt2M) {
              this.$message.error('上传头像图片大小不能超过 2MB!');
          }
          return isLt2M;
      }
    },
    async mounted() {
      var self = this;
      self.loading = true;
  
      (function () {
        if (self.businessKey2 != null && self.businessKey2.length > 0) {
          return incomeModifyRecordApi.edit(self.businessKey2);
        } else {
          return incomeModifyRecordApi.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>