import * as api from "./api.js";
import {
  AddReq,
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  DelReq,
  dict,
  EditReq,
  UserPageQuery,
  UserPageRes,
  utils
} from "@fast-crud/fast-crud";
import { ref } from "vue";

export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
  const pageRequest = async (query: UserPageQuery): Promise<UserPageRes> => {
    return await api.GetList(query);
  };
  const editRequest = async ({ form, row }: EditReq) => {
    if (form.id == null) {
      form.id = row.id;
    }
    return await api.UpdateObj(form);
  };
  const delRequest = async ({ row }: DelReq) => {
    return await api.DelObj(row.id);
  };

  const addRequest = async ({ form }: AddReq) => {
    return await api.AddObj(form);
  };
  const currentRow = ref();
  return {
    crudOptions: {
      request: {
        pageRequest,
        addRequest,
        editRequest,
        delRequest
      },
      table: {
        //默认展开第一行
        defaultExpandedRowKeys: [1],
        customRow(record: any, index: number) {
          const clazz = record.id === currentRow.value ? "fs-current-row" : "";
          return {
            onClick() {
              currentRow.value = record.id;
            },
            class: clazz
          };
        },
        onExpand(open: boolean, row: any) {
          utils.logger.info("expand事件：", open, row);
        },
        slots: {
          expandedRowRender: (scope: any) => {
            return (
              <div>
                index: {scope.index} ; row: {JSON.stringify(scope.record)}
              </div>
            );
          }
        }
      },
      columns: {
        id: {
          title: "ID",
          type: "number",
          column: {
            width: 50
          },
          form: {
            show: false
          }
        },
        radio: {
          title: "状态",
          search: { show: true },
          type: "dict-radio",
          dict: dict({
            url: "/mock/dicts/OpenStatusEnum?single"
          })
        }
      }
    }
  };
}
