import * as api from './api'
import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud'
import { inject } from 'vue'
import { BaseColumn } from '/@/views/adManage/utils/commonTabColumn.js'
import { dynBidStrategyEnum } from '/@/views/adManage/utils/enum.js'


export const createCrudOptions = function ({ crudExpose, context }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: UserPageQuery) => {
		query["profileId"] = context.profile.value.profile_id
		query["startDate"] = context.dateRange.value[0]
		query["endDate"] = context.dateRange.value[1]
		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
			},
			container: {
        fixedHeight: false
      },
			actionbar: {
				show: true,
				buttons: {
					add: {
						show: false
					},
					create: {
						text: '新建广告活动',
						type: "primary",
						show: true,
						click() {

						}
					}
				}
			},
			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: hasPermissions('dictionary:Update'),
					},
					remove: {
						show: false
						// iconRight: 'Delete',
						// type: 'text',
            // text: null
						// show: hasPermissions('dictionary:Delete'),
					},
				},
			},
			columns: {
				id: {
					title: 'ID',
					column: {
						show: false
					},
					form: {
						show: false
					}
				},
        campaignName: {
          title: '广告活动',
          column: {
            width: '200px',
						fixed: 'left'
          },
					search: {
						show: true,
						component: {
							props: {
								clearable: true
							}
						}
					},
					form: {
						rules: [{required: true, message:'必填项'}]
					}
        },
				targetingType: {
					title: '投放类型',
					type: 'dict-select',
					search: {
						show: true
					},
					dict: dict({
						data: [
							{ value: 'AUTO', label: '自动' },
							{ value: 'MANUAL', label: '手动' },
						]
					})
				},
				state: {
					title: '状态'
				},
				dynBidStrategy: {
					title: '竞价策略',
					form: {
						show: false,
					},
					column: {
						width: '160px'
					},
					type: 'dict-select',
					dict: dict({
						data: dynBidStrategyEnum
					})
				},
				startDate: {
					title: '开始日期',
					column: {
            width: '100px'
          },
				},
				endDate: {
					title: '结束日期',
					column: {
            width: '100px'
          },
				},
				budget: {
					title: '预算'
				},
				portfolioName: {
					title: '广告组合'
				},
				suggestedBudget: {
					title: '建议竞价',
					form: {
						show: false
					}
				},
				percentTimeInBudget: {
					title: '预算活跃均值',
					column:{
						minWidth: 150
					},
					form: {
						show: false
					}
				},
				MissedImpressions: {
					title: '预计错过的曝光',
					form: {
						show: false
					},
					column:{
						width: 180
					},
				},
				MissedClicks: {
					title: '预计错过的点击',
					form: {
						show: false
					},
					column:{
						width: 180
					},
				},
				MissedSales: {
					title: '预计错过的销售',
					form: {
						show: false
					},
					column:{
						width: 180
					},
				},
        ...BaseColumn
			}
		}
	}
}
