<template>
  <ele-page>
    <!-- 搜索表单 -->
    <command-search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="roleId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        :highlight-current-row="true"
        cache-key="systemDevCommandListTable"
        :pagination="false"
      >
        <template #action="{ row }">
          <el-link
            v-if="disableRunNames.indexOf(row.name) == -1"
            type="primary"
            underline="never"
            @click="openEdit(row)"
          >
            执行
          </el-link>
        </template>

        <template #arguments="{ row }">
          <el-tag
            v-for="(_, name) in row.arguments"
            :key="name"
            style="margin-right: 5px"
            >{{ name }}</el-tag
          >
        </template>

        <template #options="{ row }">
          <el-tag
            v-for="(_, name) in row.options"
            :key="name"
            style="margin-right: 5px"
            >{{ name }}</el-tag
          >
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <command-edit v-model="showEdit" :data="current" @done="reload" />
  </ele-page>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import type { EleProTable } from 'ele-admin-plus';
  import type {
    DatasourceFunction,
    Columns
  } from 'ele-admin-plus/es/ele-pro-table/types';
  import CommandSearch from './components/command-search.vue';
  import CommandEdit from './components/command-edit.vue';
  import type { Role, RoleParam } from '@/api/system/role/model';
  import { listCommand } from '@/api/system/dev-command';

  defineOptions({ name: 'SystemDevCommandList' });

  /** 表格实例 */
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  /** 部分脚本不允许前端执行 */
  const disableRunNames = [
    'admin:worker',
    'admin:gateway',
    'vendor:publish',
    'run',
    'vendor:publish'
  ];
  /** 表格列配置 */
  const columns = ref<Columns>([
    {
      type: 'index',
      columnKey: 'index',
      width: 50,
      align: 'center'
    },
    {
      prop: 'name',
      label: '名称',
      sortable: 'custom',
      width: 260
    },
    {
      prop: 'type',
      label: '类型',
      sortable: 'custom',
      width: 120
    },
    {
      prop: 'description',
      label: '简介',
      minWidth: 240
    },
    {
      prop: 'class',
      label: '脚本类',
      minWidth: 180,
      align: 'left'
    },
    {
      prop: 'arguments',
      label: '参数',
      minWidth: 180,
      slot: 'arguments',
      align: 'left'
    },
    {
      prop: 'options',
      label: '配置',
      minWidth: 230,
      slot: 'options',
      align: 'left'
    },
    {
      columnKey: 'action',
      label: '操作',
      width: 100,
      align: 'center' /* ,
      fixed: 'right' */,
      slot: 'action',
      hideInPrint: true,
      hideInExport: true
    }
  ]);

  /** 当前编辑数据 */
  const current = ref<Role | null>(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource: DatasourceFunction = ({ pages, where, orders }) => {
    return listCommand({ ...where, ...orders, ...pages });
  };

  /** 搜索 */
  const reload = (where?: RoleParam) => {
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row?: Role) => {
    current.value = row ?? null;
    showEdit.value = true;
  };
</script>
