import { Form, Link } from '@inertiajs/react';
import Shell from '../layouts/Shell';

interface EditablePost {
    id: number;
    title: string;
    category: string;
    excerpt: string;
    body: string;
    published_at: string | null;
}

export default function Editor({ post }: { post: EditablePost | null }) {
    const heading = post ? 'Edit post' : 'New post';

    return (
        <Shell active="studio">
            <div className="wrap editor-page">
                <Link className="back-link" href="/studio">
                    <span className="arrow arrow-left" aria-hidden="true" /> Back to studio
                </Link>
                <h1>{heading}</h1>
                <Form
                    key={post?.id ?? 'new'}
                    action={post ? `/studio/posts/${post.id}` : '/studio/posts'}
                    method={post ? 'put' : 'post'}
                    transform={(data) => ({ ...data, published: data.published === 'on' })}
                >
                    {({ errors, processing }) => (
                        <div className="form-fields">
                            <label>
                                Title
                                <input name="title" required maxLength={160} defaultValue={post?.title} />
                                {errors.title && (
                                    <span className="field-error" role="alert">
                                        {errors.title}
                                    </span>
                                )}
                            </label>
                            <label>
                                Category
                                <input
                                    name="category"
                                    list="categories"
                                    required
                                    maxLength={40}
                                    defaultValue={post?.category}
                                />
                                <datalist id="categories">
                                    <option value="Nature" />
                                    <option value="Culture" />
                                    <option value="Reflections" />
                                    <option value="Field notes" />
                                </datalist>
                                {errors.category && (
                                    <span className="field-error" role="alert">
                                        {errors.category}
                                    </span>
                                )}
                            </label>
                            <label>
                                Excerpt
                                <textarea
                                    name="excerpt"
                                    required
                                    maxLength={320}
                                    rows={2}
                                    defaultValue={post?.excerpt}
                                />
                                {errors.excerpt && (
                                    <span className="field-error" role="alert">
                                        {errors.excerpt}
                                    </span>
                                )}
                            </label>
                            <label>
                                Post body
                                <textarea name="body" required maxLength={50000} rows={12} defaultValue={post?.body} />
                                {errors.body && (
                                    <span className="field-error" role="alert">
                                        {errors.body}
                                    </span>
                                )}
                            </label>
                            <label className="check-label">
                                <input name="published" type="checkbox" defaultChecked={Boolean(post?.published_at)} />{' '}
                                Published
                            </label>
                            {errors.published && (
                                <span className="field-error" role="alert">
                                    {errors.published}
                                </span>
                            )}
                            <p className="muted">
                                Published posts are visible in the public journal. Uncheck to save as a draft.
                            </p>
                            <div className="row-actions">
                                <button className="solid" disabled={processing}>
                                    {processing ? 'Saving…' : 'Save post'} <span className="arrow" aria-hidden="true" />
                                </button>
                                <Link href="/studio">Cancel</Link>
                            </div>
                        </div>
                    )}
                </Form>
            </div>
        </Shell>
    );
}
