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 Anchor from "@/model/anchor.js";

export function useColumns() {
  const loading = ref(true);
  const editMap = ref({});
  const dataList = ref([]);
  const options = ref([]);
  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: "IMEI",
      prop: "imei",
      cellRenderer: ({ row, index }) => (
        <>
          {editMap.value[index]?.editable ? (
            <el-input v-model={row.imei} />
          ) : (
            <p>{row.imei}</p>
          )}
        </>
      )
    },
    {
      label: "ICCID",
      prop: "iccid",
      cellRenderer: ({ row, index }) => (
        <>
          {editMap.value[index]?.editable ? (
            <el-input v-model={row.iccid} />
          ) : (
            <p>{row.iccid}</p>
          )}
        </>
      )
    },
    {
      label: "项目ID",
      prop: "projId",
      cellRenderer: ({ row, index }) => (
        <>
          {editMap.value[index]?.editable ? (
            <el-select v-model={row.projId} clearable placeholder="请选择爱好">
              {options.value.map(item => {
                return (
                  <el-option
                    key={item.value}
                    label={item.label}
                    value={item.value}
                  />
                );
              })}
            </el-select>
          ) : (
            <el-tag type="primary">
              {options.value.filter(opt => opt.value == row.projId)[0]?.label}
            </el-tag>
          )}
        </>
      )
    },
    {
      label: "电池",
      prop: "battery",
      cellRenderer: ({ row, index }) => (
        <p>{row.battery}</p>
      )
    },
    {
      label: "状态",
      prop: "status",
      cellRenderer: ({ row, index }) => (
        <p>{row.status}</p>
      )
    },
    {
      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 anchor = new Anchor();
    anchor.id = dataList.value.length + 1;
    dataList.value.push(anchor);
  }

  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
  };
}
