<!--
 * @Description: 考核填报-待填报列表页面
 * @Author: 李亚光
 * @Date: 2023-09-14
 -->
<script lang="ts" setup name="FillingTreatList">
import { reactive, ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import FillingDialog from './fillingDialog.vue'
import { batchSubmitApi, exportFilling, getListPage } from '@/api/home/filling/filling'
import { getDictByCode } from '@/api/system/dict'
import { exportFile } from '@/utils/exportUtils'
const { proxy } = getCurrentInstance() as any
const $route = useRoute()
const listQuery = reactive({
  assessedDept: '', // 指标考核对象
  cycle: '', // 考核周期
  isSubmit: $route.path.includes('treat') ? 'false' : 'true',
  planName: '', // 考核方案名称
  resultNo: '', // 考核结果编号
  offset: 1,
  limit: 20,
  startTime: '',
  endTime: '',
})
const columns = ref([
  {
    text: '考核结果编号',
    value: 'resultNo',
    align: 'center',
  },
  {
    text: '考核方案名称',
    value: 'planName',
    align: 'center',
  },
  {
    text: '考核对象',
    value: 'assessedDeptName',
    align: 'center',
  },
  {
    text: '考核周期',
    value: 'cycleName',
    align: 'center',
  },
  {
    text: '考核时间',
    value: 'startTime',
    align: 'center',
  },
  {
    text: '考核指标项目',
    value: 'quotaProjectName',
    align: 'center',
  },
  {
    text: '考核指标类型',
    value: 'quotaTypeName',
    align: 'center',
  },
  {
    text: '一级考核指标',
    value: 'priIndicators',
    align: 'center',
  },
  {
    text: '考核指标得分',
    value: 'score',
    align: 'center',
  },
  {
    text: '考核说明',
    value: 'assDesc',
    align: 'center',
  },
])
const treatColums = ref([
  {
    text: '提交状态',
    value: 'submitStatusName',
    align: 'center',
  },
  {
    text: '填报截至时间',
    value: 'timeScope',
    align: 'center',
  },
])
const alreadyColums = ref([
  {
    text: '附件',
    value: 'fileName',
    align: 'center',
  },
  {
    text: '提交状态',
    value: 'submitStatusName',
    align: 'center',
  },
  {
    text: '填报时间',
    value: 'endTime',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getListPage(listQuery).then((response) => {
    list.value = response.data.rows.map((item: any) => ({ ...item, timeScope: `${item.startTime}-${item.endTime}` }))
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 开始结束时间
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 reset = () => {
  datetimerange.value = []
  listQuery.assessedDept = ''
  listQuery.planName = ''
  listQuery.resultNo = ''
  listQuery.startTime = ''
  listQuery.endTime = ''
  listQuery.cycle = ''
  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 objectList = ref<{ id: string; value: string; name: string }[]>()
// 考核周期下拉框
const cycleList = ref<{ id: string; value: string; name: string }[]>()
// 获取字典
const fetchSelectList = () => {
  getDictByCode('ass_object').then((res) => {
    objectList.value = res.data
  })
  getDictByCode('cycle_code').then((res) => {
    cycleList.value = res.data
  })
}
fetchSelectList()
// 填报
const fillingRef = ref()
const filling = (row: any) => {
  fillingRef.value.initDialog(row)
}
// 提交
const submit = (row: any) => {
  ElMessageBox.confirm(
    '确定提交考核结果吗？一经提交，不可修改！',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    batchSubmitApi({ ids: [row.id] }).then((res) => {
      ElMessage.success('操作成功')
      search()
    })
  })
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 批量提交
const batchSubmit = () => {
  let data = [] as string[]
  if (selectList.value.filter((item: any) => item.submitStatusName === '未提交').length) {
    data = selectList.value.filter((item: any) => item.submitStatusName === '未提交').map((item: any) => item.id)
  }
  else {
    ElMessage.warning('请选择未提交的填报')
    return
  }
  // 只能提交  未提交状态的数据
  ElMessageBox.confirm(
    '确定提交考核结果吗？一经提交，不可修改！',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    batchSubmitApi({ ids: data }).then((res) => {
      ElMessage.success('操作成功')
      search()
    })
  })
}
// 导出列表
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),
    }
    exportFilling(data).then((res) => {
      exportFile(res.data, '已填报考核结果')
      loading.close()
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('无可导出内容')
  }
}
</script>

<template>
  <app-container>
    <filling-dialog ref="fillingRef" @close-refresh="search" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.resultNo" 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.assessedDept" placeholder="指标考核对象" clearable />
        <!-- <el-select v-model.trim="listQuery.assessedDept" placeholder="指标考核对象" clearable>
          <el-option v-for="item in objectList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select> -->
      </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-area>
    <table-container>
      <template #btns-right>
        <el-button v-if="$route.path.includes('treat')" type="primary" @click="batchSubmit">
          提交
        </el-button>
        <el-button v-else type="primary" @click="exportList">
          导出
        </el-button>
      </template>
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      >
        <template v-if="$route.path.includes('treat')" #columns>
          <el-table-column v-for="item in treatColums" :key="item.text" :label="item.text" :prop="item.value" align="center" />
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="filling(scope.row)">
                填报
              </el-button>
              <el-button :disabled="scope.row.submitStatusName === '未填报'" link type="primary" size="small" @click="submit(scope.row)">
                提交
              </el-button>
            </template>
          </el-table-column>
        </template>
        <template v-else #columns>
          <el-table-column v-for="item in alreadyColums" :key="item.text" :label="item.text" :prop="item.value" align="center" />
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

