<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="应用" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入应用标识"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="全部" clearable class="!w-160px">
          <el-option label="Android" value="android" />
          <el-option label="iOS" value="ios" />
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-input
          v-model="queryParams.channel"
          placeholder="请输入渠道"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="versionName">
        <el-input
          v-model="queryParams.versionName"
          placeholder="如 1.2.3"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
      <el-form-item label="强更" prop="isForceUpdate">
        <el-select v-model="queryParams.isForceUpdate" placeholder="全部" clearable class="!w-120px">
          <el-option label="是" :value="true" />
          <el-option label="否" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="isActive">
        <el-select v-model="queryParams.isActive" placeholder="全部" clearable class="!w-120px">
          <el-option label="生效" :value="true" />
          <el-option label="停用" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" prop="releasedAt">
        <el-date-picker
          v-model="queryParams.releasedAt"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-260px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['keyboard:app-versions:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['keyboard:app-versions:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['keyboard:app-versions:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="ID" align="center" prop="id" width="70" />
      <el-table-column label="应用" align="center" prop="appId" min-width="120" />
      <el-table-column label="平台" align="center" prop="platform" width="90">
        <template #default="scope">
          <el-tag v-if="scope.row.platform === 'android'" type="success">Android</el-tag>
          <el-tag v-else-if="scope.row.platform === 'ios'" type="primary">iOS</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="渠道" align="center" prop="channel" min-width="120" />
      <el-table-column label="版本号" align="center" prop="versionName" width="120" />
      <el-table-column label="版本码" align="center" prop="versionCode" width="100" />
      <el-table-column label="最低版本" align="center" prop="minSupportedCode" width="100" />
      <el-table-column label="强更" align="center" prop="isForceUpdate" width="80">
        <template #default="scope">
          <el-tag :type="scope.row.isForceUpdate ? 'danger' : 'info'">
            {{ scope.row.isForceUpdate ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isActive" width="80">
        <template #default="scope">
          <el-tag :type="scope.row.isActive ? 'success' : 'info'">
            {{ scope.row.isActive ? '生效' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="下载地址" align="center" prop="downloadUrl" min-width="180">
        <template #default="scope">
          <el-link v-if="scope.row.downloadUrl" :href="scope.row.downloadUrl" target="_blank" type="primary">
            打开链接
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        align="center"
        prop="releasedAt"
        :formatter="dateFormatter"
        width="180"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        :formatter="dateFormatter"
        width="180"
      />
      <el-table-column label="操作" align="center" fixed="right" width="130">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['keyboard:app-versions:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['keyboard:app-versions:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <AppVersionsForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { AppVersionsApi, AppVersions } from '@/api/keyboard/appversions'
import AppVersionsForm from './AppVersionsForm.vue'

defineOptions({ name: 'AppVersions' })

const message = useMessage()
const { t } = useI18n()

const loading = ref(true)
const list = ref<AppVersions[]>([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  appId: undefined,
  platform: undefined,
  channel: undefined,
  versionName: undefined,
  isForceUpdate: undefined,
  isActive: undefined,
  releasedAt: []
})
const queryFormRef = ref()
const exportLoading = ref(false)

const getList = async () => {
  loading.value = true
  try {
    const data = await AppVersionsApi.getAppVersionsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await AppVersionsApi.deleteAppVersions(id)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: AppVersions[]) => {
  checkedIds.value = records.map((item) => item.id!)
}
const handleDeleteBatch = async () => {
  try {
    await message.delConfirm()
    await AppVersionsApi.deleteAppVersionsList(checkedIds.value)
    checkedIds.value = []
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const handleExport = async () => {
  try {
    await message.exportConfirm()
    exportLoading.value = true
    const data = await AppVersionsApi.exportAppVersions(queryParams)
    download.excel(data, 'App版本管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

onMounted(() => {
  getList()
})
</script>
