<script setup lang="ts">

import { onMounted } from 'vue'
import { clientDownloadExcel, clientGet, clientPost } from '@/utils/request.ts'
import { ElLoading, ElMessage } from 'element-plus'

interface Property {
  id: string; // 主键
  receiptNumber?: string; // 收据号
  building?: string; // 楼栋
  roomNumber?: string; // 房号
  collectionTime?: Date; // 收取时间
  rent?: number; // 房租
  deposit?: number; // 押金
  waterFee?: number; // 水费
  electricityFee?: number; // 电费
  propertyManagementFee?: number; // 物业管理费
  rentPeriod?: string; // 租金收取时段
  handler?: string; // 经手人
  remarks?: string; // 备注
  createTime?: Date; // 创建时间
  createBy?: string; // 创建人
  updateTime?: Date; // 修改时间
  updateBy?: string; // 修改人
}

interface PropertyOneResponse extends BaseResponse{
  data: Property;
}

interface PropertyListResponse extends BaseResponse{
  data: PageType<Property>;
}

interface AddProperty {
  receiptNumber?: string; // 收据号
  building?: string; // 楼栋
  roomNumber?: string; // 房号
  collectionTime?: Date; // 收取时间
  rent?: number; // 房租
  deposit?: number; // 押金
  waterFee?: number; // 水费
  electricityFee?: number; // 电费
  propertyManagementFee?: number; // 物业管理费
  rentPeriod?: string; // 租金收取时段
  handler?: string; // 经手人
  remarks?: string; // 备注
}

interface UpdateProperty {
  id?: string;
  receiptNumber?: string; // 收据号
  building?: string; // 楼栋
  roomNumber?: string; // 房号
  collectionTime?: Date; // 收取时间
  rent?: number; // 房租
  deposit?: number; // 押金
  waterFee?: number; // 水费
  electricityFee?: number; // 电费
  propertyManagementFee?: number; // 物业管理费
  rentPeriod?: string; // 租金收取时段
  handler?: string; // 经手人
  remarks?: string; // 备注
}




//新增
const add = async (data:AddProperty) =>{
  const res = await clientPost<AddProperty,BaseResponse>('/apublicRentalHousing/save',{
    ...data
  })

  if(res.code !== 200){
    ElMessage.error(res.msg);
    return;
  }

  ElMessage.success(res.msg);
}

//根据id获取数据
const getById = async (id:string) =>{
  const res = await clientGet<null,PropertyOneResponse>('/apublicRentalHousing/getById/'+id);
  if(res.code !== 200){
    ElMessage.error(res.msg);
    return;
  }
  console.log(res.data);
}

//分页获取数据
const getList = async () =>{
  const res = await clientGet<{
    pageNum: number;
    pageSize: number;
    receiptNumber?: string;
    roomNumber?: string;
    building?: string;
  },PropertyListResponse>('/apublicRentalHousing/findByPage',{
    params:{
      pageNum: 1,
      pageSize: 10,
      building:'',//这是模糊搜索的值(如果没有值就不要传个空的过去)
      roomNumber:'',//这是模糊搜索的值(如果没有值就不要传个空的过去)
      receiptNumber:''//这是模糊搜索的值(如果没有值就不要传个空的过去)
    }
  });

  if(res.code !== 200){
    ElMessage.error(res.msg);
    return;
  }
  console.log(res.data);
}

//批量删除
const delBatch = async (ids: string[]) => {
  const res = await clientPost<string, BaseResponse>('/apublicRentalHousing/deleteBatch', JSON.stringify(ids));
  if (res.code !== 200) {
    ElMessage.error(res.msg)
    return
  }
  ElMessage.success(res.msg)
}

//修改
const update = async (updateId:string,data:UpdateProperty) =>{
  const res = await clientPost<UpdateProperty,BaseResponse>('/apublicRentalHousing/update',{
    ...data,
    id:updateId
  })
  if(res.code !== 200){
    ElMessage.error(res.msg);
    return;
  }
  ElMessage.success(res.msg);
}

//导出为excel
const exportExcel = async () => {
  const loading = ElLoading.service({
    lock: true,
    text: '导出中，请稍候...',
    background: 'rgba(0, 0, 0, 0.1)',
    fullscreen: true,
  })

  try {
    await clientDownloadExcel('/apublicRentalHousing/exportData', {
      params: {
        roomNumber: '',//这是模糊搜索的值(如果没有值就不要传个空的过去)
        building: '',//这是模糊搜索的值(如果没有值就不要传个空的过去)
        receiptNumber: '',//这是模糊搜索的值(如果没有值就不要传个空的过去)
      },
    })
  } catch (error) {
    console.error('导出失败:', error)
    ElMessage.error('导出失败')
  } finally {
    loading.close()
  }
}

//excel导入
const importExcel = async (file: File) => {
  const res = await clientPost<{file: File},BaseResponse>('/apublicRentalHousing/importData',{
    file
  })
  if(res.code !== 200){
    ElMessage.error(res.msg);
    return;
  }
  ElMessage.success(res.msg);
}





const init = () => {
}

onMounted(()=>{
  init();
})

</script>

<template>
  <div>公租房收入情况信息</div>
</template>
