<template>
  <div class="h-full flex flex-col p-2">
    <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 shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" @submit.prevent="handleQuery" :inline="true">
            <el-form-item label="车位状态" prop="parkingStatus">
              <el-select v-model="queryParams.parkingStatus" placeholder="请选择" clearable>
                <el-option v-for="dict in com_parking_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="审核状态" prop="checkStatus">
              <el-select v-model="queryParams.checkStatus" placeholder="请选择" clearable>
                <el-option v-for="dict in com_review" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="车位编号" prop="parkingCode">
              <el-input v-model="queryParams.parkingCode" placeholder="请输入车位编号" clearable @keyup.enter="handleQuery" />
            </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" class="mb8">
          <!-- <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['carArea:add:parking']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['carArea:edit:parking']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['parking:parking:remove']"
              >删除</el-button
            >
          </el-col> -->
          <right-toolbar
            :disable-delete="multiple"
            :disable-edit="single"
            :show-add="checkPermi(['parking:parking:add'])"
            :show-edit="checkPermi(['parking:parking:edit'])"
            :show-delete="checkPermi(['parking:parking:remove'])"
            @update:add="handleAdd"
            @update:edit="handleUpdate()"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="parkingList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="区域" align="center" prop="areaName"> </el-table-column>
        <el-table-column label="车位编号" align="center" prop="parkingCode" />
        <el-table-column label="车位面积" align="center" prop="parkingArea">
          <template #default="scope"> {{ scope.row.parkingArea }}㎡ </template>
        </el-table-column>
        <el-table-column label="楼层" align="center" prop="parkingFloor" />
        <el-table-column label="车位状态" align="center" prop="parkingStatus">
          <template #default="scope">
            <dict-tag :options="com_parking_status" :value="scope.row.parkingStatus" />
          </template>
        </el-table-column>
        <el-table-column label="车位类型" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="com_parking_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="持有人" align="center" prop="residentName" />
        <el-table-column label="审核状态" align="center" prop="checkStatus">
          <template #default="scope">
            <dict-tag :options="com_review" :value="scope.row.checkStatus" />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <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="['parking:parking:query']">审核</el-button>
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['parking:parking:edit']">编辑</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['parking:parking: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="Parking" lang="ts">
import Pageheader from '@/components/Pageheader/index.vue';
import { listParking, getParking, delParking, addParking, updateParking } from '@/api/system/parking/index';
import { ParkingVO, ParkingQuery, ParkingForm } from '@/api/system/parking/type';
import { checkPermi } from '@/utils/permission';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_review } = toRefs<any>(proxy?.useDict('com_review'));
const { com_parking_type } = toRefs<any>(proxy?.useDict('com_parking_type'));
const { com_parking_status } = toRefs<any>(proxy?.useDict('com_parking_status'));

const router = useRouter();
const parkingList = ref<ParkingVO[]>([]);
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 initFormData: ParkingForm = {
  id: undefined,
  areaId: undefined,
  parkingCode: undefined,
  parkingArea: undefined,
  parkingStatus: undefined,
  type: undefined,
  residentId: undefined,
  addTime: undefined,
  addBy: undefined,
  checkStatus: undefined,
  remark: undefined,
  createBy: undefined,
  createTime: undefined,
  updateBy: undefined,
  updateTime: undefined
};
const data = reactive<PageData<ParkingForm, ParkingQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    areaId: undefined,
    parkingCode: undefined,
    parkingArea: undefined,
    parkingStatus: '',
    type: undefined,
    residentId: undefined,
    addTime: undefined,
    addBy: undefined,
    checkStatus: undefined
  }
});

const { queryParams, form, rules } = toRefs(data);
const AreaList = ref([]);
/** 查询车位管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listParking(queryParams.value);
  parkingList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

function getAreaInfoById(id: string) {
  const obj = AreaList.value.find((item) => item.id === id);
  return `${obj.areaName}${obj.areaCode}`;
}

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

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

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

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

/** 修改按钮操作 */
const handleUpdate = async (row?: ParkingVO) => {
  const _id = row.id || ids.value[0];
  router.push({
    path: '/carArea/editParking',
    query: {
      id: _id
    }
  });
};
/** 审核按钮操作 */
const handleMain = async (row?: ParkingVO) => {
  router.push({
    path: '/carArea/parkMain',
    query: {
      id: row.id
    }
  });
};

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

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

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