<template>
  <MyLayout>
    <el-card class="my-query-box mt8" shadow="never">
      <el-form :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="关键字" prop="keyWord">
          <el-input v-model="queryParams.keyWord" placeholder="请输入关键字" clearable />
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="queryParams.stDate"
            type="datetime"
            placeholder="选择开始时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="queryParams.edDate"
            type="datetime"
            placeholder="选择结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Search" @click="handleQuery"> 查询 </el-button>
          <el-button v-auth="'api:admin:usp-relay:add'" type="primary" icon="ele-Plus" @click="handleAdd"> 新增 </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="my-fill mt8" shadow="never">
      <div class="my-tools-box mb8 my-flex my-flex-between">
        <div>
        </div>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        border
      >
        <el-table-column prop="deviceNo" label="设备编号" min-width="100" show-overflow-tooltip />
        <el-table-column prop="assetNo" label="资产编号" min-width="100" show-overflow-tooltip />
        <el-table-column prop="ip" label="IP地址" min-width="120" show-overflow-tooltip />
        <el-table-column prop="port" label="端口号" min-width="80" show-overflow-tooltip />
        <el-table-column prop="serviceName" label="服务名称" min-width="120" show-overflow-tooltip />
        <el-table-column label="状态" width="80" align="center" show-overflow-tooltip>
          <template #default="{ row }">
            <el-tag type="success" v-if="row.enabled">启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right" header-align="center" align="center">
          <template #default="{ row }">
            <el-button
              v-if="auth('api:admin:usp-relay:update')"
              icon="ele-EditPen"
              size="small"
              text
              type="primary"
              @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button
              v-if="auth('api:admin:usp-relay:soft-delete')"
              icon="ele-Delete"
              size="small"
              text
              type="danger"
              @click="handleDelete(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="my-flex my-flex-end" style="margin-top: 10px">
        <el-pagination
          v-model:currentPage="pageInput.currentPage"
          v-model:page-size="pageInput.pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          size="small"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
    </el-card>

    <relay-form
      v-model="showDialog"
      :title="formTitle"
      ref="formRef"
      @refresh="getList"
    />
  </MyLayout>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { UspRelayApi } from '/@/api/admin/UspRelayApi'
import type { RelayGetPageOutput, PageInputRelayGetPageInput } from '/@/api/admin/data-contracts'
import { auth } from '/@/utils/authFunction'
import RelayForm from './components/relay-form.vue'

// 查询参数
const queryParams = reactive({
  keyWord: '',
  stDate: '',
  edDate: '',
})

// 分页参数
const pageInput = reactive<PageInputRelayGetPageInput>({
  currentPage: 1,
  pageSize: 20,
  filter: {
    keyWord: '',
    stDate: '',
    edDate: '',
  },
})

// 表格数据
const tableData = ref<RelayGetPageOutput[]>([])
const total = ref(0)
const loading = ref(false)

// 表单相关
const formRef = ref()
const formTitle = ref('')
const showDialog = ref(false)

// 查询表单引用
const queryFormRef = ref()

// 获取列表数据
const getList = async () => {
  loading.value = true
  try {
    const res = await new UspRelayApi().getPage(pageInput)
    if (res.success) {
      tableData.value = res.data?.list || []
      total.value = res.data?.total || 0
    }
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

// 查询按钮点击
const handleQuery = () => {
  pageInput.currentPage = 1
  pageInput.filter = {
    keyWord: queryParams.keyWord,
    stDate: queryParams.stDate,
    edDate: queryParams.edDate,
  }
  getList()
}

// 重置查询
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  queryParams.stDate = ''
  queryParams.edDate = ''
  handleQuery()
}

// 新增按钮点击
const handleAdd = () => {
  formTitle.value = '新增继电器'
  formRef.value?.open()
}

// 编辑按钮点击
const handleEdit = (row: RelayGetPageOutput) => {
  formTitle.value = '编辑继电器'
  formRef.value?.open(row)
}

// 删除按钮点击
const handleDelete = (row: RelayGetPageOutput) => {
  ElMessageBox.confirm('确认要删除该继电器吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      try {
        const res = await new UspRelayApi().softDelete({ id: row.id })
        if (res.success) {
          ElMessage.success('删除成功')
          getList()
        }
      } catch (error) {
        console.error(error)
      }
    })
    .catch(() => {})
}

// 分页大小变化
const handleSizeChange = (val: number) => {
  pageInput.currentPage = 1
  pageInput.pageSize = val
  getList()
}

// 当前页变化
const handleCurrentChange = (val: number) => {
  pageInput.currentPage = val
  getList()
}

// 初始化
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
