<template>
  <div class="h-full p-2 flex flex-col">
    <Pageheader title="房屋租赁"></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>
          <div class="h-full flex flex-align-center">
            <el-form label-width="120px" ref="queryFormRef" :model="queryParams" :inline="true">
              <el-form-item label="房屋用途" prop="houseUse">
                <el-select v-model="queryParams.houseUse" placeholder="选择房屋用途" style="width: 240px">
                  <el-option v-for="item in com_house_use" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="发布人" prop="houseId">
                <el-input v-model="queryParams.rentalName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['houseRental:houseRental:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['houseRental:houseRental:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['houseRental:houseRental:remove']"
              >删除</el-button
            >
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table class="h-full" v-loading="loading" border :data="houseRentalList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="房屋信息" align="center" prop="name" />
        <el-table-column label="租赁状态" align="center" prop="rentalStatus">
          <template #default="{ row }">
            <DictTag :options="com_rental_status" :value="row.rentalStatus"></DictTag>
          </template>
        </el-table-column>
        <el-table-column label="发布人" align="center" prop="rentalName" />
        <el-table-column label="联系方式" align="center" prop="phone" />
        <el-table-column label="发布时间" align="center" prop="pubTime" />
        <el-table-column label="房屋用途" align="center" prop="houseUse">
          <template #default="{ row }">
            <DictTag :options="com_house_use" :value="row.houseUse"></DictTag>
          </template>
        </el-table-column>
        <el-table-column label="租赁方式" align="center" prop="rentalType">
          <template #default="{ row }">
            <el-tag v-if="row.rentalType === 0">整租</el-tag>
            <el-tag type="success" v-else>合租</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleMain(scope.row)" v-hasPermi="['houseRental:houseRental:edit']">详情</el-button>
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['houseRental:houseRental:edit']">修改</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['houseRental:houseRental:remove']">删除</el-button>
          </template>
        </el-table-column>
      </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="HouseRental" lang="ts">
import Pageheader from '@/components/Pageheader/index.vue';
import { listHouseRental, delHouseRental } from '@/api/system/houseRental/index';
import { HouseRentalVO, HouseRentalQuery, HouseRentalForm } from '@/api/system/houseRental/type';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_house_use } = toRefs<any>(proxy?.useDict('com_house_use'));
const { com_rental_status } = toRefs<any>(proxy?.useDict('com_rental_status'));
const { com_rental_method } = toRefs<any>(proxy?.useDict('com_rental_method'));

const router = useRouter();
const houseRentalList = ref<HouseRentalVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const houseRentalFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: HouseRentalForm = {
  id: undefined,
  rentalName: undefined,
  rent: undefined,
  rentalType: undefined,
  houseFace: undefined,
  houseArea: undefined,
  floorNo: undefined,
  inTime: undefined,
  facilities: undefined,
  houseImageUrl: undefined,
  supInfo: undefined,
  phone: undefined,
  vx: undefined,
  email: undefined,
  houseId: undefined
};
const data = reactive<PageData<HouseRentalForm, HouseRentalQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,

    houseUse: undefined,
    rentalName: undefined,
    rent: undefined,
    rentalType: undefined,
    houseFace: undefined,
    houseArea: undefined,
    floorNo: undefined,
    inTime: undefined,
    facilities: undefined,
    houseImageUrl: undefined,
    supInfo: undefined,
    phone: undefined,
    vx: undefined,
    email: undefined,
    houseId: undefined,
    params: {}
  }
});

const { queryParams, form } = toRefs(data);

/** 查询房屋租赁管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listHouseRental(queryParams.value);
  houseRentalList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  houseRentalFormRef.value?.resetFields();
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: HouseRentalVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  router.push({
    path: '/house/addhouseRental'
  });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: HouseRentalVO) => {
  router.push({
    path: '/house/addhouseRental',
    query: {
      id: row.id
    }
  });
};
/** 详情按钮操作 */
const handleMain = async (row?: HouseRentalVO) => {
  router.push({
    path: '/house/houseMain',
    query: {
      id: row.id
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: HouseRentalVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('该信息删除后无法恢复，确定要删除吗？').finally(() => (loading.value = false));
  await delHouseRental(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

onMounted(() => {
  getList();
});
</script>

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