import * as api from './api'
import {AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, dict, EditReq, UserPageQuery} from '@fast-crud/fast-crud'
import {inject} from 'vue'
import {SbBaseColumn} from '/@/views/adManage/utils/commonTabColumn.js'
import {parseQueryParams} from '/@/views/adManage/utils/tools.js'
import XEUtils from 'xe-utils'
import { useRouter } from 'vue-router'

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 router = useRouter()

  //权限判定
  const hasPermissions = inject('$hasPermissions')

  return {
    crudOptions: {
      table: {
        height: 800,
        headerCellStyle: {
          backgroundColor: '#f6f7fa', // 直接设置背景颜色
          height: '20px',
          borderRight: 'none'
        },
        cellStyle: {
          border: 'none',
          borderBottom: '0.5px solid #ddd',
        },
        stripe: false,
        showSummary: true,
      },
      container: {
        fixedHeight: false
      },
      // actionbar: {
      //   show: true,
      //   color: "#626aef",
      //   buttons: {
      //     add: {
      //       show: false
      //     },
      //     create: {
      //       text: '新建广告活动',
      //       // type: 'primary',
      //       color: "#626aef",
      //       plain: true,
      //       show: true,
      //       click() {

      //       }
      //     },
      //   }
      // },
      actionbar: {
        show: true,
        buttons: {
          add: {
            show: false,
          },
          create: {
            text: '新建广告活动',
            type: 'primary',
            show: true,
            color: "#626aef",
            plain: true,
            click() {
              router.push({
                name: 'SbCreateCampaigns',
                query: { campaignId: 123, tagsViewName: '新建广告活动' },
              })
            
            },
          },
        },
      },
      search: {
        show: false
      },
      toolbar: {
        buttons: {
          search: {
            show: true
          },
          compact: {
            show: false
          }
        }
      },
      request: {
        pageRequest,
        addRequest,
        editRequest,
        delRequest,
      },
      rowHandle: {
        fixed: 'right',
        width: 100,
        align: 'center',
        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: '广告活动名称',
          sortable: true,
          column: {
            fixed: 'left',
            width: 180,
            // showOverflowTooltip: true,
          },
        },
        state: {
          title: '状态',
          sortable: true,
          column: {
            width: '90px',
            align: 'center'
          },
          type: 'dict-select',
          search: {
            show: true
          },
          dict: dict({
            data: [
              {value: 'PAUSED', label: '已暂停', color: 'warning'},
              {value: 'ENABLED', label: '投放中', color: 'success'},
            ]
          })
        },
        bidOptimization: {
          title: '竞价',
          form: {
            show: false,
          },
          column: {
            width: '100px',
            sortable: true,
            align: 'center',
            formatter: (row) => {
              switch (row.value) {
                case 1:
                  return '自动竞价'
                case 0:
                  return '自定义竞价'
                default:
                  return '-'
              }
            }
          },
        },
        startDate: {
          title: '开始日期',
          column: {
            width: '120px',
            align: 'center',
            sortable: true,
          },
        },
        endDate: {
          title: '结束日期',
          column: {
            width: '120px',
            align: 'center',
            sortable: true,
            formatter: (row) => {
              if (row.value !== null) {
                return row.value
              } else {
                return '--'
              }
            }
          },
        },
        portfolioName: {
          title: '广告组合',
          type: 'text',
          column: {
            width: '120px',
            align: 'center',
            formatter: (row) => {
              if (row.value !== null) {
                return row.value
              } else {
                return '--'
              }
            }
          }
        },
        dailyBudget: {
          title: '预算',
          column: {
            sortable: true,
            width: '120px',
            align: 'right',
          }
        },
        ...SbBaseColumn
      }
    }
  }
}
