import React from 'react'
import { useForm, usePage } from '@inertiajs/react'
import StudioLayout from '../../Layouts/StudioLayout'

export default function StudioGroupChallengeEditor() {
  const { props } = usePage()
  const challenge = props.challenge || null
  const form = useForm({
    title: challenge?.title || '',
    summary: challenge?.summary || '',
    description: challenge?.description || '',
    visibility: challenge?.visibility || props.visibilityOptions?.[0]?.value || 'public',
    participation_scope: challenge?.participation_scope || props.participationScopeOptions?.[0]?.value || 'group_only',
    status: challenge?.status || props.statusOptions?.[0]?.value || 'draft',
    start_at: challenge?.start_at ? challenge.start_at.slice(0, 16) : '',
    end_at: challenge?.end_at ? challenge.end_at.slice(0, 16) : '',
    rules_text: challenge?.rules_text || '',
    submission_instructions: challenge?.submission_instructions || '',
    judging_mode: challenge?.judging_mode || '',
    linked_collection_id: challenge?.linked_collection?.id || '',
    linked_project_id: challenge?.linked_project?.id || '',
    featured_artwork_id: challenge?.featured_artwork?.id || '',
    cover_file: null,
  })
  const attachForm = useForm({ artwork_id: '' })

  const submit = (event) => {
    event.preventDefault()
    const options = { forceFormData: true, preserveScroll: true }
    if (props.updateUrl) {
      form.post(props.updateUrl, { ...options, _method: 'patch' })
      return
    }
    form.post(props.storeUrl, options)
  }

  return (
    <StudioLayout title={props.title} subtitle={props.description}>
      <div className="grid gap-6 xl:grid-cols-[minmax(0,1fr)_360px]">
        <form onSubmit={submit} className="rounded-[28px] border border-white/10 bg-white/[0.03] p-6">
          <div className="grid gap-4">
            <input value={form.data.title} onChange={(event) => form.setData('title', event.target.value)} placeholder="Challenge title" className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
            <textarea value={form.data.summary} onChange={(event) => form.setData('summary', event.target.value)} placeholder="Short summary" rows={3} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
            <textarea value={form.data.description} onChange={(event) => form.setData('description', event.target.value)} placeholder="Challenge description" rows={8} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
            <div className="grid gap-4 md:grid-cols-3">
              <select value={form.data.visibility} onChange={(event) => form.setData('visibility', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">{(props.visibilityOptions || []).map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}</select>
              <select value={form.data.participation_scope} onChange={(event) => form.setData('participation_scope', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">{(props.participationScopeOptions || []).map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}</select>
              <select value={form.data.status} onChange={(event) => form.setData('status', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">{(props.statusOptions || []).map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}</select>
            </div>
            <div className="grid gap-4 md:grid-cols-2">
              <input type="datetime-local" value={form.data.start_at} onChange={(event) => form.setData('start_at', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
              <input type="datetime-local" value={form.data.end_at} onChange={(event) => form.setData('end_at', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
            </div>
            <textarea value={form.data.rules_text} onChange={(event) => form.setData('rules_text', event.target.value)} placeholder="Rules" rows={4} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
            <textarea value={form.data.submission_instructions} onChange={(event) => form.setData('submission_instructions', event.target.value)} placeholder="Submission instructions" rows={4} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
            <select value={form.data.judging_mode} onChange={(event) => form.setData('judging_mode', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">
              <option value="">No judging mode</option>
              {(props.judgingModeOptions || []).map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}
            </select>
            <div className="grid gap-4 md:grid-cols-3">
              <select value={form.data.linked_collection_id} onChange={(event) => form.setData('linked_collection_id', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">
                <option value="">No linked collection</option>
                {(props.collectionOptions || []).map((option) => <option key={option.id} value={option.id}>{option.title}</option>)}
              </select>
              <select value={form.data.linked_project_id} onChange={(event) => form.setData('linked_project_id', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">
                <option value="">No linked project</option>
                {(props.projectOptions || []).map((option) => <option key={option.id} value={option.id}>{option.title}</option>)}
              </select>
              <select value={form.data.featured_artwork_id} onChange={(event) => form.setData('featured_artwork_id', event.target.value)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">
                <option value="">No featured artwork</option>
                {(props.artworkOptions || []).map((option) => <option key={option.id} value={option.id}>{option.title}</option>)}
              </select>
            </div>
            <input type="file" accept="image/*" onChange={(event) => form.setData('cover_file', event.target.files?.[0] || null)} className="rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none" />
          </div>
          <button type="submit" className="mt-6 rounded-full border border-white/10 bg-white/[0.05] px-5 py-2.5 text-sm font-semibold text-white">Save challenge</button>
        </form>

        <div className="space-y-6">
          {props.publishUrl ? <form onSubmit={(event) => { event.preventDefault(); form.post(props.publishUrl, { preserveScroll: true }) }} className="rounded-[28px] border border-white/10 bg-white/[0.03] p-6"><button type="submit" className="rounded-full border border-white/10 bg-white/[0.05] px-5 py-2.5 text-sm font-semibold text-white">Publish challenge</button></form> : null}
          {props.attachArtworkUrl ? (
            <form onSubmit={(event) => { event.preventDefault(); attachForm.post(props.attachArtworkUrl, { preserveScroll: true }) }} className="rounded-[28px] border border-white/10 bg-white/[0.03] p-6">
              <h2 className="text-lg font-semibold text-white">Attach artwork</h2>
              <select value={attachForm.data.artwork_id} onChange={(event) => attachForm.setData('artwork_id', event.target.value)} className="mt-4 w-full rounded-2xl border border-white/10 bg-black/20 px-4 py-3 text-white outline-none">
                <option value="">Choose artwork</option>
                {(props.artworkOptions || []).map((option) => <option key={option.id} value={option.id}>{option.title}</option>)}
              </select>
              <button type="submit" className="mt-4 rounded-full border border-white/10 bg-white/[0.05] px-4 py-2 text-sm font-semibold text-white">Attach</button>
            </form>
          ) : null}
        </div>
      </div>
    </StudioLayout>
  )
}