<template>
  <ArtTableFullScreen>
    <div class="carrier-page" id="table-full-screen">
      <!-- 搜索栏 -->
      <ArtSearchBar
        v-model:filter="searchForm"
        :items="searchFormItems"
        label-width="85"
        :show-expand="false"
        @reset="handleReset"
        @search="handleSearch"
      ></ArtSearchBar>

      <ElCard shadow="never" class="art-table-card">
        <!-- 表格头部 -->
        <ArtTableHeader
          :columnList="columnOptions"
          v-model:columns="columnChecks"
          @refresh="handleRefresh"
        >
          <template #left>
            <ElButton @click="showDialog('add')" v-permission="'carrier:add'">新增运营商</ElButton>
          </template>
        </ArtTableHeader>

        <!-- 表格 -->
        <ArtTable
          ref="tableRef"
          row-key="id"
          :loading="loading"
          :data="carrierList"
          :currentPage="pagination.page"
          :pageSize="pagination.pageSize"
          :total="pagination.total"
          :marginTop="10"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
          <template #default>
            <ElTableColumn v-for="col in columns" :key="col.prop || col.type" v-bind="col" />
          </template>
        </ArtTable>

        <!-- 新增/编辑对话框 -->
        <ElDialog
          v-model="dialogVisible"
          :title="dialogType === 'add' ? '新增运营商' : '编辑运营商'"
          width="30%"
        >
          <ElForm ref="formRef" :model="form" :rules="rules" label-width="120px">
            <ElFormItem label="运营商编码" prop="carrier_code">
              <ElInput
                v-model="form.carrier_code"
                placeholder="请输入运营商编码或点击生成"
                :disabled="dialogType === 'edit'"
                clearable
              >
                <template #append>
                  <ElButton v-if="dialogType === 'add'" @click="handleGenerateCarrierCode"
                    >生成编码</ElButton
                  >
                </template>
              </ElInput>
            </ElFormItem>
            <ElFormItem label="运营商名称" prop="carrier_name">
              <ElInput v-model="form.carrier_name" placeholder="请输入运营商名称" />
            </ElFormItem>
            <ElFormItem label="运营商类型" prop="carrier_type">
              <ElSelect
                v-model="form.carrier_type"
                placeholder="请选择运营商类型"
                style="width: 100%"
                :disabled="dialogType === 'edit'"
              >
                <ElOption
                  v-for="item in CARRIER_TYPE_OPTIONS"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </ElSelect>
            </ElFormItem>
            <ElFormItem label="运营商描述" prop="description">
              <ElInput
                v-model="form.description"
                type="textarea"
                :rows="3"
                placeholder="请输入运营商描述"
              />
            </ElFormItem>
            <ElFormItem label="流量重置日" prop="data_reset_day">
              <ElInputNumber
                v-model="form.data_reset_day"
                :min="1"
                :max="28"
                :step="1"
                controls-position="right"
                placeholder="请输入流量重置日"
                style="width: 100%"
              />
              <div style="margin-top: 5px; font-size: 12px; color: #909399">
                运营商每月清零网关计数器的日期（1-28日），默认为1日
              </div>
            </ElFormItem>
            <ElFormItem label="实名链接类型" prop="realname_link_type">
              <ElSelect
                v-model="form.realname_link_type"
                placeholder="请选择实名链接类型"
                style="width: 100%"
              >
                <ElOption label="无需实名" value="none" />
                <ElOption label="模板实名" value="template" />
                <ElOption label="网关实名" value="gateway" />
              </ElSelect>
            </ElFormItem>
            <ElFormItem
              v-if="form.realname_link_type === 'template'"
              label="实名链接模板"
              prop="realname_link_template"
            >
              <ElInput
                v-model="form.realname_link_template"
                type="textarea"
                :rows="2"
                placeholder="请输入实名链接模板"
              />
              <div style="margin-top: 5px; font-size: 12px; color: #909399">
                支持变量：{iccid}、{msisdn}等
              </div>
            </ElFormItem>
          </ElForm>
          <template #footer>
            <div class="dialog-footer">
              <ElButton @click="dialogVisible = false">取消</ElButton>
              <ElButton type="primary" @click="handleSubmit(formRef)" :loading="submitLoading">
                提交
              </ElButton>
            </div>
          </template>
        </ElDialog>
      </ElCard>
    </div>
  </ArtTableFullScreen>
</template>

<script setup lang="ts">
  import { h, watch, nextTick } from 'vue'
  import { CarrierService } from '@/api/modules'
  import { ElMessage, ElMessageBox, ElTag, ElSwitch, ElButton } from 'element-plus'
  import type { FormInstance, FormRules } from 'element-plus'
  import type { Carrier, CarrierType } from '@/types/api'
  import type { SearchFormItem } from '@/types'
  import { useCheckedColumns } from '@/composables/useCheckedColumns'
  import { useAuth } from '@/composables/useAuth'
  import { generateCarrierCode } from '@/utils/codeGenerator'
  import { formatDateTime } from '@/utils/business/format'
  import {
    CommonStatus,
    getStatusText,
    CARRIER_TYPE_OPTIONS,
    getCarrierTypeLabel,
    getCarrierTypeColor
  } from '@/config/constants'

  defineOptions({ name: 'CarrierManagement' })

  const { hasAuth } = useAuth()
  const canUpdateCarrierStatus = hasAuth('carrier:update_status')
  const dialogVisible = ref(false)
  const loading = ref(false)
  const submitLoading = ref(false)
  const tableRef = ref()

  // 搜索表单初始值
  const initialSearchState = {
    carrier_name: '',
    carrier_type: null,
    status: null
  }

  // 搜索表单
  const searchForm = reactive({ ...initialSearchState })

  // 搜索表单配置
  const searchFormItems: SearchFormItem[] = [
    {
      label: '运营商名称',
      prop: 'carrier_name',
      type: 'input',
      config: {
        clearable: true,
        placeholder: '请输入运营商名称'
      }
    },
    {
      label: '运营商类型',
      prop: 'carrier_type',
      type: 'select',
      config: {
        clearable: true,
        placeholder: '请选择运营商类型'
      },
      options: () => [
        { label: '中国移动', value: 'CMCC' },
        { label: '中国联通', value: 'CUCC' },
        { label: '中国电信', value: 'CTCC' },
        { label: '中国广电', value: 'CBN' }
      ]
    },
    {
      label: '状态',
      prop: 'status',
      type: 'select',
      config: {
        clearable: true,
        placeholder: '请选择状态'
      },
      options: () => [
        { label: '启用', value: CommonStatus.ENABLED },
        { label: '禁用', value: CommonStatus.DISABLED }
      ]
    }
  ]

  // 分页
  const pagination = reactive({
    page: 1,
    pageSize: 20,
    total: 0
  })

  // 列配置
  const columnOptions = [
    { label: '运营商编码', prop: 'carrier_code' },
    { label: '运营商名称', prop: 'carrier_name' },
    { label: '运营商类型', prop: 'carrier_type' },
    { label: '运营商描述', prop: 'description' },
    ...(canUpdateCarrierStatus ? [{ label: '状态', prop: 'status' }] : []),
    { label: '创建时间', prop: 'created_at' }
  ]

  const formRef = ref<FormInstance>()

  const rules = reactive<FormRules>({
    carrier_code: [
      { required: true, message: '请输入运营商编码', trigger: 'blur' },
      { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
    ],
    carrier_name: [
      { required: true, message: '请输入运营商名称', trigger: 'blur' },
      { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
    ],
    carrier_type: [{ required: true, message: '请选择运营商类型', trigger: 'change' }],
    description: [{ max: 500, message: '描述不能超过500个字符', trigger: 'blur' }],
    data_reset_day: [
      {
        validator: (rule: any, value: any, callback: any) => {
          if (value === null || value === undefined || value === '') {
            callback() // 允许为空
          } else {
            const numValue = Number(value)
            if (isNaN(numValue)) {
              callback(new Error('请输入有效的数字'))
            } else if (!Number.isInteger(numValue)) {
              callback(new Error('请输入整数'))
            } else if (numValue < 1 || numValue > 28) {
              callback(new Error('流量重置日必须在1-28之间'))
            } else {
              callback()
            }
          }
        },
        trigger: 'blur'
      }
    ],
    realname_link_type: [{ required: true, message: '请选择实名链接类型', trigger: 'change' }],
    realname_link_template: [
      {
        required: true,
        validator: (rule: any, value: any, callback: any) => {
          if (form.realname_link_type === 'template' && !value) {
            callback(new Error('请输入实名链接模板'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  })

  const form = reactive<any>({
    id: 0,
    carrier_code: '',
    carrier_name: '',
    carrier_type: null,
    description: '',
    data_reset_day: 1,
    realname_link_type: 'none',
    realname_link_template: ''
  })

  const carrierList = ref<Carrier[]>([])

  // 动态列配置
  const { columnChecks, columns } = useCheckedColumns(() => [
    {
      prop: 'carrier_code',
      label: '运营商编码',
      minWidth: 160
    },
    {
      prop: 'carrier_name',
      label: '运营商名称',
      minWidth: 150
    },
    {
      prop: 'carrier_type',
      label: '运营商类型',
      width: 120,
      formatter: (row: any) => {
        const color = getCarrierTypeColor(row.carrier_type)
        return h(
          ElTag,
          { style: `background-color: ${color}; border-color: ${color}; color: #fff;` },
          () => getCarrierTypeLabel(row.carrier_type)
        )
      }
    },
    {
      prop: 'description',
      label: '运营商描述',
      minWidth: 200,
      showOverflowTooltip: true
    },
    ...(canUpdateCarrierStatus
      ? [
          {
            prop: 'status',
            label: '状态',
            width: 100,
            formatter: (row: any) => {
              return h(ElSwitch, {
                modelValue: row.status,
                activeValue: CommonStatus.ENABLED,
                inactiveValue: CommonStatus.DISABLED,
                activeText: getStatusText(CommonStatus.ENABLED),
                inactiveText: getStatusText(CommonStatus.DISABLED),
                inlinePrompt: true,
                'onUpdate:modelValue': (val: string | number | boolean) =>
                  handleStatusChange(row, val as number)
              })
            }
          }
        ]
      : []),
    {
      prop: 'created_at',
      label: '创建时间',
      width: 180,
      formatter: (row: any) => formatDateTime(row.created_at)
    },
    {
      prop: 'actions',
      label: '操作',
      width: 160,
      fixed: 'right',
      formatter: (row: any) => {
        const buttons: any[] = []

        if (hasAuth('carrier:edit')) {
          buttons.push(
            h(
              ElButton,
              {
                type: 'primary',
                link: true,
                onClick: () => showDialog('edit', row)
              },
              () => '编辑'
            )
          )
        }

        if (hasAuth('carrier:delete')) {
          buttons.push(
            h(
              ElButton,
              {
                type: 'danger',
                link: true,
                onClick: () => deleteCarrier(row)
              },
              () => '删除'
            )
          )
        }

        return h('div', { style: 'display: flex; gap: 8px;' }, buttons)
      }
    }
  ])

  onMounted(() => {
    getTableData()
  })

  // 监听对话框关闭，清除验证状态
  watch(dialogVisible, (val) => {
    if (!val) {
      nextTick(() => {
        formRef.value?.clearValidate()
      })
    }
  })

  // 获取运营商列表
  const getTableData = async () => {
    loading.value = true
    try {
      const params = {
        page: pagination.page,
        page_size: pagination.pageSize,
        carrier_name: searchForm.carrier_name || undefined,
        carrier_type: searchForm.carrier_type || undefined,
        status: searchForm.status !== null ? searchForm.status : undefined
      }
      const res = await CarrierService.getCarriers(params)
      if (res.code === 0) {
        carrierList.value = res.data.items || []
        pagination.total = res.data.total || 0
      }
    } catch (error) {
      console.error(error)
    } finally {
      loading.value = false
    }
  }

  // 重置搜索
  const handleReset = () => {
    Object.assign(searchForm, { ...initialSearchState })
    pagination.page = 1
    getTableData()
  }

  // 搜索
  const handleSearch = () => {
    pagination.page = 1
    getTableData()
  }

  // 刷新表格
  const handleRefresh = () => {
    getTableData()
  }

  // 处理表格分页变化
  const handleSizeChange = (newPageSize: number) => {
    pagination.pageSize = newPageSize
    getTableData()
  }

  const handleCurrentChange = (newCurrentPage: number) => {
    pagination.page = newCurrentPage
    getTableData()
  }

  const dialogType = ref('add')

  // 显示新增/编辑对话框
  const showDialog = (type: string, row?: any) => {
    dialogVisible.value = true
    dialogType.value = type

    if (type === 'edit' && row) {
      form.id = row.id
      form.carrier_code = row.carrier_code
      form.carrier_name = row.carrier_name
      form.carrier_type = row.carrier_type
      form.description = row.description
      form.data_reset_day = row.data_reset_day || 1
      form.realname_link_type = row.realname_link_type || 'none'
      form.realname_link_template = row.realname_link_template || ''
    } else {
      form.id = 0
      form.carrier_code = ''
      form.carrier_name = ''
      form.carrier_type = null
      form.description = ''
      form.data_reset_day = 1
      form.realname_link_type = 'none'
      form.realname_link_template = ''
    }

    // 清除表单验证状态
    nextTick(() => {
      formRef.value?.clearValidate()
    })
  }

  // 处理编码生成
  const handleGenerateCarrierCode = () => {
    form.carrier_code = generateCarrierCode()
    // 清除该字段的验证错误提示
    nextTick(() => {
      formRef.value?.clearValidate('carrier_code')
    })
    ElMessage.success('编码生成成功')
  }

  // 删除运营商
  const deleteCarrier = (row: any) => {
    ElMessageBox.confirm(`确定删除运营商 ${row.carrier_name} 吗？`, '删除确认', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'error'
    })
      .then(async () => {
        try {
          await CarrierService.deleteCarrier(row.id)
          ElMessage.success('删除成功')
          await getTableData()
        } catch (error) {
          console.error(error)
        }
      })
      .catch(() => {
        // 用户取消删除
      })
  }

  // 提交表单
  const handleSubmit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return

    await formEl.validate(async (valid) => {
      if (valid) {
        submitLoading.value = true
        try {
          const data = {
            carrier_code: form.carrier_code,
            carrier_name: form.carrier_name,
            carrier_type: form.carrier_type as CarrierType,
            description: form.description || undefined,
            data_reset_day: form.data_reset_day || null,
            realname_link_type: form.realname_link_type,
            realname_link_template:
              form.realname_link_type === 'template' ? form.realname_link_template : undefined
          }

          if (dialogType.value === 'add') {
            await CarrierService.createCarrier(data)
            ElMessage.success('新增成功')
          } else {
            const updateData = {
              carrier_name: form.carrier_name,
              description: form.description || undefined,
              data_reset_day: form.data_reset_day || null,
              realname_link_type: form.realname_link_type,
              realname_link_template:
                form.realname_link_type === 'template' ? form.realname_link_template : undefined
            }
            await CarrierService.updateCarrier(form.id, updateData)
            ElMessage.success('修改成功')
          }

          dialogVisible.value = false
          formEl.resetFields()
          await getTableData()
        } catch (error) {
          console.error(error)
        } finally {
          submitLoading.value = false
        }
      }
    })
  }

  // 状态切换
  const handleStatusChange = async (row: any, newStatus: number) => {
    const oldStatus = row.status
    // 先更新UI
    row.status = newStatus
    try {
      await CarrierService.updateCarrierStatus(row.id, newStatus)
      ElMessage.success('状态切换成功')
    } catch (error) {
      // 切换失败，恢复原状态
      row.status = oldStatus
      console.error(error)
    }
  }
</script>

<style scoped lang="scss">
  .carrier-page {
    height: 100%;
  }
</style>
