<!--
 * @Author: ysh
 * @Date: 2025-02-20 10:20:22
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-02-27 10:49:10
-->
<template>
  <view class="form-container">
    <!-- 表单标题 -->
    <view class="form-title">
      <text class="title-text">借款审批</text>
      <view class="title-line"></view>
    </view>
    <!-- 表单内容 -->
    <uni-forms ref="form" :modelValue="form" :rules="rules" label-position="top" label-width="150" class="custom-form">
      <!-- 当前节点 -->
      <uni-forms-item label="当前节点" class="form-item">
        <uni-tag :inverted="true" type="primary" :text="taskName"></uni-tag>
      </uni-forms-item>

      <!-- 流程发起人 -->
      <uni-forms-item label="填报人" class="form-item">
        <uni-tag :inverted="true" type="primary" :text="startUserName"></uni-tag>
      </uni-forms-item>

      <!-- 填报日期 -->
      <uni-forms-item label="填报日期" class="form-item">
        <text>{{ form.applyDate }}</text>
      </uni-forms-item>

      <!-- 借款类型 -->
      <uni-forms-item label="借款类型" required class="form-item" name="borrowUsage">
        <uni-data-checkbox v-model="form.borrowUsage" :localdata="borrowUsageOptions"></uni-data-checkbox>
      </uni-forms-item>

      <!-- 借款类型 -->
      <uni-forms-item label="借款事由" required class="form-item" name="applyReason" v-if="form.borrowUsage != 0">
        <uv-textarea v-model="form.applyReason" placeholder="请输入借款事由"></uv-textarea>
      </uni-forms-item>

      <!-- 选择项目 -->
      <uni-forms-item label="选择项目" required class="form-item" v-if="form.borrowUsage == 0" name="projectId">
        <u-button type="primary" @click="openProject = true" v-if="taskName == '借款申请'">选择项目</u-button>
        <ProjectPicker :visible.sync="openProject" :selected.sync="selectedProject" @confirm="handleConfirm" />
        <ProjectInfo :project="projectObj"></ProjectInfo>
      </uni-forms-item>

      <!-- 借款明细 -->
      <uni-forms-item label="借款明细" required class="form-item">
        <BorrowDetail :borrowId="form.borrowId"></BorrowDetail>
      </uni-forms-item>

    </uni-forms>


  </view>
</template>

<script>
import ProjectPicker from '@/pages/components/ProjectPicker.vue';
import ProjectInfo from '@/pages/components/ProjectInfo.vue';
import { parseTime } from "@/utils/common.js"
import { listBorrow, getBorrow, delBorrow, addBorrow, updateBorrow } from "@/api/oa/borrow/borrow";
import { listProject, getProject } from "@/api/oa/project/project";
import BorrowDetail from './borrowDetail.vue';
export default {
  components: {
    ProjectPicker,
    ProjectInfo,
    BorrowDetail
  },
  props: {
    taskForm: Object,
    taskName: String, // 当前节点
    startUserName: String, // 流程发起人
  },
  created() {
    this.initForm();
    uni.setNavigationBarTitle({
      title: '借款审批'
    });
  },
  data() {
    return {
      form: {
        submitTime: '',
        borrowUsage: '0',
      },
      rules: {},
      borrowUsageOptions: [{
        text: '项目借款',
        value: '0',
        disable: false
      }, {
        text: '非项目借款',
        value: '1',
        disable: false
      }, {
        text: '工会借款',
        value: '2',
        disable: false
      }, {
        text: '党委借款',
        value: '3',
        disable: false
      }, {
        text: '团委借款',
        value: '4',
        disable: false
      }
      ],
      openProject: false,
      selectedProject: null, //选中的项目
      projectObj: {}, //项目信息的项目
      fromTotal: 0,
    }
  },
  methods: {
    initForm() {
      getBorrow(this.taskForm.formId).then(res => {
        if (res.data) {
          this.fromTotal = 1;
          this.form = res.data;
          console.log(this.form);
          if (this.form.projectId) {
            getProject(this.form.projectId).then(res => {
              if (res.data) {
                this.selectedProject = res.data;
                this.projectObj = res.data;
              }
            })
          }
        } else {
          this.fromTotal = 0;
          this.form.applier = this.$store.getters.userId;
          this.form.applyDate = parseTime(new Date(), "{y}-{m}-{d}");
        }
      })
    },
    handleConfirm(project) {
      console.log('选中的项目:', project);
      this.selectedProject = project;
      this.projectObj = project;
    },
  },
}
</script>

<style lang="scss" scoped></style>