<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="houseId">
              <el-input v-model="queryParams.houseId" 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="['payRecordList:payRecordList:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete()"
              v-hasPermi="['payRecordList:payRecordList:remove']"
              >删除</el-button
            >
          </el-col> -->
          <right-toolbar
            :disable-delete="multiple"
            :show-edit="false"
            :show-add="checkPermi(['payRecordList:payRecordList:add'])"
            :show-delete="checkPermi(['payRecordList:payRecordList:remove'])"
            @update:add="handleAdd"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="payRecordList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="收费设备类型" align="center" prop="deviceType">
          <template #default="scope">
            <dict-tag :options="com_waterelectricity_topup_type" :value="scope.row.deviceType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="收费项目" align="center" prop="payType">
          <template #default="scope">
            <dict-tag :options="com_payrecord_pay_type" :value="scope.row.payType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="抄表周期" align="center" prop="recordMonth" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.recordMonth, '{y}-{m}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开始日期" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束日期" align="center" prop="endTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="recordMonth" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.recordMonth, '{y}-{m}-{d}') }}</span>
          </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="['sdb:main:payrecord']">明细</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['payRecord:payRecord: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>
    <!-- 添加或修改抄表周期明细对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
      <el-form ref="payRecordListFormRef" :model="form" label-position="left">
        <el-row>
          <el-col :span="12">
            <el-form-item label="抄表小区:" prop="recordId">
              {{ form.villageName ?? '--' }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房屋号码:" prop="houseName"> {{ form.houseName ?? '--' }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="住户姓名:" prop="recordId">
              {{ form.residentName ?? '--' }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住户编号:" prop="residentCode"> {{ form.residentCode ?? '--' }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="住户性别:" prop="recordId">
              {{ form.gender === '1' ? '男' : '女' }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码:" prop="residentCode"> {{ form.phone ?? '--' }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收费项目:" prop="recordId">
              <dict-tag v-if="form.payType" :options="com_payrecord_pay_type" :value="form.payType"></dict-tag>
              <span v-else>--</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抄表月份:" prop="residentCode"> {{ form.recordMonth ?? '--' }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="收费起止日期:" prop="recordId">
              {{ parseTime(form.startTime, '{y}-{m}-{d}') }} -- {{ parseTime(form.endTime, '{y}-{m}-{d}') }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="起始读数:" prop="startRead"> {{ form.startRead ?? '--' }} 度 </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="终止读数:" prop="endRead"> {{ form.endRead ?? '--' }} 度</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="本期用度:" prop="startRead"> {{ form.currentCost ?? '--' }} 度 </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单价:" prop="price"> {{ form.price ?? '--' }} 元/度</el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="费用合计:" prop="totalCost"> {{ form.totalCost ?? '--' }} 元 </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PayRecord" lang="ts">
import { listPayRecord, getPayRecord, delPayRecord, addPayRecord, updatePayRecord } from '@/api/system/SdbPayRecord';
import { PayRecordVO, PayRecordQuery, PayRecordForm } from '@/api/system/SdbPayRecord/type';
import { PayRecordListForm } from '@/api/system/SdbPayRecordMain/type';
import { checkPermi } from '@/utils/permission';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_waterelectricity_topup_type } = toRefs<any>(proxy?.useDict('com_waterelectricity_topup_type'));
const { com_payrecord_pay_type } = toRefs<any>(proxy?.useDict('com_payrecord_pay_type'));

const payRecordList = ref<PayRecordVO[]>([]);
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 payRecordFormRef = ref<ElFormInstance>();

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

const initFormData: PayRecordListForm = {
  id: undefined,
  recordId: undefined,
  housenumname: undefined,
  payType: undefined,
  recordMonth: undefined,
  startTime: undefined,
  endTime: undefined,
  currentCost: undefined,
  totalCost: undefined,
  deviceCode: undefined,
  deviceType: undefined,
  villageName: undefined,
  houseName: undefined,
  residentName: undefined,
  gender: undefined,
  phone: undefined,
  startRead: undefined,
  endRead: undefined,
  price: undefined,
  residentCode: undefined
};
const data = reactive<PageData<PayRecordListForm, PayRecordQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceType: undefined,
    houseId: undefined,
    payType: undefined,
    recordMonth: undefined,
    startTime: undefined,
    endTime: undefined,
    params: {}
  }
});

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

/** 查询 抄表周期管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPayRecord(queryParams.value);
  payRecordList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

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

/** 详情按钮操作 */
const handleMain = async (row?: PayRecordVO) => {
  router.push({
    path: '/sdb/PayRecordMain',
    query: {
      id: row?.id
    }
  });
};

/** 修改按钮操作 */
const handleUpdate = async (id: string) => {
  // const res = await getPayRecordList(id);
  // Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '最新抄表详细';
};
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

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

/** 删除按钮操作 */
const handleDelete = async (row?: PayRecordVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除该抄表周期？').finally(() => (loading.value = false));
  await delPayRecord(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

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