<!-- 平台接入列表 -->
<template>
  <div class="page-full">
    <el-card shadow="never" class="page-full-part">
      <el-form :model="params" inline ref="queryRef">
        <el-form-item prop="deptName" class="mr10">
          <el-input @keyup.enter.native="getList" style="width: 240px;" v-model="params.keyWord" :placeholder="$t('message.iotCard.platformManage.search.placeholder')" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">
            <el-icon>
              <ele-Search />
            </el-icon>
            {{ $t('message.iotCard.platformManage.search.query') }}
          </el-button>
          <el-button v-auth="'reset'" @click="resetQuery()">
            <el-icon>
              <ele-Refresh />
            </el-icon>
            {{ $t('message.iotCard.platformManage.search.reset') }}
          </el-button>
          <el-button v-auth="'add'" type="primary" @click="toAddItemPage()">
            <el-icon>
              <ele-FolderAdd />
            </el-icon>
            {{ $t('message.iotCard.platformManage.search.add') }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" max-height="calc(100vh  - 210px);" v-loading="loading" style="width: 100%">
        <el-table-column v-col="'name'" :label="$t('message.iotCard.platformManage.table.columns.name')" prop="name" align="left" />
        <el-table-column v-col="'type'" :label="$t('message.iotCard.platformManage.table.columns.platformType')" prop="types" :formatter="formatPlatformType" align="center" width="120" />
        <el-table-column v-col="'appKey'" :label="$t('message.iotCard.platformManage.table.columns.appId')" prop="appKey" align="center" />
        <el-table-column v-col="'simStatus'" :label="$t('message.iotCard.platformManage.table.columns.status')" prop="simStatus" align="center" width="140">
          <template #default="scope">
            <el-tag :type="scope.row.status === '0' ? 'danger' : ''">{{ formatStatus(scope.row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column v-col="'remark'" :label="$t('message.iotCard.platformManage.table.columns.description')" prop="remark" align="center" show-overflow-tooltip />
        <el-table-column v-col="'handle'" width="110" :label="$t('message.iotCard.platformManage.table.columns.actions')" fixed="right" prop="handle" align="center">
          <template #default="scope">
            <el-button v-auth="'detail'" size="small" text type="primary" @click="onOpenDetail(scope.row)">{{ $t('message.iotCard.platformManage.table.actions.edit') }}</el-button>
            <el-button v-auth="'del'" size="small" text type="warning" @click="onDel(scope.row)">{{ $t('message.iotCard.platformManage.table.actions.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
    </el-card>
    <AddOrEditItem ref="AddOrEditItemRef" @updateList="getList()" />
  </div>
</template>

<script lang="ts" setup>
import api from '/@/api/modules/iotCard';
import { defineAsyncComponent, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useSearch } from "/@/hooks/useCommon"
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
const AddOrEditItem = defineAsyncComponent(() => import('./addOrEditItem.vue'));

const { params, tableData, getList, loading } = useSearch<any[]>(
  api.platform.getList,
  "Data"
)
getList();

const AddOrEditItemRef = ref();

/** 重置按钮操作 */
const resetQuery = () => {
  params.keyWord = ""
  getList();
};

/**
 * 单一删除
 */
const onDel = (row: any) => {
  ElMessageBox.confirm(t('message.iotCard.platformManage.messages.deleteConfirm', { cardNumber: row.accNumber }), t('message.iotCard.platformManage.messages.tip'), {
    confirmButtonText: t('message.iotCard.platformManage.messages.confirm'),
    cancelButtonText: t('message.iotCard.platformManage.messages.cancel'),
    type: 'warning',
  }).then(async () => {
    await api.simCard.deleteItem({ ids: [row.id] });
    ElMessage.success(t('message.iotCard.platformManage.messages.deleteSuccess'));
    getList();
  });
};

const formatStatus = (val: any) => {
  // 1：开启 0：禁用
  const statuses = [t('message.iotCard.platformManage.status.disabled'), t('message.iotCard.platformManage.status.enabled')];
  return statuses[val];
}

const formatPlatformType = (row: any) => {
  // 1：电信 2：联通 3：移动
  const types = ['', t('message.iotCard.platformManage.addOrEdit.operators.telecom'), t('message.iotCard.platformManage.addOrEdit.operators.unicom'), t('message.iotCard.platformManage.addOrEdit.operators.mobile')];
  return types[row.types];
}

const onOpenDetail = (item: any) => {
  AddOrEditItemRef.value.openDialog(item);
}

const toAddItemPage = () => {
  AddOrEditItemRef.value.openDialog();
}
</script>
