<script setup lang="ts">
import { ref } from "vue";
import searchConfig from "./config/search.config";
import contentConfig from "./config/content.config";

import { PageContentInstance } from "/@/components/PageContent";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { usePageSearch, type PageHooks } from "/@/hooks/page";
import { usePermission } from "/@/hooks/core";

import { ElMessage } from "element-plus";
import apis from "./config/apis";
import { httpStatus } from "/@/api/system/role";
import { responseHandle } from "/@/utils/responseHandle";

const pageName = "scripts";
const { hasPermissionWithCode, permissions } = usePermission(pageName);
const pageContentRef = ref<PageContentInstance | null>(null);

async function deleteItem(id: string) {
  const response = await apis.delete_exec({ id });
  if (response.code === 1) {
    pageContentRef.value.onSearch();
  } else {
    ElMessage.warning(response.message);
  }
}
const handleStatus = async row => {
  const { id, status } = row;
  const { code, message } = await apis.status({
    id,
    status: status + "" === "1" ? "0" : "1"
  });

  if (code === 1) {
    pageContentRef.value.onSearch();
  } else {
    ElMessage.warning(message);
  }
};

const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig)
};
</script>

<template>
  <page-auth :page-name="pageName">
    <page-container
      :hooks="hooks"
      :page-name="pageName"
      :get-content-ref="ref => (pageContentRef = ref)"
      :content-config="contentConfig"
    >
      <template #content_action="row">
        <el-link
          :href="row.down_url"
          v-if="String(row.status) === '2' && hasPermissionWithCode('10')"
          target="_blank"
          :underline="false"
        >
          <ElButton
            link
            size="small"
            type="primary"
            :icon="useRenderIcon('download-line')"
          />
        </el-link>

        <el-popconfirm
          :title="String(row.status) === '1' ? '改为禁用？' : '改为启用？'"
          v-if="
            (permissions.some(i => i == '7') && row.status + '' === '1') ||
            (permissions.some(i => i == '6') && row.status + '' === '0')
          "
          @confirm="handleStatus(row)"
        >
          <template #reference>
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="'mini'"
              :icon="
                useRenderIcon(
                  String(row.status) === '1'
                    ? 'close-circle-line'
                    : 'checkbox-circle-line'
                )
              " /></template
        ></el-popconfirm>
      </template>
    </page-container>
  </page-auth>
</template>
