<template>
  <div class="oa-sys-list-view">
    <div class="searchBox">
      <div class="form">
        <span class="formSpan">申请人：</span>
        <el-input v-model="qrs.userId" placeholder="请输入申请人" style="width: 160px" />
      </div>
      <div class="form">
        <span class="formSpan">申请时间：</span>
        <el-date-picker
          v-model="applyDateObj"
          type="daterange"
          unlink-panels
          range-separator="To"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </div>
      <div class="form">
        <span class="formSpan">采购单号：</span>
        <el-input v-model="qrs.procureNo" placeholder="请输入采购单号" style="width: 160px" />
      </div>
      <div class="from">
        <div class="btnBox">
          <el-button type="primary" style="background: #3485ff" @click="searchHandle">
            <img src="@/assets/imgs/OA/search.png" class="mr-8px" alt="" />
            查询</el-button
          >
          <el-button type="primary">
            <img src="@/assets/imgs/OA/open.png" class="mr-8px" alt="" />
            导出</el-button
          >
        </div>
      </div>
    </div>
    <div class="tableBox">
      <TableLayout :is-loading="loading" :data="tableData">
        <el-table-column label="序号" width="60">
          <template #default="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column prop="userNickname" label="申请人" width="80" />
        <el-table-column
          show-overflow-tooltip="true"
          prop="workerDept"
          label="申请部门"
          width="250"
        />
        <el-table-column
          show-overflow-tooltip="true"
          prop="applyTime"
          label="申请时间"
          width="180"
        />

        <el-table-column
          show-overflow-tooltip="true"
          prop="projectName"
          label="项目名称"
          width="350"
        />

        <el-table-column
          show-overflow-tooltip="true"
          prop="procureNo"
          label="采购单号"
          width="240"
        />

        <el-table-column
          show-overflow-tooltip="true"
          prop="totalAmount"
          label="采购总金额"
          width="150"
        />

        <el-table-column
          show-overflow-tooltip="true"
          prop="procureType"
          label="采购类型"
          width="150"
        >
          <template #default="scope">
            {{ procureTypeMap[scope.row.procureType] }}
          </template>
        </el-table-column>

        <el-table-column
          show-overflow-tooltip="true"
          prop="commonCostStatus"
          label="报销状态"
          width="150"
        >
          <template #default="scope">
            {{ costStatusMap[scope.row.commonCostStatus] }}
          </template>
        </el-table-column>
        <el-table-column 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="qrs.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 moment from 'moment'
import { useRouter } from 'vue-router'
import request from '@/config/axios'
import * as DeptApi from '@/api/system/dept'
import { handleTree } from '@/utils/tree'
import { arrFlatten } from '../../attendanceCenter/attendAuth'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import TableLayout from '../../oaViews/layout/TableLayout.vue'

defineOptions({ name: 'DeptPurchase' })
const { wsCache } = useCache()
const user = wsCache.get(CACHE_KEY.USER)
const costStatusMap: any = { 0: '未报销', 1: '报销中', 90: '已报销' }
const procureTypeMap: any = { 1: '正常', 2: '直接录入', 3: '采购（代办）' }
const router = useRouter()
const tableRef: any = ref(null)
const tableHeight: any = ref(0)
const deptId = user.user.deptId ? user.user.deptId : ''
const qrs: any = ref({
  state: '',
  projectDept: '',
  userId: '',
  deptId: deptId,
  applyDateOn: '',
  applyDateOff: '',
  procureNo: '',
  assetNo: '',
  assetName: '',
  assetType: '',
  pageNo: 1,
  pageSize: 10,
  custodianDept: '',
  custodian: '',
  assetModel: ''
})
const deptList = ref<Tree[]>([]) // 树形结构
const deptSort: any = ref([])
const loading = ref(true) // 列表的加载中

const initTreeDeps = async () => {
  DeptApi.getSimpleDeptList().then((res) => {
    deptList.value = handleTree(res)
    deptSort.value = arrFlatten(res, 'children')
  })
}

const handleCurrentChange = (pageNo: number) => {
  qrs.pageNo = pageNo
  queryProjectListAjax()
}
const operateClick = (row: any) => {
  router.push({
    path: '/processLook',
    query: {
      url:
        `${
          import.meta.env.VITE_PROCESS_DETAIL_URI
        }/IFlowInstance/redirectFlowPage?flowInstanceId=` + row.instanceId
    }
  })
}
const applyDateObj = ref()
const tableData = ref<Array<any>>([])
const total = ref<number>()
const searchHandle: () => void = () => {
  queryProjectListAjax()
}
const queryProjectListAjax = async () => {
  loading.value = true

  const urlApi = `/purchase/page`
  if (applyDateObj.value && applyDateObj.value.length > 0) {
    qrs.applyDateOn = moment(applyDateObj.value[0]).format('YYYY-MM-DD')
    qrs.applyDateOff = moment(applyDateObj.value[1]).format('YYYY-MM-DD')
  }
  const sendData = {
    ...qrs,
    pageSize: 10
  }
  const result = await request.get({ url: urlApi, params: sendData._rawValue }, '/business')
  tableData.value = result['records']
  total.value = result['total']
  loading.value = false
}

queryProjectListAjax()

/**
 * 获取行政区tree结构数据
 */
const areaTree = ref<Array<any>>()
const queryAreaTreeAjax = async (): Promise<void> => {
  const urlApi = `/system/area/tree`
  const result = await request.get({ url: urlApi })
  areaTree.value = result
}
queryAreaTreeAjax()
onMounted(() => {
  initTreeDeps()
  //tableHeight.value = tableRef.value.clientHeight
})
</script>
