<template>
  <el-dialog v-model="dialog.visible" :title="dialog.title" width="50%" :draggable="draggable">
    <el-card>
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px" class="-mb-15px">
        <el-form-item label="设备机号" prop="termNo">
          <el-input v-model="queryParams.termNo" placeholder="请输入设备机号" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="设备名称" prop="termName">
          <el-input v-model="queryParams.termName" placeholder="请输入设备名称" clearable style="width: 200px" @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-card>
    <el-card class="mt-15px">
      <el-table v-loading="loading" :data="xfTermList" max-height="400px" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="设备机号" align="center" prop="termNo" />
        <el-table-column label="设备名称" align="center" prop="termName" show-overflow-tooltip />
        <el-table-column label="所属餐厅" align="center" prop="roomName" show-overflow-tooltip />
        <el-table-column label="结算账户" align="center" prop="accountName" show-overflow-tooltip />
        <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      </el-table>
      <!-- 翻页组件 -->
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <template #footer>
      <div class="dialog-footer">
        <el-button :loading="formLoading" type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="dialog.visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { listXfTerm } from '@/api/consumption/xfTerm';
import { XfTermVO } from '@/api/consumption/xfTerm/types';

// 对话框
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 窗体是否可以拖动
const draggable = ref(true);
// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  termNo: undefined,
  termName: '',
  termIp: '',
  params: {}
});
// 查询窗体Ref
const queryFormRef = ref<ElFormInstance>();
// 是否加载遮罩层
const loading = ref(true);
const xfTermList = ref<XfTermVO[]>([]);
// 总记录数
const total = ref(0);
// 列表选中的记录Id
const ids = ref<Array<number | string>>([]);
const formLoading = ref(false);
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

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

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

/** 查询消费设备列表 */
const getList = async () => {
  loading.value = true;
  const res = await listXfTerm(queryParams);
  xfTermList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 多选框选中数据 */
const handleSelectionChange = (selection: XfTermVO[]) => {
  ids.value = selection.map((item) => item.termId);
};
const open = async () => {
  dialog.visible = true;
  dialog.title = '添加设备';
  ids.value = [];
  resetQuery();
};
// 传回给父组件的属性与方法
// 提供 open 方法，用于打开弹窗
defineExpose({ open });
/** 触发父组件的事件 */
/** 定义 success 事件，用于操作成功后的回调 */
const emit = defineEmits(['success']);
/** 提交按钮 */
const submitForm = async () => {
  // 校验表单
  if (ids.value.length == 0) {
    proxy?.$modal.msgSuccess('请选择数据');
    return;
  }
  // 提交请求
  formLoading.value = true;
  try {
    dialog.visible = false;
    // 发送操作成功的事件
    emit('success', ids.value);
  } finally {
    formLoading.value = false;
  }
};
</script>
