<template>
  <div class="container">
    <el-row>
      <el-col :xl="8" :lg="8" :md="8" :sm="12" :xs="24">
        <el-form
          ref="form"
          :model="incomeFrom"
          label-width="120px"
          :rules="rules"
        >
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="incomeFrom.projectName" disabled></el-input>
          </el-form-item>

          <el-form-item label="收入类别" prop="incomeType">
            <el-select v-model="incomeFrom.incomeType" placeholder="请选择">
              <el-option
                v-for="item in typeList"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="收入金额" prop="incomeMoney">
            <el-input v-model="incomeFrom.incomeMoney"></el-input>
          </el-form-item>

          <el-form-item label="收到时间" prop="incomeTime">
            <el-date-picker
              v-model="incomeFrom.incomeTime"
              type="datetime"
              placeholder="选择日期时间"
              @change="timestampToTime()"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="收入对象" prop="incomeObject">
            <el-input v-model="incomeFrom.incomeObject"></el-input>
          </el-form-item>

          <el-form-item label="收入内容" prop="incomeBody">
            <el-input v-model="incomeFrom.incomeBody"></el-input>
          </el-form-item>

          <el-form-item label="收入备注" prop="incomeRemarks">
            <el-input v-model="incomeFrom.incomeRemarks"></el-input>
          </el-form-item>

          <el-form-item label="收入详情" prop="incomeInformation">
            <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8 }"
              v-model="incomeFrom.incomeInformation"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-upload2"
              @click="submitForm()"
              >提交</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete-solid"
              @click="deleteIncome()"
              >删除</el-button
            >
            <el-button @click="back()">返回</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { selectAll } from "@/api/type";
import { update, selectById, del } from "@/api/imcome";
export default {
  data() {
    return {
      incomeFrom: {
        incomeBody: null,
        incomeInformation: null,
        incomeMoney: null,
        incomeObject: null,
        incomeRemarks: null,
        incomeTime: null,
        incomeType: null,
        projectId: null,
        userId: null,
        projectName: null,
      },
      typeList: [],
      projectData: {},
      rules: {
        projectName: [
          { required: true, message: "请选择项目", trigger: "change" },
        ],
        incomeType: [
          { required: true, message: "请选择收入类型", trigger: "change" },
        ],
        incomeMoney: [
          { required: true, message: "请输入收入金额", trigger: "blur" },
        ],
        incomeTime: [
          { required: true, message: "请选择收入时间", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    incomeId() {
      return this.$route.query.id;
    },
  },
  created() {
    this.incomeFrom.userId = this.$store.getters.urId;
    this.getTypeList();
    this.getIncome();
  },
  methods: {
    //获取收入类型
    getTypeList() {
      let param = {
        companyId: this.$store.getters.companyId,
        typeCategory: 0,
      };
      selectAll(param)
        .then((res) => {
          if (res.success) {
            this.typeList = res.data;
          } else {
            this.$message.error(res.msg);
            this.back();
          }
        })
        .catch((error) => {
          this.$message.error("获取类型失败");
          this.back();
        });
    },
    //获取收入信息
    getIncome() {
      selectById({ incomeId: this.incomeId })
        .then((res) => {
          if (res.success) {
            this.incomeFrom = res.data;
            this.incomeFrom.userId = this.$store.getters.urId;
          } else {
            this.$message.error(res.msg);
            this.back();
          }
        })
        .catch((error) => {
          this.$message.error("获取信息失败");
          this.back();
        });
    },
    //提交
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          update(this.incomeFrom)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.msg);
                this.back();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((error) => {
              this.$message.error("新建失败");
            });
        }
      });
    },
    //返回
    back() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({
        path: "/incomeList",
        query: {
          id: this.$route.query.projectId,
        },
      });
    },
    //时间处理 yyyy-MM-dd hh:mm:ss
    timestampToTime() {
      var date = this.incomeFrom.incomeTime;
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
      var h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var m =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      this.incomeFrom.incomeTime = Y + M + D + h + m + s;
    },
    //删除
    deleteIncome() {
      this.$confirm("确定要删除此收入记录吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del(this.incomeFrom)
            .then((res) => {
              if (res.success) {
                this.$message.success("删除成功");
                this.back();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((error) => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
.container {
  padding: 10px;
}
</style>