<!-- 平台接入列表 -->
<template>
  <div class="page-full">
    <el-card shadow="nover" 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="请输入关键字搜索" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">
            <el-icon>
              <ele-Search />
            </el-icon>
            查询
          </el-button>
          <el-button v-auth="'reset'" @click="resetQuery()">
            <el-icon>
              <ele-Refresh />
            </el-icon>
            重置
          </el-button>
          <el-button v-auth="'add'" type="primary" @click="toAddItemPage()">
            <el-icon>
              <ele-FolderAdd />
            </el-icon>
            新增
          </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="名称" prop="name" align="left" />
        <el-table-column v-col="'type'" label="平台类型" prop="types" :formatter="function (row: any) { return ['', '电信', '联通', '移动'][row.types] }" align="center" width="120" />
        <el-table-column v-col="'appKey'" label="App ID" prop="appKey" align="center" />
        <el-table-column v-col="'simStatus'" label="状态" prop="simStatus" align="center" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === '0' ? 'danger' : 'primary'">{{ formatStatus(scope.row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column v-col="'remark'" label="说明" prop="remark" align="center" show-overflow-tooltip />
        <el-table-column v-col="'handle'" width="110" label="操作" fixed="right" prop="handle" align="center">
          <template #default="scope">
            <el-button v-auth="'detail'" size="small" text type="primary" @click="onOpenDetail(scope.row)">编辑</el-button>
            <el-button v-auth="'del'" size="small" text type="warning" @click="onDel(scope.row)">删除</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/iotCard';
import { defineAsyncComponent, ref } from 'vue';
import { ElMessageBox, ElMessage, formatter } from 'element-plus';
import { useSearch } from "/@/hooks/useCommon"
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(`此操作将卡号为：“${row.accNumber}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    await api.simCard.deleteItem({ ids: [row.id] });
    ElMessage.success('删除成功');
    getList();
  });
};

const formatStatus = (val: any) => {
  // 1：开启 0：禁用
  return ['禁用', '开启'][val];
}

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

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