import { polishScript, saveScript, taskinfo } from "../../../utils/request"

type PolishStyle = {
  id: string
  name: string
  icon: string
  activeIcon: string
}

const styles: PolishStyle[] = [
  { id: 'natural', name: '自然口播', icon: '/static/images/Frame-15355646363.png', activeIcon: '/static/images/Frame-1561268387.png' },
  { id: 'goods', name: '带货风格', icon: '/static/images/Frame-1561268385.png', activeIcon: '/static/images/Frame-1561268389.png' },
  { id: 'emotion', name: '情感风格', icon: '/static/images/Frame-1561268384.png', activeIcon: '/static/images/Frame-1561268388.png' },
  { id: 'teach', name: '专业讲解', icon: '/static/images/Frame-1561268383.png', activeIcon: '/static/images/Frame-1561268390.png' },
  { id: 'viral', name: '爆款短视频', icon: '/static/images/Frame-1561268382.png', activeIcon: '/static/images/Frame-1561268386.png' },
]

const styleMap: Record<string, string> = {
  natural: 'friendly',
  goods: 'product',
  emotion: 'rewrite',
  teach: 'professional',
  viral: 'rewrite',
}

Page({
  data: {
    styles,
    selectedStyle: 'natural',
    taskinfo: null as any,
    inEdit: false,
  },

  onLoad(ops: any) {
    taskinfo(ops.taskId).then((res: any) => {
      if (!res.code) {
        this.setData({ taskinfo: res.data })
      } else {
        wx.showModal({ title: '温馨提示', content: res.message || '获取文案失败', showCancel: false })
      }
    })
  },

  handleBack() {
    wx.navigateBack({
      fail: () => wx.switchTab({ url: '/pages/index/index' }),
    })
  },

  handleStyleSelect(e: WechatMiniprogram.TouchEvent) {
    const selectedStyle = e.currentTarget.dataset.id
    if (!selectedStyle) return

    this.setData({ selectedStyle })
  },

  handleEdit() {
    if (!this.data.taskinfo) return

    this.setData({ inEdit: !this.data.inEdit })
  },

  changeContent(e: any) {
    const taskinfo = this.data.taskinfo
    if (!taskinfo) return

    taskinfo.script_text = e.detail.value
    this.setData({ taskinfo })
  },

  handleRegenerate() {
    const taskinfo = this.data.taskinfo
    if (!taskinfo) return

    const content = taskinfo.script_text || taskinfo.original_script || ''
    if (!content) {
      wx.showToast({ title: '暂无可润色文案', icon: 'none' })
      return
    }

    wx.showLoading({ title: '润色中' })
    polishScript(taskinfo.task_no, content, styleMap[this.data.selectedStyle] || 'optimize').then((res: any) => {
      wx.hideLoading()
      if (res.code) {
        wx.showModal({ title: '温馨提示', content: res.message || '润色失败', showCancel: false })
        return
      }

      taskinfo.script_text = res.data.scriptText
      this.setData({ taskinfo, inEdit: false })
      wx.showToast({ title: '已重新润色', icon: 'success' })
    }).catch(() => {
      wx.hideLoading()
      wx.showModal({ title: '温馨提示', content: '网络异常，请稍后再试', showCancel: false })
    })
  },

  handleConfirm() {
    this.saveCurrentScript(true)
  },

  saveCurrentScript(next: boolean) {
    const taskinfo = this.data.taskinfo
    if (!taskinfo) return

    const content = (taskinfo.script_text || '').trim()
    if (!content) {
      wx.showToast({ title: '文案不能为空', icon: 'none' })
      return
    }

    wx.showLoading({ title: '保存中' })
    saveScript(taskinfo.task_no, content).then((res: any) => {
      wx.hideLoading()
      if (res.code) {
        wx.showModal({ title: '温馨提示', content: res.message || '保存失败', showCancel: false })
        return
      }

      taskinfo.script_text = res.data.scriptText
      this.setData({ taskinfo, inEdit: false })
      if (next) {
        wx.redirectTo({
          url: '/pages/aiPackage/record/index?taskid=' + taskinfo.task_no,
        })
      } else {
        wx.showToast({ title: '已保存', icon: 'success' })
      }
    }).catch(() => {
      wx.hideLoading()
      wx.showModal({ title: '温馨提示', content: '网络异常，请稍后再试', showCancel: false })
    })
  },
})



