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: 'left',
          },
          search: {
            show: true,
            component: {
              props: {
                clearable: true
              }
            }
          },
          form: {
            rules: [{required: true, message: '必填项'}]
          }
        },
        placement: {
          title: '广告位',
          column: {
            align: 'left',
            width: '160px',
            fixed: 'left',
            formatter: (row) => {
              switch (row.value) {
                case 'Other on-Amazon':
                  return '其他页面'
                case 'Product pages':
                  return '产品页面'
                case 'Top of search (first page)':
                  return '首页'
                case 'Detail Page on-Amazon':
                  return '详细信息首页'
                case 'Other Placements':
                  return '其他位置'
                default:
                  return '--'
              }
            }
          }
        },
        bidOptimization: {
          title: '竞价策略',
          column: {
            align: 'center',
            width: '160px',
            formatter: (row) => {
              switch (row.value) {
                case 1:
                  return '自动竞价'
                case 0:
                  return '自定义竞价'
                default:
                  return '--'
              }
            }
          }
        },
        bidAdjustment: {
          title: '竞价调整',
          column: {
            align: 'right',
            width: '100px',
            formatter: (row) => {
              console.log(row.value)
              if (row.value != null) {
                return row.value
              } else {
                return '--'
              }
            }
          }
        },

        ...BaseColumn
      }
    }
  }
}
