<template>
  <div class="page">
    <PageHeader
      :title="t('ipRules.title')"
      :description="t('ipRules.description')"
    >
      <template #actions>
        <el-button type="primary" @click="showCreate = true">{{ t('ipRules.addRule') }}</el-button>
      </template>
    </PageHeader>

    <AppCard>
      <el-table :data="rules" size="small">
        <el-table-column prop="cidr" :label="t('ipRules.cidr')" min-width="160" />
        <el-table-column prop="type" :label="t('common.type')" width="100" />
        <el-table-column prop="scope" :label="t('ipRules.scope')" width="120" />
        <el-table-column :label="t('common.enabled')" width="90">
          <template #default="{ row }">
            <el-tag :type="row.enabled ? 'success' : 'info'" size="small">{{ row.enabled ? t('common.yes') : t('common.no') }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="t('common.actions')" width="100" fixed="right">
          <template #default="{ row }">
            <el-button size="small" type="danger" link @click="remove(row)">{{ t('common.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </AppCard>

    <el-dialog v-model="showCreate" :title="t('ipRules.addRule')" width="480px" destroy-on-close>
      <el-form label-width="100px">
        <el-form-item :label="t('ipRules.cidr')"><el-input v-model="form.cidr" :placeholder="t('ipRules.cidrPlaceholder')" /></el-form-item>
        <el-form-item :label="t('common.type')">
          <el-select v-model="form.type">
            <el-option :label="t('ipRules.typeAllow')" value="allow" />
            <el-option :label="t('ipRules.typeDeny')" value="deny" />
          </el-select>
        </el-form-item>
        <el-form-item :label="t('ipRules.scope')">
          <el-select v-model="form.scope">
            <el-option :label="t('ipRules.scopeProxy')" value="proxy" />
            <el-option :label="t('ipRules.scopeAdmin')" value="admin" />
            <el-option :label="t('ipRules.scopeAll')" value="all" />
          </el-select>
          <p v-if="form.type === 'allow' && form.scope !== 'proxy'" class="form-hint form-hint--destructive">
            {{ t('ipRules.allowWarning') }}
          </p>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showCreate = false">{{ t('common.cancel') }}</el-button>
        <el-button type="primary" @click="create">{{ t('common.add') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
import api, { type IPRule } from '@/api/client'
import PageHeader from '@/components/PageHeader.vue'
import AppCard from '@/components/AppCard.vue'

const { t } = useI18n()

const rules = ref<IPRule[]>([])
const showCreate = ref(false)
const form = ref({ cidr: '', type: 'allow' as const, scope: 'proxy' as const })

async function load() {
  const res = await api.get('/ip-rules')
  rules.value = res.data.data
}

async function create() {
  await api.post('/ip-rules', form.value)
  showCreate.value = false
  form.value = { cidr: '', type: 'allow', scope: 'proxy' }
  ElMessage.success(t('ipRules.added'))
  load()
}

async function remove(row: IPRule) {
  await ElMessageBox.confirm(t('common.confirmDelete'))
  await api.delete(`/ip-rules/${row.id}`)
  load()
}

onMounted(load)
</script>
