<template>
  <div class="h-full flex flex-col p-2">
    <Pageheader></Pageheader>
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" @submit.prevent="handleQuery" :inline="true">
            <el-form-item label="选择日期" prop="itemName">
              <el-date-picker
                v-model="starDate"
                type="daterange"
                unlink-panels
                range-separator="到"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="YYYY/MM/DD"
                value-format="YYYY/MM/DD"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="5">
            <span style="font-size: 1.2rem">月报列表</span>
          </el-col>
          <el-col :span="14"> </el-col>
          <el-col :span="5" style="text-align: right">
            <el-button type="primary" @click="exportCurrentPage">导出当前页</el-button>
            <el-button type="primary" @click="exportAll">导出所有</el-button>
            <el-button type="primary" @click="exportChecked">导出选中</el-button>
          </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="itemList" @selectionChange="handleChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="日期" width="100" align="center" prop="statDate" />
        <el-table-column label="计划巡检任务" width="120" align="center" prop="totalPlans" />
        <el-table-column label="按时完成" align="center" prop="completedTasks" />
        <el-table-column label="超时完成" align="center" prop="outCompletedTasks" />
        <el-table-column label="超时未完成" align="center" prop="outUncompletedTasks" />
        <el-table-column label="进行中" align="center" prop="activeTask" />
        <el-table-column label="未开始" align="center" prop="pendingTask" />
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Item" lang="ts">
import { listItem } from '@/api/system/Inspection';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const itemList = ref<any[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const starDate = ref([]);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    statDate: '',
    queryDateEnd: '',
    queryDateBegin: '',
    dataType: '3' // 1 日报 3 月报
  }
});

const { queryParams } = toRefs(data);

/** 查询巡检项目列表 */
const getList = async () => {
  queryParams.value.statDate = starDate.value.join('-');
  queryParams.value.queryDateBegin = starDate.value[0];
  queryParams.value.queryDateEnd = starDate.value[1];
  loading.value = true;
  listItem(queryParams.value)
    .then((res) => {
      itemList.value = res.rows;
      total.value = res.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  starDate.value = [];
  handleQuery();
};
/** 导出当前页数据 */
const exportCurrentPage = () => {
  proxy?.download(
    '/inspection/data/export/page',
    {
      ...queryParams.value
    },
    `巡检月报_${new Date().toLocaleString()}.xlsx`
  );
};
const exportAll = () => {
  proxy?.download(
    '/inspection/data/export',
    {
      ...queryParams.value
    },
    `巡检月报_${new Date().toLocaleString()}.xlsx`
  );
};
const multipleSelection = ref([]);
function handleChange(val) {
  multipleSelection.value = val;
}
const exportChecked = () => {
  if (multipleSelection.value.length > 0) {
    const listValue = multipleSelection.value.map((item) => item.id);
    console.log(listValue);
    proxy?.download(
      '/inspection/data/export/selected',
      {
        list: listValue.join(','),
        dataType: '3'
      },
      `巡检月报_选中${listValue.length}条数据_${new Date().toLocaleString()}.xlsx`
    );
  } else {
    ElMessage.warning('请选择数据');
  }
};
onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.el-table {
  height: calc(100% - 80px);
}
</style>
