<template>
  <div class="p-2 h-full flex flex-col">
    <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">
          <div class="flex flex-items-center justify-between">
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">
              <el-form-item label="精准查询">
                <el-select v-model="queryParamsType" style="width: 160px; margin-right: 20px">
                  <el-option v-for="item in deviceCodeList" :key="item.value" :label="item.name" :value="item.value"></el-option>
                </el-select>
                <el-input v-model="queryParamsValue" 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>
            <el-button @click="confimu">返回</el-button>
          </div>
        </el-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="payRecordListList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="房间号" align="center" prop="houseName" />
        <el-table-column label="仪表名称" align="center" prop="payType" />
        <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="currentCost" />
        <el-table-column label="费用合计(元)" align="center" prop="totalCost" />
        <el-table-column label="设备编号" align="center" prop="deviceCode" />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)">详情</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 :options="com_payrecord_pay_type" :value="form.payType"></dict-tag>
            </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="PayRecordList" lang="ts">
import { listPayRecordList, getPayRecordList, delPayRecordList, addPayRecordList, updatePayRecordList } from '@/api/system/SdbPayRecordMain/index';
import { PayRecordListVO, PayRecordListQuery, PayRecordListForm } from '@/api/system/SdbPayRecordMain/type';

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

const payRecordListList = ref<PayRecordListVO[]>([]);
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 router = useRouter();

const queryFormRef = ref<ElFormInstance>();
const payRecordListFormRef = 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, PayRecordListQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    residentName: undefined,
    houseName: undefined,
    paycordName: undefined
  }
});
const queryParamsType = ref('houseName');
const queryParamsValue = ref('');
const deviceCodeList = [
  {
    name: '房屋号',
    value: 'houseName'
  },
  {
    name: '住户姓名',
    value: 'residentName'
  },
  {
    name: '仪表名称',
    value: 'paycordName'
  }
];
const { queryParams, form } = toRefs(data);

/** 查询抄表周期明细列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPayRecordList(queryParams.value);
  payRecordListList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    residentName: undefined,
    houseName: undefined,
    paycordName: undefined
  };
  queryParamsType.value = 'houseName';
  queryParamsValue.value = '';
  handleQuery();
};

/** 修改按钮操作 */
const handleUpdate = async (row?: PayRecordListVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getPayRecordList(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '抄表详细';
};

/** 提交按钮 */
const submitForm = () => {
  payRecordListFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updatePayRecordList(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addPayRecordList(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};
const confimu = () => {
  router.back();
};
onMounted(() => {
  getList();
});
</script>

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