import * as api from './api'
import {AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, dict, EditReq, UserPageQuery} from '@fast-crud/fast-crud'
import {inject} 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: 800
      },
      container: {
        fixedHeight: false
      },
      actionbar: {
        show: true,
        buttons: {
          add: {
            show: false
          },
        }
      },
      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: {
            iconRight: 'Delete',
            type: 'text',
            text: null
            // show: hasPermissions('dictionary:Delete'),
          },
        },
      },
      columns: {
        id: {
          title: 'ID',
          column: {
            show: false
          },
          form: {
            show: false
          }
        },
        resolvedExpression_value: {title: '商品和分类'},
        state: {
          title: '状态',
          column: {
            width: '90px',
            align: 'center'
          },
          type: 'dict-select',
          search: {
            show: true
          },
          dict: dict({
            data: [
              {value: 'PAUSED', label: '已暂停', color: 'warning'},
              {value: 'ENABLED', label: '投放中', color: 'success'},
            ]
          })
        },
        campaignName: {
          title: '广告活动名称',
          column: {
            width: '200px',
            align: 'center',
            fixed: 'left'
          },
          search: {
            show: true,
            component: {
              props: {
                clearable: true
              }
            }
          },
          form: {
            rules: [{required: true, message: '必填项'}]
          }
        },
        adGroupName: {
          title: '广告组名称',
          column: {
            align: 'center',
          },
        },
        suggestedBudget: {
          title: '建议竞价',
          column: {
            align: 'center',
          },
        },
        bid: {
          title: '出价',
          column: {
            align: 'center',
          },
        },
        '标签': {},
        Impression: {
          title: '曝光量',
          column: {
            align: 'center',
          },
        },
        '搜索结果顶部展示份额': {},
        Click: {
          title: '点击量',
          column: {
            align: 'center',
          },
        },
        CTR: {title: '点击率'},
        Spend: {title: '花费'},
        CPC: {title: '点击成本'},
        TotalPurchases: {title: '订单数'},
        TotalSales: {title: '销售额'},
        TotalUnitOrdered: {title: '销量'},
        PurchasesRate: {title: '转化率'},
        ACOS: {title: 'ACOS'},
        ROAS: {title: 'ROAS'},
        CPA: {title: '订单成本'},
        ...BaseColumn
      }
    }
  }
}
