
<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="900px"
    >
      <div class="user-panel" v-loading="loading">
        <el-form
          ref="form"
          :model="formModel"
          :rules="ruleValidate"
          :label-width="'100px'"
          :inline="true"
        >
        <el-form-item label="序号" prop="serialNum">
            <el-input v-model="formModel.serialNum" placeholder="请输入序号" style="width: 300px"></el-input>
          </el-form-item>
        <el-form-item label="名称" prop="name">
            <el-input v-model="formModel.name" placeholder="请输入名称" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="预算收入" prop="budgetRevenue">
            <el-input-number v-model="formModel.budgetRevenue" placeholder="请输入预算收入" style="width: 300px"></el-input-number>
          </el-form-item>
          <el-form-item label="是否合计" prop="isSum">
            <el-select v-model="formModel.isSum" placeholder="请选择" style="width:300px">
              <el-option
                v-for="item in isSumList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="收入时间区间" prop="incomeTimeRangeArr">
            <el-date-picker
              style="width: 300px"
              v-model="formModel.incomeTimeRangeArr"
              type="monthrange"
              range-separator="至"
              start-placeholder="开始月份"
              end-placeholder="结束月份"
              value-format='yyyy-MM'
              format='yyyy-MM'
              >
            </el-date-picker>
          </el-form-item>
          <!-- <el-form-item label="收入类型" prop="incomeType">
            <el-select v-model="formModel.incomeType" clearable placeholder="请选择" style="width: 300px" >
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value">
                </el-option>
              </el-select>
          </el-form-item> -->
          <el-form-item label="终止日" prop="endDate">
            <el-date-picker
              style="width: 300px"
              v-model="formModel.endDate"
              type="date"
              placeholder="选择日期"
              value-format='yyyy-MM-dd'
              format='yyyy-MM-dd'>
            </el-date-picker>
          </el-form-item>
          <el-form-item label="责任人" prop="chargePerson">
            <el-input style="width: 300px" v-model="formModel.chargePerson" placeholder="请输入责任人"></el-input>
          </el-form-item>
          <el-form-item label="排序号" prop="index">
            <el-input-number v-model="formModel.index" placeholder="请输入排序号" style="width: 300px"></el-input-number>
          </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 templateOptionApi from "@/api/base/templateOption";
  
  export default {
    props: ["businessKey2", "title"],
    data() {
      return {
          showPwd: true,
          ruleValidate: {
            serialNum: [
              { required: true, message: "序号不能为空", trigger: "blur" },
              ],
              name: [
              { required: true, message: "名称不能为空", trigger: "blur" },
              ],
              budgetRevenue: [
              { required: true, message: "预算收入不能为空", trigger: "blur" },
              ],
              isSum: [
              { required: true, message: "是否合计不能为空", trigger: "blur" },
              ],
              index: [
              { required: true, message: "排序号不能为空", trigger: "blur" },
              ],
          },
          roleList: [],
          formModel: {},
          showDialog: true,
          loading: false,
          submitting: false,
          typeList:[
            {
                "name":"收入",
                "value":"1",
            },
            {
                "name":"支出",
                "value":"2",
            }
          ],
          isSumList:[
            {
              "label":"否",
              "value":false
            },
            {
              "label":"是",
              "value":true
            }]
      };
    },
    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 templateOptionApi.addAndFundIncome(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 () {
        return templateOptionApi.createFundIncome(self.businessKey2);
        
      })()
        .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>