<template>
  <div class="oa-sys-list-view">
    <div class="searchBox">
      <div class="form">
        <span class="formSpan">用款单号：</span>
        <el-input
          v-model="queryParams.useMoneyNo"
          placeholder="请输入报销单号"
          style="width: 210px"
        />
      </div>
      <div class="form">
        <span class="formSpan">项目部门：</span>
        <el-input
          v-model="queryParams.projectDeptId"
          placeholder="请输入项目部门"
          style="width: 160px"
        />
      </div>
      <div class="form" style="width: 380px">
        <span class="formSpan">申请时间：</span>
        <el-date-picker
          v-model="applyDate"
          type="daterange"
          unlink-panels
          range-separator="到"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeApplyDate"
        />
      </div>
      <div class="from">
        <div class="btnBox">
          <el-button type="primary" style="background: #3485ff" @click="handleQuery">
            <img src="@/assets/imgs/OA/search.png" class="mr-8px" alt="" />
            查询</el-button
          >
        </div>
      </div>
    </div>
    <div class="infoBox">
      <ul>
        <li v-for="(item, index) in infoList" :key="index" class="mr-40px">
          <p>{{ item.name }}：</p>
          <h4 class="font-size-18px">{{ item.num }}</h4>
        </li>
      </ul>
    </div>
    <div class="tableBox">
      <TableLayout :is-loading="loading" :data="list">
        <el-table-column type="index" />
        <el-table-column
          show-overflow-tooltip="true"
          align="center"
          prop="useMoneyNo"
          label="用款单号"
        />
        <el-table-column
          align="center"
          prop="applyTime"
          :formatter="dateFormatter2"
          label="申请时间"
        />
        <el-table-column align="center" prop="totalAmount" label="用款总金额" />
        <el-table-column
          show-overflow-tooltip="true"
          align="center"
          prop="projectName"
          label="项目名称"
        />
        <el-table-column
          show-overflow-tooltip="true"
          align="center"
          prop="projectDeptName"
          label="项目部门"
        />
        <el-table-column
          align="center"
          prop="agreedPayTime"
          :formatter="dateFormatter2"
          label="约定付款期限"
        />
        <el-table-column align="center" prop="status" label="状态" />
        <el-table-column align="center" label="操作" width="80" fixed="right">
          <template #default="scope">
            <div class="operateBtn" @click="operateClick(scope.row)">
              <span>查看</span>
            </div>
          </template>
        </el-table-column>
      </TableLayout>

      <div class="pageBox">
        <el-pagination
          v-model:current-page="queryParams.pageNo"
          :page-size="10"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import * as UseMoneyApi from '@/api/oa/useMoney'
import { dateFormatter2 } from '@/utils/formatTime'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import TableLayout from '../../oaViews/layout/TableLayout.vue'
import { useRouter } from 'vue-router'
import moment from 'moment'

defineOptions({ name: 'UseMoneyDept' })
const { wsCache } = useCache()
const user = wsCache.get(CACHE_KEY.USER)
const userId = user.user.id ? user.user.id : ''
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  commonCostNo: undefined,
  userId: userId,
  deptId: undefined,
  applyTime: [],
  projectDeptId: undefined,
  costType: undefined,
  flowStatus: undefined,
  totalAmount: undefined,
  applyOn: '',
  applyOff: ''
})
const infoList: any = ref([
  {
    name: '用款总金额(元)',
    num: '0'
  },
  {
    name: '已用款总金额(元)',
    num: '0'
  },
  {
    name: '申请用款总金额(元)',
    num: '0'
  }
])
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数

/** 查询列表 */
const getList = async () => {
  loading.value = true
  if (applyDate.value && applyDate.value.length > 0) {
    queryParams.applyOn = moment(applyDate.value[0]).format('YYYY-MM-DD')
    queryParams.applyOff = moment(applyDate.value[1]).format('YYYY-MM-DD')
  }
  try {
    const data = await UseMoneyApi.getUseMoneyPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const changeApplyDate = (value, data) => {
  if (!value) {
    queryParams.applyOn = ''
    queryParams.applyOff = ''
  }
}
const applyDate = ref()
/** 查询概况 */
const getOverview = async () => {
  if (applyDate.value && applyDate.value.length > 0) {
    queryParams.applyOn = moment(applyDate.value[0]).format('YYYY-MM-DD')
    queryParams.applyOff = moment(applyDate.value[1]).format('YYYY-MM-DD')
  }
  const data = await UseMoneyApi.getUseMoneyOverview(queryParams)
  infoList.value[0].num = data.totalAmount
  infoList.value[1].num = data.totalUseAmount
  infoList.value[2].num = data.totalApplyAmount
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
  getOverview()
}

/** 分页操作 */
const handleCurrentChange = (pageNo: number) => {
  queryParams.pageNo = pageNo
  getList()
}
const router = useRouter()
const operateClick = (row: any) => {
  router.push({
    path: '/processLook',
    query: {
      url:
        `${
          import.meta.env.VITE_PROCESS_DETAIL_URI
        }/IFlowInstance/redirectFlowPage?flowInstanceId=` + row.instanceId
    }
  })
}
/** 初始化 */
onMounted(() => {
  getList()
  getOverview()
})
</script>
