<script lang="ts" setup name="ResultDeptList">
import { reactive, ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import DeptDialog from './components/edit.vue'
import { exportDept, getListPage } from '@/api/home/result/dept'
import { getDictByCode } from '@/api/system/dict'
import { exportFile } from '@/utils/exportUtils'

const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  assNo: '', // 考核结果编号
  assStatus: '', //	考核状态
  assessedDeptName: '', //	考核部门名称
  cycle: '', // 考核周期
  endTime: '', // 结束时间
  planName: '', // 方案名称
  recoRating: '', // 	考核等级
  reportStatus: '', // 	填报状态
  sorce: '', // 考核得分
  startTime: '', // 开始时间
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '考核结果编号',
    value: 'assNo',
    align: 'center',
  },
  {
    text: '考核方案名称',
    value: 'planName',
    align: 'center',
  },
  {
    text: '考核对象',
    value: 'assObjectName',
    align: 'center',
  },
  {
    text: '具体考核部门',
    value: 'assessedDeptName',
    align: 'center',
  },
  {
    text: '考核周期',
    value: 'cycleName',
    align: 'center',
  },
  {
    text: '考核时间',
    value: 'timeFmt',
    align: 'center',
  },
  {
    text: '考核等级评定',
    value: 'recoRatingName',
    align: 'center',
  },
  {
    text: '考核得分',
    value: 'score',
    align: 'center',
  },
  {
    text: '填报状态',
    value: 'reportStatusName',
    align: 'center',
  },
  {
    text: '填报完成时间',
    value: 'reportTime',
    align: 'center',
  },
  {
    text: '考核状态',
    value: 'assStatusName',
    align: 'center',
  },
  {
    text: '考核完成时间',
    value: 'submitTime',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  if (newVal.length) {
    listQuery.startTime = `${newVal[0]} 00:00:00`
    listQuery.endTime = `${newVal[1]} 23:59:59`
  }
  else {
    listQuery.startTime = ''
    listQuery.endTime = ''
  }
})

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.assNo = '' // 考核结果编号
  listQuery.assStatus = '' //	考核状态
  listQuery.assessedDeptName = '' //	考核部门名称
  listQuery.cycle = '' // 考核周期
  listQuery.endTime = '' // 结束时间
  listQuery.planName = '' // 方案名称
  listQuery.recoRating = '' // 	考核等级
  listQuery.reportStatus = '' // 	填报状态
  listQuery.sorce = '' // 考核得分
  listQuery.startTime = '' // 开始时间
  listQuery.offset = 1
  listQuery.limit = 20
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
const $router = useRouter()
// 新建编辑操作
const handler = (row: any, type: string) => {
  $router.push({
    path: `/deptlist/${type}`,
    query: {
      // row: JSON.stringify(row),
      id: row.id,
    },
  })
}

const assStatusList = ref<{ id: string; value: string; name: string }[]>()
const cycleList = ref<{ id: string; value: string; name: string }[]>()
const recoRatingList = ref<{ id: string; value: string; name: string }[]>()
const reportStatusList = ref<{ id: string; value: string; name: string }[]>()
// 获取字典
const fetchSelectList = () => {
  getDictByCode('ass_status').then((res) => {
    assStatusList.value = res.data
  })
  getDictByCode('cycle_code').then((res) => {
    cycleList.value = res.data
  })
  getDictByCode('level_code').then((res) => {
    recoRatingList.value = res.data
  })
  getDictByCode('report_status').then((res) => {
    reportStatusList.value = res.data
  })
}
fetchSelectList()
// 导出列表
const exportList = () => {
  if (list.value.length) {
    const loading = ElLoading.service({
      lock: true,
      text: 'Loading',
      background: 'rgba(255, 255, 255, 0.8)',
    })
    const data = {
      ...listQuery,
      offset: undefined,
      limit: undefined,
      // ids: selectList.value.map(item => item.id),
    }
    exportDept(data).then((res) => {
      exportFile(res.data, '部门考核结果')
      loading.close()
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('无可导出内容')
  }
}
const deptDialogRef = ref()
const edit = (row: any) => {
  deptDialogRef.value.initDialog(row)
}
</script>

<template>
  <app-container>
    <dept-dialog ref="deptDialogRef" @close-refresh="search" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.assNo" placeholder="考核结果编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.planName" placeholder="考核方案名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.assessedDeptName" placeholder="具体考核部门" clearable />
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.cycle" placeholder="考核周期" clearable>
          <el-option v-for="item in cycleList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="考核开始时间" end-placeholder="考核结束时间"
        />
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.recoRating" placeholder="考核等级" clearable>
          <el-option v-for="item in recoRatingList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.sorce" placeholder="考核得分" clearable />
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.assStatus" placeholder="考核状态" clearable>
          <el-option v-for="item in assStatusList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.reportStatus" placeholder="填报状态" clearable>
          <el-option v-for="item in reportStatusList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <el-button type="primary" @click="exportList">
          导出
        </el-button>
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="false" :is-multi="true" @change="changePage"
      >
        <template #columns>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handler(scope.row, 'detail')">
                详情
              </el-button>
              <el-button link type="primary" size="small" @click="edit(scope.row)">
                编辑
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

