import type {
  LoadingConfig,
  AdaptiveConfig,
  PaginationProps
} from "@pureadmin/table";
import { ref, reactive } from "vue";
import { delObjectProperty } from "@pureadmin/utils";
import { clone, delay } from "@pureadmin/utils";

import Project from "@/model/project.js";

export function useColumns() {
  const loading = ref(true);
  const editMap = ref({});
  const dataList = ref([]);
  const options = ref([
    {
      value: 0,
      label: "未开始",
      type: "info"
    },
    {
      value: 1,
      label: "进行中",
      type: ""
    },
    {
      value: 2,
      label: "已完成",
      type: "success"
    }
  ]);

  const columns: TableColumnList = [
    {
      label: "ID",
      prop: "id",
      cellRenderer: ({ row, index }) => (
        <>
          {editMap.value[index]?.editable ? (
            <el-input v-model={row.id} />
          ) : (
            <p>{row.id}</p>
          )}
        </>
      )
    },
    {
      label: "项目名称",
      prop: "name",
      cellRenderer: ({ row, index }) => (
        <>
          {editMap.value[index]?.editable ? (
            <el-input v-model={row.name} />
          ) : (
            <p>{row.name}</p>
          )}
        </>
      )
    },
    {
      label: "项目版本",
      prop: "version",
      cellRenderer: ({ row, index }) => (
        <>
          {editMap.value[index]?.editable ? (
            <el-input v-model={row.version} />
          ) : (
            <p>{row.version}</p>
          )}
        </>
      )
    },
    {
      label: "区域数量",
      prop: "locations",
      cellRenderer: ({ row, index }) => (
        <p>{row.locations.length}</p>
      )
    },
    {
      label: "人员数量",
      prop: "persons",
      cellRenderer: ({ row, index }) => (
        <p>{row.persons.length}</p>
      )
    },
    {
      label: "项目状态",
      prop: "status",
      cellRenderer: ({ row, index }) => (
        <el-tag type={options.value.filter(opt => opt.value == row.status)[0]?.type}>
          {options.value.filter(opt => opt.value == row.status)[0]?.label}
        </el-tag>
      )
    },
    {
      label: "操作",
      fixed: "right",
      slot: "operation"
    }
  ];

  /** 分页配置 */
  const pagination = reactive<PaginationProps>({
    pageSize: 20,
    currentPage: 1,
    pageSizes: [20, 40, 60],
    total: 0,
    align: "right",
    background: true,
    size: "default"
  });

  /** 加载动画配置 */
  const loadingConfig = reactive<LoadingConfig>({
    text: "正在加载第一页...",
    viewBox: "-10, -10, 50, 50",
    spinner: `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
      `
    // svg: "",
    // background: rgba()
  });

  /** 撑满内容区自适应高度相关配置 */
  const adaptiveConfig: AdaptiveConfig = {
    /** 表格距离页面底部的偏移量，默认值为 `96` */
    offsetBottom: 110
    /** 是否固定表头，默认值为 `true`（如果不想固定表头，fixHeader设置为false并且表格要设置table-layout="auto"） */
    // fixHeader: true
    /** 页面 `resize` 时的防抖时间，默认值为 `60` ms */
    // timeout: 60
    /** 表头的 `z-index`，默认值为 `100` */
    // zIndex: 100
  };

  function onSizeChange(val) {
    console.log("onSizeChange", val);
  }

  function onCurrentChange(val) {
    loadingConfig.text = `正在加载第${val}页...`;
    loading.value = true;
    delay(600).then(() => {
      loading.value = false;
    });
  }

  function onEdit(row, index) {
    editMap.value[index] = Object.assign({ ...row, editable: true });
  }

  function onSave(index) {
    editMap.value[index].editable = false;
  }

  function onCancel(index) {
    editMap.value[index].editable = false;
    dataList.value[index] = delObjectProperty(editMap.value[index], "editable");
  }

  function onAdd() {
    const v = new Project();
    v.id = dataList.value.length + 1;
    dataList.value.push(v);
  }

  function onDel(row) {
    const index = dataList.value.indexOf(row);
    if (index !== -1) dataList.value.splice(index, 1);
  }

  return {
    loading,
    editMap,
    columns,
    dataList,
    options,
    pagination,
    loadingConfig,
    adaptiveConfig,
    onAdd,
    onDel,
    onEdit,
    onSave,
    onCancel,
    onSizeChange,
    onCurrentChange
  };
}
