module IHP.IDE.SchemaDesigner.View.Columns.New where

import IHP.ViewPrelude
import IHP.IDE.SchemaDesigner.Types
import IHP.IDE.ToolServer.Types
import IHP.IDE.ToolServer.Layout
import IHP.IDE.SchemaDesigner.View.Layout
import IHP.IDE.SchemaDesigner.View.Columns.Edit (typeSelector)

data NewColumnView = NewColumnView
    { NewColumnView -> [Statement]
statements :: [Statement]
    , NewColumnView -> Text
tableName :: Text
    , NewColumnView -> [Text]
tableNames :: [Text]
    , NewColumnView -> [Text]
enumNames :: [Text]
    }

instance View NewColumnView where
    html :: NewColumnView -> Html
html NewColumnView { [Text]
[Statement]
Text
enumNames :: [Text]
tableNames :: [Text]
tableName :: Text
statements :: [Statement]
$sel:enumNames:NewColumnView :: NewColumnView -> [Text]
$sel:tableNames:NewColumnView :: NewColumnView -> [Text]
$sel:tableName:NewColumnView :: NewColumnView -> Text
$sel:statements:NewColumnView :: NewColumnView -> [Statement]
.. } = [hsx|
        <div class="row no-gutters bg-white" id="schema-designer-viewer">
            {renderObjectSelector (zip [0..] statements) (Just tableName)}
            {renderColumnSelector tableName  (zip [0..] columns) statements}
        </div>
        {migrationStatus}
        {renderModal modal}
    |]
        where
            table :: Maybe Statement
table = Text -> [Statement] -> Maybe Statement
forall (t :: * -> *).
Foldable t =>
Text -> t Statement -> Maybe Statement
findStatementByName Text
tableName [Statement]
statements
            columns :: [Column]
columns = [Column] -> (Statement -> [Column]) -> Maybe Statement -> [Column]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (Proxy "columns" -> CreateTable -> [Column]
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "columns" (Proxy "columns")
Proxy "columns"
#columns (CreateTable -> [Column])
-> (Statement -> CreateTable) -> Statement -> [Column]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Statement -> CreateTable
unsafeGetCreateTable) Maybe Statement
table

            modalContent :: Html
modalContent = [hsx|
                {renderFlashMessages}
                <form method="POST" action={CreateColumnAction} id="new-column">
                    <input type="hidden" name="tableName" value={tableName}/>

                    <div class="form-group">
                        <input
                            id="colName"
                            name="name"
                            type="text"
                            class="form-control"
                            autofocus="autofocus"
                            placeholder="Name"
                            data-table-name-singular={singularize tableName}
                            />
                    </div>

                    <div class="form-group">
                        {typeSelector Nothing enumNames}

                        <div class="mt-1 text-muted d-flex" id="column-options">
                            {generateReferenceCheckboxes}

                            <div class="custom-control custom-checkbox">
                                <input id="allowNull" type="checkbox" name="allowNull" class="mr-1 custom-control-input"/>
                                <label class="custom-control-label mr-2" for="allowNull">
                                    Nullable
                                </label>
                            </div>

                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" id="isUnique" name="isUnique" class="mr-1 custom-control-input"/>
                                <label class="mx-2 custom-control-label" for="isUnique">
                                    Unique
                                </label>
                            </div>

                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" name="primaryKey" id="primaryKey" class="mr-1 custom-control-input"/>
                                <label class="mx-2 custom-control-label" for="primaryKey">
                                    Primary Key
                                </label>
                            </div>

                            <div class="custom-control custom-checkbox">
                                <input id="isArray" type="checkbox" name="isArray" class="mr-1 custom-control-input"/>
                                <label class="mx-2 custom-control-label" for="isArray">
                                    Array Type
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        {defaultSelector}
                    </div>

                    <div class="text-right">
                        <button type="submit" class="btn btn-primary">Create Column</button>
                    </div>

                    <input type="hidden" name="primaryKey" value={inputValue False}/>
                    <input type="hidden" name="allowNull" value={inputValue False}/>
                    <input type="hidden" name="isUnique" value={inputValue False}/>
                    <input type="hidden" name="isArray" value={inputValue False}/>
                    <input type="hidden" name="isReference" value={inputValue False}/>
                    <input type="hidden" name="referenceTable" value=""/>
                </form>
            |]
                where
                    generateReferenceCheckboxes :: Html
generateReferenceCheckboxes = [hsx|{forEach tableNames checkbox}|]
                        where checkbox :: Text -> Html
checkbox Text
tableName = [hsx|
                                <div class="custom-control custom-checkbox" style="display: none;" data-attribute={(singularize tableName) <> "_id"} data-table={tableName} >
                                    <input id="reference" type="checkbox" name="isReference" class="mr-1 custom-control-input"/>
                                    <label class="mx-2 ref custom-control-label" id="refText">
                                        References {tableName}
                                    </label>
                                </div>
                                |]
                    defaultSelector :: Html
defaultSelector = [hsx|
                        <select id="defaultSelector" name="defaultValue" class="form-control select2">
                            <option value="" selected={True}>no default</option>
                            <option value="''">""</option>
                        </select>
                    |]
            modalFooter :: Maybe Html
modalFooter = Maybe Html
forall a. Monoid a => a
mempty
            modalCloseUrl :: Text
modalCloseUrl = TablesController -> Text
forall controller. HasPath controller => controller -> Text
pathTo ShowTableAction :: Text -> TablesController
ShowTableAction { Text
$sel:tableName:TablesAction :: Text
tableName :: Text
tableName }
            modalTitle :: Text
modalTitle = Text
"New Column"
            modal :: Modal
modal = Modal :: Html -> Maybe Html -> Text -> Text -> Modal
Modal { Html
$sel:modalContent:Modal :: Html
modalContent :: Html
modalContent, Maybe Html
$sel:modalFooter:Modal :: Maybe Html
modalFooter :: Maybe Html
modalFooter, Text
$sel:modalCloseUrl:Modal :: Text
modalCloseUrl :: Text
modalCloseUrl, Text
$sel:modalTitle:Modal :: Text
modalTitle :: Text
modalTitle }