<template>
  <div class="p-2 h-full 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 shadow="hover">
          <el-form ref="queryFormRef" :inline="true" :model="queryParams" @submit.prevent="handleQuery">
            <el-form-item label="缴费人" prop="payerName">
              <el-input v-model="queryParams.payerName" clearable maxlength="12" placeholder="请输入缴费人" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="车牌号" prop="carNum">
              <el-input v-model="queryParams.carNum" clearable maxlength="8" placeholder="请输入车牌号" @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">
          <right-toolbar
            :disable-delete="multiple"
            :disable-edit="single"
            :show-add="checkPermi(['parkingCost:parkingCost:add'])"
            :show-delete="checkPermi(['parkingCost:parkingCost:remove'])"
            :show-edit="checkPermi(['parkingCost:parkingCost:edit'])"
            @update:add="handleAdd"
            @update:edit="handleUpdate()"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="parkingCostList" border @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="车位" prop="areaId">
          <template #default="scope"> {{ scope.row.name }} </template>
        </el-table-column>
        <el-table-column align="center" label="收费项目" prop="chargeItem">
          <template #default="scope">
            <dict-tag :options="com_parking_charge" :value="scope.row.chargeItem" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="缴纳人" prop="payerName" />
        <el-table-column align="center" label="车牌号" prop="carNum" />
        <el-table-column align="center" label="停车时长" width="100">
          <template #default="scope">
            <span>{{ formatTimeDiff(scope.row.startTime, scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="费用" prop="cost">
          <template #default="scope"> {{ Number(scope.row.cost).toFixed(2) }}元 </template>
        </el-table-column>
        <el-table-column align="center" label="缴纳状态" prop="payerStatus">
          <template #default="scope">
            <dict-tag :options="com_parking_payer_status" :value="scope.row.payerStatus" />
          </template>
        </el-table-column>
        <!--        <el-table-column label="支付方式" align="center" prop="payMethod">-->
        <!--          <template #default="scope">-->
        <!--            <dict-tag v-if="scope.row.payMethod" :options="com_pay_method" :value="scope.row.payMethod" />-->
        <!--            <span v-else>&#45;&#45;</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column align="center" label="备注" prop="remark" />
        <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作">
          <template #default="scope">
            <el-button v-has-permi="['parkingCost:parkingCost:edit']" link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button v-has-permi="['parkingCost:parkingCost:remove']" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <Pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script lang="ts" name="ParkingCost" setup>
import Pagination from '@/components/Pagination/index.vue';
import pageHeader from '@/components/Pageheader/index.vue';
import RightToolbar from '@/components/RightToolbar/index.vue';
import { listParkingCost, delParkingCost } from '@/api/system/ParkingCost';
import { ParkingCostVO, ParkingCostQuery, ParkingCostForm } from '@/api/system/ParkingCost/type';
import { checkPermi } from '@/utils/permission';
import { formatTimeDiff } from '@/utils/time';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_parking_charge } = toRefs<any>(proxy?.useDict('com_parking_charge'));
const { com_parking_payer_status } = toRefs<any>(proxy?.useDict('com_parking_payer_status'));
const { com_pay_method } = toRefs<any>(proxy?.useDict('com_pay_method'));

const router = useRouter();
interface ParkingCostType extends ParkingCostVO {
  areaName?: string;
  parkingName?: string;
}
const parkingCostList = ref<ParkingCostType[]>([]);
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: ParkingCostForm = {
  id: undefined,
  areaId: undefined,
  parkingId: undefined,
  chargeItem: undefined,
  payerName: undefined,
  carNum: undefined,
  startTime: undefined,
  endTime: undefined,
  cost: undefined,
  payerStatus: undefined,
  payMethod: undefined,
  remark: undefined,
  villageId: undefined
};
const data = reactive<PageData<ParkingCostForm, ParkingCostQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    areaId: undefined,
    parkingId: undefined,
    chargeItem: undefined,
    payerName: undefined,
    carNum: undefined,
    startTime: undefined,
    endTime: undefined,
    cost: undefined,
    payerStatus: undefined,
    payMethod: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '停车缴费表id不能为空', trigger: 'blur' }]
  }
});

const { queryParams } = toRefs(data);
/** 查询停车缴费管理列表 */
const getList = async () => {
  loading.value = true;
  // 停车缴费
  listParkingCost(queryParams.value)
    .then((res) => {
      parkingCostList.value = res.rows;
      total.value = res.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

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

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

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

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

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

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

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

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