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

          <el-form-item label="支出类别" prop="expendType">
            <el-select v-model="expendFrom.expendType" 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="expendMoney">
            <el-input v-model="expendFrom.expendMoney"></el-input>
          </el-form-item>

          <el-form-item label="支出时间" prop="expendTime">
            <el-date-picker
              v-model="expendFrom.expendTime"
              type="datetime"
              placeholder="选择日期时间"
              @change="timestampToTime()"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="支出对象" prop="expendObject">
            <el-input v-model="expendFrom.expendObject"></el-input>
          </el-form-item>

          <el-form-item label="支出用途" prop="expendBody">
            <el-input v-model="expendFrom.expendBody"></el-input>
          </el-form-item>

          <el-form-item label="支出附言" prop="expendRemarks">
            <el-input v-model="expendFrom.expendRemarks"></el-input>
          </el-form-item>

          <el-form-item label="支出详情" prop="expendInformation">
            <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8 }"
              v-model="expendFrom.expendInformation"
            ></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="deleteExpend()"
              >删除</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/expend";
import { money } from "@/utils/myValidate";
export default {
  data() {
    return {
      expendFrom: {
        expendBody: null,
        expendInformation: null,
        expendMoney: null,
        expendObject: null,
        expendRemarks: null,
        expendTime: null,
        expendType: null,
        projectId: null,
        userId: null,
        projectName: null,
      },
      typeList: [],
      projectData: {},
      rules: {
        projectName: [
          { required: true, message: "请选择项目", trigger: "change" },
        ],
        expendType: [
          { required: true, message: "请选择支出类型", trigger: "change" },
        ],
        expendMoney: [
          {
            validator: money,
            trigger: "change"
          }
        ],
        expendTime: [
          { required: true, message: "请选择支出时间", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    expendId() {
      return this.$route.query.id;
    },
  },
  created() {
    this.expendFrom.userId = this.$store.getters.urId;
    this.getTypeList();
    this.getexpend();
  },
  methods: {
    //获取支出类型
    getTypeList() {
      let param = {
        companyId: this.$store.getters.companyId,
        typeCategory: 1,
      };
      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()
        });
    },
    //获取支出信息
    getexpend(){
      selectById({expendId:this.expendId}).then((res)=>{
        if(res.success){
          this.expendFrom = res.data;
          this.expendFrom.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.expendFrom)
            .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: "/expendList",
        query: {
          id: this.$route.query.projectId,
        },
      });
    },
    //时间处理 yyyy-MM-dd hh:mm:ss
    timestampToTime() {
      var date = this.expendFrom.expendTime;
      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.expendFrom.expendTime = Y + M + D + h + m + s;
    },
    //删除
    deleteExpend(){
      this.$confirm("确定要删除此收入记录吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        del(this.expendFrom)
          .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>