<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api'

const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const query = reactive({
  page: 1,
  size: 50,
  tenant_id: ''
})

// 租户列表
const tenants = ref([])

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('')
const editingId = ref(null)
const formRef = ref(null)
const form = reactive({
  tenant_id: '',
  channel_name: '',
  channel_type: 'dingtalk_bot',
  webhook_url: '',
  sign_secret: '',
  description: ''
})

const rules = {
  tenant_id: [{ required: true, message: '请选择租户', trigger: 'change' }],
  channel_name: [{ required: true, message: '请输入渠道名称', trigger: 'blur' }],
  channel_type: [{ required: true, message: '请选择渠道类型', trigger: 'change' }],
  webhook_url: [{ required: true, message: '请输入 Webhook 地址', trigger: 'blur' }]
}

const channelTypes = [
  { value: 'dingtalk_bot', label: '钉钉机器人' },
  { value: 'wecom_bot', label: '企微机器人' }
]

async function fetchList() {
  loading.value = true
  try {
    const params = { ...query }
    if (!params.tenant_id) delete params.tenant_id
    
    const res = await api.get('/api/notification-channels', { params })
    tableData.value = res.data.items || []
    total.value = res.data.total || 0
  } catch (e) {
    console.error(e)
  } finally {
    loading.value = false
  }
}

async function fetchTenants() {
  try {
    const res = await api.get('/api/tenants', { params: { size: 1000 } })
    tenants.value = res.data.items || []
  } catch (e) {
    console.error(e)
  }
}

function handleSearch() {
  query.page = 1
  fetchList()
}

function handleCreate() {
  editingId.value = null
  dialogTitle.value = '新建通知渠道'
  Object.assign(form, {
    tenant_id: '',
    channel_name: '',
    channel_type: 'dingtalk_bot',
    webhook_url: '',
    sign_secret: '',
    description: ''
  })
  dialogVisible.value = true
}

function handleEdit(row) {
  editingId.value = row.id
  dialogTitle.value = '编辑通知渠道'
  Object.assign(form, {
    tenant_id: row.tenant_id,
    channel_name: row.channel_name,
    channel_type: row.channel_type,
    webhook_url: row.webhook_url,
    sign_secret: row.sign_secret || '',
    description: row.description || ''
  })
  dialogVisible.value = true
}

async function handleSubmit() {
  await formRef.value.validate()
  
  try {
    if (editingId.value) {
      await api.put(`/api/notification-channels/${editingId.value}`, form)
      ElMessage.success('更新成功')
    } else {
      await api.post('/api/notification-channels', form)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    fetchList()
  } catch (e) {
    // 错误已在拦截器处理
  }
}

async function handleDelete(row) {
  await ElMessageBox.confirm(`确定删除渠道 "${row.channel_name}" 吗？`, '提示', {
    type: 'warning'
  })
  
  try {
    await api.delete(`/api/notification-channels/${row.id}`)
    ElMessage.success('删除成功')
    fetchList()
  } catch (e) {
    // 错误已在拦截器处理
  }
}

async function handleToggle(row) {
  try {
    await api.put(`/api/notification-channels/${row.id}`, {
      is_enabled: !row.is_enabled
    })
    ElMessage.success(row.is_enabled ? '已禁用' : '已启用')
    fetchList()
  } catch (e) {
    // 错误已在拦截器处理
  }
}

async function handleTest(row) {
  try {
    ElMessage.info('发送测试消息中...')
    const res = await api.post(`/api/notification-channels/${row.id}/test`)
    if (res.data.success) {
      ElMessage.success('测试消息发送成功')
    } else {
      ElMessage.error(`发送失败: ${res.data.message}`)
    }
  } catch (e) {
    // 错误已在拦截器处理
  }
}

function getTenantName(tenantId) {
  const tenant = tenants.value.find(t => t.code === tenantId)
  return tenant ? tenant.name : tenantId
}

function getChannelTypeName(type) {
  const item = channelTypes.find(t => t.value === type)
  return item ? item.label : type
}

onMounted(() => {
  fetchList()
  fetchTenants()
})
</script>

<template>
  <div class="page-container">
    <div class="page-header">
      <div class="title">通知渠道管理</div>
      <el-button type="primary" @click="handleCreate">
        <el-icon><Plus /></el-icon>
        新建渠道
      </el-button>
    </div>
    
    <div class="page-tip">
      <el-alert type="info" :closable="false">
        通知渠道用于定时任务执行后发送消息。支持钉钉机器人和企微机器人。
        脚本中设置 <code>result = {'content': '消息内容', 'title': '标题'}</code> 变量，任务执行后会自动发送到配置的渠道。
      </el-alert>
    </div>
    
    <!-- 筛选 -->
    <div class="filter-bar">
      <el-select v-model="query.tenant_id" placeholder="全部租户" clearable style="width: 180px">
        <el-option v-for="t in tenants" :key="t.code" :label="t.name" :value="t.code" />
      </el-select>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>
    
    <!-- 表格 -->
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column label="租户" width="120">
        <template #default="{ row }">
          {{ getTenantName(row.tenant_id) }}
        </template>
      </el-table-column>
      <el-table-column prop="channel_name" label="渠道名称" min-width="150" />
      <el-table-column label="类型" width="120">
        <template #default="{ row }">
          <el-tag :type="row.channel_type === 'dingtalk_bot' ? 'primary' : 'success'" size="small">
            {{ getChannelTypeName(row.channel_type) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="webhook_url" label="Webhook" min-width="200" show-overflow-tooltip />
      <el-table-column label="状态" width="80">
        <template #default="{ row }">
          <el-tag :type="row.is_enabled ? 'success' : 'info'" size="small">
            {{ row.is_enabled ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button type="info" link size="small" @click="handleTest(row)">测试</el-button>
          <el-button :type="row.is_enabled ? 'warning' : 'success'" link size="small" @click="handleToggle(row)">
            {{ row.is_enabled ? '禁用' : '启用' }}
          </el-button>
          <el-button type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="550px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属租户" prop="tenant_id">
          <el-select v-model="form.tenant_id" placeholder="选择租户" style="width: 100%">
            <el-option v-for="t in tenants" :key="t.code" :label="t.name" :value="t.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道名称" prop="channel_name">
          <el-input v-model="form.channel_name" placeholder="如: 销售群机器人" />
        </el-form-item>
        <el-form-item label="渠道类型" prop="channel_type">
          <el-select v-model="form.channel_type" style="width: 100%">
            <el-option v-for="t in channelTypes" :key="t.value" :label="t.label" :value="t.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="Webhook" prop="webhook_url">
          <el-input v-model="form.webhook_url" placeholder="机器人 Webhook 地址" />
          <div class="form-tip">
            <template v-if="form.channel_type === 'dingtalk_bot'">
              钉钉机器人 Webhook 格式: https://oapi.dingtalk.com/robot/send?access_token=xxx
            </template>
            <template v-else>
              企微机器人 Webhook 格式: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx
            </template>
          </div>
        </el-form-item>
        <el-form-item v-if="form.channel_type === 'dingtalk_bot'" label="加签密钥">
          <el-input v-model="form.sign_secret" placeholder="SEC开头的密钥（可选）" />
          <div class="form-tip">
            如果创建机器人时选择了「加签」安全设置，请填写密钥（以 SEC 开头）
          </div>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" type="textarea" :rows="2" placeholder="渠道描述（可选）" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header .title {
  font-size: 20px;
  font-weight: 600;
}

.page-tip {
  margin-bottom: 16px;
}

.page-tip code {
  background: #f5f7fa;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
}

.filter-bar {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
}

.form-tip {
  color: #909399;
  font-size: 12px;
  margin-top: 4px;
}
</style>
