import * as api from './api'
import {AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, 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'
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,
        headerCellStyle: {
          backgroundColor: '#f6f7fa', // 直接设置背景颜色
          height: '20px',
          borderRight: 'none',
        },
        cellStyle: {
          border: 'none',
          borderBottom: '0.5px solid #ddd',
        },
        showSummary: true,
        stripe: false
      },
      container: {
        fixedHeight: false
      },
      actionbar: {
        show: false,
        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
          }
        },
        campaignName: {
          title: '广告活动',
          column: {
            width: '210px',
            fixed: 'left',
            align: 'center',
          },
          search: {
            show: true,
            component: {
              props: {
                clearable: true
              }
            }
          },
          form: {
            rules: [{required: true, message: '必填项'}]
          }
        },
        placement: {
          title: '广告位',
          column: {
            align: 'center',
            width: '160px',
            formatter: (row) => {
              switch (row.value) {
                case 'top':
                  return '搜索结果顶部(首页)'
                case 'rest_of_search':
                  return '搜索结果的其余位置'
                case 'product_page':
                  return '商品页面'
                default:
                  return '暂无数据'
              }
            }
          }
        },
        dynBidStrategy: {
          title: '竞价策略',
          column: {
            align: 'center',
            width: '160px',
            formatter: (row) => {
              switch (row.value) {
                case 'MANUAL':
                  return '固定竞价'
                case 'AUTO_FOR_SALES':
                  return '动态竞价-提高和降低'
                case 'LEGACY_FOR_SALES':
                  return '动态竞价-仅降低'
                default:
                  return '暂无数据'
              }
            }
          }
        },
        bidAdjustment: {
          title: '竞价调整',
          column: {
            align: 'right',
            width: '100px',
            formatter: (row) => {
              if (row.value != null) {
                return row.value
              } else {
                return '--'
              }
            }
          }
        },

        ...BaseColumn
      }
    }
  }
}
