import * as api from './api'
import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud'
import { inject, nextTick, ref } from 'vue'
import { TemplateType } from '../utils/enum'

export const createCrudOptions = function ({ crudExpose, context }: CreateCrudOptionsProps): CreateCrudOptionsRet {

  const { editTmpl } = context

  const pageRequest = async (query: UserPageQuery) => {
    return await api.GetList(query)
  }
  const editRequest = async ({ form, row }: EditReq) => {
    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 hasPermissions = inject('$hasPermissions')

  return {
    crudOptions: {
      table: {
        height: 800,
        size: 'small',
      },
      container: {
        fixedHeight: false,
      },
      request: {
        pageRequest,
        addRequest,
        editRequest,
        delRequest,
      },
      rowHandle: {
        fixed: 'right',
        width: 100,
        buttons: {
          view: {
            show: false,
          },
          edit: {
          	text: null,
            iconRight: 'Edit',
          	type: 'text',
          	// show: hasPermissions('dictionary:Update'),
            order: 1,
          	tooltip: {
          		placement: 'top',
          		content: '编辑',
          	},
            click: (ctx: any) => {
              editTmpl(ctx.row)
            }
          },
          custom: {
            text: null,
            iconRight: 'SetUp',
            type: 'text',
            order: 2,
            tooltip: {
          		placement: 'top',
          		content: '关联广告活动',
          	}
          },
          remove: {
            iconRight: 'Delete',
            type: 'text',
            text: null,
            order: 3,
            tooltip: {
          		placement: 'top',
          		content: '删除',
          	}
            // show: hasPermissions('dictionary:Delete'),
          },
        },
      },
      actionbar: {
        show: true,
        buttons: {
          add: {
            show: false,
          }
        }
      },
      columns: {
        id: {
          title: 'ID',
          column: {
            width: 100,
            align: 'center',
          },
        },
        name: {
          title: '模板名称',
          column: {
            width: '150px',
          },
          search: {
            show: false
          }
        },
        "rule.type": {
          title: '模板类型',
          type: 'dict-select',
          dict: dict({
            data: TemplateType
          }),
          search: {
            show: true,
            component: {
              props: {
                clearable: true,
              },
            },
          }
        },
        campaignNumber: {
          title: '广告活动数量'
        },
        creator_username: {
          title: '创建人'
        },
        modifier_username: {
          title: '修改人'
        }
      }
    }
  }
}
