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 { useCompute } from '@fast-crud/fast-crud'

export const createCrudOptions = function ({ crudExpose, context }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const { showDialog } = 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')
  const { compute } = useCompute()

  return {
    crudOptions: {
      table: {
        height: 800,
      },
      container: {
        fixedHeight: false,
      },
      actionbar: {
        show: true,
        buttons: {
          add: {
            text: 'xxx',
            show: false,
          },
          create: {
            text: '添加店铺',
            type: 'primary',
            show: true,
            click() {
							showDialog('ad')
            }
          },
        },
      },
      search: {
        show: false,
      },
      toolbar: {
        buttons: {
          search: {
            show: true,
          },
          compact: {
            show: false,
          },
        },
      },
      request: {
        pageRequest,
        addRequest,
        editRequest,
        delRequest,
      },
      rowHandle: {
        fixed: 'right',
        width: 80,
        buttons: {
          view: {
            show: false,
          },
          edit: {
            iconRight: 'Edit',
            type: 'text',
            text: null,
            show: false,
          },
          remove: {
            iconRight: 'Delete',
            type: 'text',
            text: null,
            show: false,
          },
          custom: {
            type: 'text',
            text: 'SP授权',
            disabled: compute((context: any) => {
              return context.row.ad_auth === 1 && context.row.sp_auth === 1
            }),
						click(context: any) {
							showDialog('sp', context.row)
						}
          },
        },
      },
      columns: {
        custom_shop_name: {
          title: '亚马逊店铺名称',
        },
        advertiser_id: {
          title: '店铺ID',
        },
        region: {
          title: '区域',
          type: 'dict-select',
          dict: dict({
            data: [
              { label: '北美洲', value: 1 },
              { label: '欧洲', value: 2 },
              { label: '亚洲', value: 3 },
            ],
          }),
        },
        country_code: {
          title: '站点',
        },
        type: {
          title: '店铺类型',
        },
        ad_auth: {
          title: '广告API授权',
          column: {
            cellRender(scope) {
              if (scope.value === 1) return <span style="color: green">已授权</span>
              return <span style="color: red">未授权</span>
            },
          },
        },
        sp_auth: {
          title: 'SP-API授权',
          column: {
            cellRender(scope) {
              if (scope.value === 1) return <span style="color: green">已授权</span>
              return <span style="color: red">未授权</span>
            },
          },
        },
        // creator: {
        // 	title: '授权人'
        // },
        // email: {
        // 	title: '授权人邮箱'
        // }
      },
    },
  }
}
