<template>
  <div class="oa-sys-list-view">
    <div class="searchBox">
      <div class="form">
        <span class="formSpan">报销单号：</span>
        <el-input v-model="queryParams.travelCostNo" placeholder="请输入报销单号" />
      </div>
      <div class="form">
        <span class="formSpan">报销人：</span>
        <UserOrgTree v-model="queryParams.userId" placeholder="请选择报销人" />
      </div>
      <div class="form">
        <span class="formSpan">报销人部门：</span>
        <DeptTree v-model="queryParams['deptId']" placeholder="请选择部门" check-strictly />
      </div>
      <div class="form">
        <span class="formSpan">项目部门：</span>
        <DeptTree v-model="queryParams['projectDeptId']" placeholder="请选择部门" check-strictly />
      </div>
      <div class="form" style="width: 380px">
        <span class="formSpan">申请时间：</span>
        <el-date-picker
          v-model="queryParams.applyTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          unlink-panels
          range-separator="到"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </div>
      <div class="form">
        <span class="formSpan">状态：</span>
        <el-select v-model="queryParams.status" placeholder="状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.FINANCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </div>
      <div class="form">
        <span class="formSpan">报销总金额：</span>
        <el-input v-model="queryParams.totalAmount" placeholder="请输入报销总金额" />
      </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.toFixed(2) }}</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="travelCostNo"
          label="报销单号"
        />
        <el-table-column align="center" prop="userNickname" label="报销人" />
        <el-table-column
          show-overflow-tooltip="true"
          align="center"
          prop="deptName"
          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="projectNo"
          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" label="差旅起止时间" width="240">
          <template #default="scope">
            <span>{{ dateFormatter2(null, null, scope.row.startTime) }}</span>
            -
            <span>{{ dateFormatter2(null, null, scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="status" label="状态" />
        <el-table-column align="center" label="操作" fixed="right" width="80">
          <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 { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as TravelCostApi from '@/api/oa/travelCost'
import * as DeptApi from '@/api/system/dept'
import { dateFormatter2 } from '@/utils/formatTime'
import { handleTree } from '@/utils/tree'
import TableLayout from '../../oaViews/layout/TableLayout.vue'
import DeptTree from '@/views/OaSystem/components/DeptTree/index.vue'
import UserOrgTree from '@/views/OaSystem/components/UserOrgTree/index.vue'

defineOptions({ name: 'ClfbxPage' })
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  travelCostNo: undefined,
  userId: undefined,
  deptId: undefined,
  applyTime: [],
  projectDeptId: undefined,
  costType: undefined,
  status: undefined,
  totalAmount: undefined
})
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 deptList = ref<Tree[]>([]) // 树形结构
const userList = ref<Tree[]>([]) // 树形结构
const router = useRouter()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TravelCostApi.getTravelCostPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 用户列表 */
const initTreeUsers = async () => {
  DeptApi.getSimpleDeptUserAllList().then((res) => {
    userList.value = handleTree(res)
  })
}

/** 部门列表 */
const initTreeDeps = async () => {
  DeptApi.getSimpleDeptList().then((res) => {
    deptList.value = handleTree(res)
  })
}

/** 查询概况 */
const getOverview = async () => {
  const data = await TravelCostApi.getTravelCostOverview(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 operateClick = (row: any) => {
  router.push({
    path: '/processLook',
    query: {
      url:
        `${
          import.meta.env.VITE_PROCESS_DETAIL_URI
        }/IFlowInstance/redirectFlowPage?flowInstanceId=` + row.instanceId
    }
  })
}

/** 分页操作 */
const handleCurrentChange = (pageNo: number) => {
  queryParams.pageNo = pageNo
  getList()
}

/** 初始化 */
onMounted(() => {
  initTreeDeps()
  initTreeUsers()
  getList()
  getOverview()
})
</script>
