import { getRoleList } from "@/api/system";
import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref, computed, onMounted } from "vue";
// 获取URLLIST
import { getGroupUrl } from "@/utils/getUrl/getUrl"
import { http } from "@/utils/http";
// 获取当前板块接口列表
const UrlList = await getGroupUrl(['kxsConfigServer']);

export function useApiGroup() {
  let form = reactive({
    groupName: "", //名称

  });
  const dataList = ref([]);
  const loading = ref(false);
  const dialogAddVisible = ref(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "名称",
      prop: "groupName",
      minWidth: 200
    },
    {
      label: "分类",
      prop: "groupKind",
      minWidth: 200
    },

    {
      label: "操作",
      fixed: "right",
      width: 200,
      slot: "operation"
    }
  ];
  // 当前页数量切换
  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
    onSearch()
  }
  // 当前页码切换
  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch()
  }
  // 选择表格项
  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
    onSearch()
  }
  // 搜索列表
  async function onSearch(type = 'search') {
    if (type === 'all') {
      form.groupName = "";

    }
    // if (type == 'search' && !Object.values(form).some(item => !!item)) {
    //   return ElMessage({
    //     message: "请输入查询条件",
    //     type: "error"
    //   });
    // };
    loading.value = true;
    const { status, msg, data }: any = await http.Request({ method: UrlList.kxsConfigServer.apiGrouplist.method, url: UrlList.kxsConfigServer.apiGrouplist.url, params: { ...form, pageSize: pagination.pageSize, pageNum: pagination.currentPage } });
    if (status === 1) {
      dataList.value = data.records;
      pagination.total = data.total;
      setTimeout(() => {
        loading.value = false;
      }, 500);
    } else {
      ElMessageBox.alert(msg, "提示", {
        confirmButtonText: "关闭",
        type: "warning"
      });
    }
  }
  onMounted(() => {
    onSearch();
  });

  // 删除
  function handleDelete(row) {
    ElMessageBox.confirm(
      `是否删除该api接口分组? `,
      "提示",
      {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning"
      }
    ).then(async () => {
      const { status, msg }: any = await http.Request({ method: UrlList.kxsConfigServer.apiGroupdelete.method, url: UrlList.kxsConfigServer.apiGroupdelete.url, params: row.id });
      if (status === 1) {
        ElMessage({
          message: "删除成功",
          type: "success"
        });
        onSearch();
      } else {
        ElMessageBox.alert(msg, "提示", {
          confirmButtonText: "关闭",
          type: "warning"
        });
      };
    })
  }

  // 新增
  const addVisible = ref(false);
  function handleAdd() {
    addVisible.value = true;
  };

  // 修改
  const editUpdateVisible = ref(false);
  const editUpdateFormData = ref({});
  function handleUpdate(row) {
    editUpdateVisible.value = true;
    // 表格数据赋值
    editUpdateFormData.value = row;
  };

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
    handleAdd,
    addVisible,
    handleUpdate,
    editUpdateVisible,
    editUpdateFormData,
    handleDelete,

  };
}
