<!--
 * @Author: ysh
 * @Date: 2024-06-21 18:51:59
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-02-13 11:07:30
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="projectId">
        <el-select v-model="queryType" style="width: 100px;">
          <el-option label="项目编号" value="1"></el-option>
          <el-option label="项目名称" value="2"></el-option>
        </el-select>
        <el-select v-model="queryParams.projectId" clearable filterable remote reserve-keyword placeholder="请输入关键字"
          :remote-method="remoteMethod" :loading="loading" style="width: 300px;">
          <el-option v-for="project in projectList" :key="project.projectId"
            :label="project.projectNumber + '-' + project.projectName" :value="project.projectId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="借款人" prop="applier">
        <el-select v-model="queryParams.applier" filterable clearable @change="handleQuery">
          <el-option v-for="user in $store.state.user.userList" :key="user.userId" :label="user.nickName"
            :value="user.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="借款类型" prop="borrowUsage">
        <el-select v-model="queryParams.borrowUsage" filterable clearable @change="handleQuery">
          <el-option label="项目借款" value="0">
          </el-option>
          <el-option label="非项目借款" value="1">
          </el-option>
          <el-option label="工会借款" value="2">
          </el-option>
          <el-option label="党委借款" value="3">
          </el-option>
          <el-option label="团委借款" value="4">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- <el-tabs v-model="activeName" type="card" @tab-click="handleClick" v-if="roles.some(element => element === 'dept')">
      <el-tab-pane label="项目借款" name="0">
        <borrow-table :borrowUsage="activeName" @clickView="handleView"></borrow-table>
      </el-tab-pane>
      <el-tab-pane label="非项目借款" name="1"></el-tab-pane>
      <el-tab-pane label="工会借款" name="2"></el-tab-pane>
      <el-tab-pane label="党委借款" name="3"></el-tab-pane>
      <el-tab-pane label="团委借款" name="4"></el-tab-pane>
    </el-tabs> -->
    <el-table v-loading="loading" :data="borrowList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="借款类型" align="center" prop="borrowUsage">
        <template slot-scope="scope">
          <el-tag :type="setTagType(scope.row.borrowUsage)">
            {{ setBorrowUsage(scope.row.borrowUsage) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="借款事由" align="center" prop="applyReason" :formatter="setReasonByUsage" />
      <el-table-column label="申请金额" align="center" prop="applyAmount" />
      <el-table-column label="核准金额" align="center" prop="managerAmount" />
      <el-table-column label="借款人" align="center" prop="applier">
        <template slot-scope="scope">
          {{ getUserName(scope.row.applier) }}
        </template>
      </el-table-column>
      <el-table-column label="借款人说明" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="借款日期" align="center" prop="applyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借款支付时间" align="center" prop="lendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['oa:borrow:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:borrow:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改cmc借款申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="64%" append-to-body>
      <borrow-form :taskForm="taskForm" :taskName="''" :flowDisabled="false"></borrow-form>
    </el-dialog>
  </div>
</template>

<script>
import { listBorrow, getBorrow, delBorrow, addBorrow, updateBorrow } from "@/api/oa/borrow/borrow";
import { listProject } from '@/api/oa/project/project';
import BorrowForm from '../../flowable/form/finance/borrowForm.vue';
import { mapGetters } from 'vuex'
import BorrowTable from './borrowTable.vue';
export default {
  name: "Borrow",
  components: {
    BorrowForm,
    BorrowTable
  },
  computed: {
    ...mapGetters(['roles', 'deptId']),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc借款申请表格数据
      borrowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        borrowType: null,
        applyReason: null,
        applier: null,
        applyDate: null,
        applyAmount: null,
        managerAmount: null,
        deptComment: null,
        deptUserId: null,
        xmUserId: null,
        xmComment: null,
        managerComment: null,
        managerUserId: null,
        zjlUserId: null,
        zjlComment: null,
        cwUserId: null,
        cwComment: null,
        deptTime: null,
        xmTime: null,
        managerTime: null,
        zjlTime: null,
        lendTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      projectList: [],
      taskForm: {
        formId: '',
        procDefName: '借款审批'
      },
      activeName: '0',
      queryType: '1',
    };
  },
  created() {
    this.queryParams.borrowUsage = '0'
    this.getList();
  },
  methods: {
    /** 查询cmc借款申请列表 */
    getList() {
      this.loading = true;
      if (this.roles.some(element => element === "dept")) {
        if (this.queryParams.borrowUsage == '0') {
          this.queryParams.undertakingDept = this.deptId;
          this.queryParams.applyDept = null;
        }
        else {
          this.queryParams.applyDept = this.deptId;
          this.queryParams.undertakingDept = null;
        }
      }
      if (this.roles.some(e => e === 'leader' || e=== 'finance')) {
        this.queryParams.applyDept = null;
        this.queryParams.undertakingDept = null;
      }
      listBorrow(this.queryParams).then(response => {
        this.borrowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteMethod(val) {
      let params1 = {
        pageNum: 1,
        pageSize: 20,
        projectNumber: val
      }
      let params2 = {
        pageNum: 1,
        pageSize: 20,
        projectName: val
      }
      let params = {};
      if (this.queryType == '1') {
        params = params1
      } else {
        params = params2
      }
      listProject(params).then(res => {
        this.projectList = res.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        borrowJd: null,
        projectId: null,
        borrowType: null,
        applyReason: null,
        applier: null,
        applyDate: null,
        applyAmount: null,
        managerAmount: null,
        deptComment: null,
        deptUserId: null,
        xmUserId: null,
        xmComment: null,
        managerComment: null,
        managerUserId: null,
        zjlUserId: null,
        zjlComment: null,
        cwUserId: null,
        cwComment: null,
        deptTime: null,
        xmTime: null,
        managerTime: null,
        zjlTime: null,
        lendTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.borrowJd)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc借款申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const borrowJd = row.borrowJd || this.ids
      getBorrow(borrowJd).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc借款申请";
      });
    },
    handleView(row) {
      this.open = true;
      this.taskForm.formId = row.borrowId
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.borrowJd != null) {
            updateBorrow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBorrow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const borrowJds = row.borrowJd || this.ids;
      this.$modal.confirm('是否确认删除cmc借款申请编号为"' + borrowJds + '"的数据项？').then(function () {
        return delBorrow(borrowJds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/borrow/export', {
        ...this.queryParams
      }, `borrow_${new Date().getTime()}.xlsx`)
    },
    setBorrowUsage(row) {
      if (row == '0') {
        return '项目借款'
      } else if (row == '1') {
        return '非项目借款'
      } else if (row == '2') {
        return '工会借款'
      } else if (row == '3') {
        return '党委借款'
      } else if (row == '4') {
        return '团委借款'
      }
    },
    setTagType(row) {
      if (row == '0') {
        return 'success'
      } else if (row == '1') {
        return 'warning'
      } else if (row == '2') {
        return ''
      } else if (row == '3') {
        return ''
      } else if (row == '14') {
        return ''
      }
    },
    setReasonByUsage(row) {
      if (row.projectId) {
        return row.project.projectNumber + '-' + row.project.projectName
      } else {
        return row.applyReason
      }
    },
    handleClick() {

    }
  }
};
</script>
