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 { BaseColumn } from '/@/views/adManage/utils/commonTabColumn.js';
import { parseQueryParams } from '/@/views/adManage/utils/tools.js'
import XEUtils from 'xe-utils';

export const createCrudOptions = function ({ crudExpose, context }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: UserPageQuery) => {
		const params = parseQueryParams(context.value)
		XEUtils.assign(query, params)
		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: 750,
				headerCellStyle: {
					backgroundColor: '#f6f7fa', // 直接设置背景颜色
					height: '20px',
					// border: '0.5px solid #ddd',
				},
				cellStyle: {
					border: 'none',
					borderBottom: '0.5px solid #ddd',
				},
			},
			container: {
        fixedHeight: false
      },
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			toolbar: {
        buttons: {
					search: {
						show: true
					},
					compact: {
						show: false
					}
				}
			},
			rowHandle: {
				fixed: 'right',
				width: 80,
				buttons: {
					view: {
						show: false,
					},
					edit: {
						iconRight: 'Edit',
						type: 'text',
            text: null
						// show: hasPermissions('dictionary:Update'),
					},
					remove: {
						iconRight: 'Delete',
						type: 'text',
            text: null
						// show: hasPermissions('dictionary:Delete'),
					},
				},
			},
			columns: {
        adGroupName: {
          title: '广告组名称',
          column: {
            width: '150px'
          },
					search: {
						show: true,
						component: {
							props: {
								clearable: true
							}
						}
					},
					form: {
						rules: [{required: true, message:'必填项'}]
					}
        },
				creative_type: {
					title: '投放类型',
					type: 'dict-select',
					column: {
						align: 'center',
						width: '130px'
					},
					dict: dict({
						data: [
							{ label: '商品集', value: 'PRODUCT_COLLECTION' },
							{ label: '品牌视频', value: 'BRAND_VIDEO' },
							{ label: '视频', value: 'VIDEO' }
						]
					})
				},
				state: {
					title: '状态',
					column: {
						width: 100
					}
				},
				...BaseColumn
			}
		}
	}
}
