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

import IHP.ViewPrelude
import IHP.IDE.SchemaDesigner.Types
import IHP.IDE.ToolServer.Types
import IHP.IDE.SchemaDesigner.View.Layout

data NewForeignKeyView = NewForeignKeyView
    { NewForeignKeyView -> [Statement]
statements :: [Statement]
    , NewForeignKeyView -> Text
tableName :: Text
    , NewForeignKeyView -> Text
columnName :: Text
    , NewForeignKeyView -> [Text]
tableNames :: [Text]
    }

instance View NewForeignKeyView where
    html :: NewForeignKeyView -> Html
html NewForeignKeyView { [Text]
[Statement]
Text
tableNames :: [Text]
columnName :: Text
tableName :: Text
statements :: [Statement]
$sel:tableNames:NewForeignKeyView :: NewForeignKeyView -> [Text]
$sel:columnName:NewForeignKeyView :: NewForeignKeyView -> Text
$sel:tableName:NewForeignKeyView :: NewForeignKeyView -> Text
$sel:statements:NewForeignKeyView :: NewForeignKeyView -> [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|
                <form method="POST" action={CreateForeignKeyAction}>
                    <input type="hidden" name="tableName" value={tableName}/>
                    <input type="hidden" name="columnName" value={columnName}/>

                    <div class="form-group row">
                        <label class="col-sm-2 col-form-label">Reference Table:</label>
                        <div class="col-sm-10">
                            <select name="referenceTable" class="form-control select2" autofocus="autofocus">
                                {forEach tableNames renderTableNameSelector}
                            </select>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label class="col-sm-2 col-form-label">Name:</label>
                        <div class="col-sm-10">
                            <input name="constraintName" type="text" class="form-control" value={tableName <> "_ref_" <> columnName}/>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label class="col-sm-2 col-form-label">On Delete:</label>
                        <div class="col-sm-10">
                            <select name="onDelete" class="form-control select2">
                                {onDeleteSelector "NoAction"}
                                {onDeleteSelector "Restrict"}
                                {onDeleteSelector "SetNull"}
                                {onDeleteSelector "SetDefault"}
                                {onDeleteSelector "Cascade"}
                            </select>
                        </div>
                    </div>

                    <div class="text-right">
                        <button type="submit" class="btn btn-primary">Add Constraint</button>
                    </div>
                </form>
                {select2}
            |]
                where
                    onDeleteSelector :: a -> Html
onDeleteSelector a
option = if a
option a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
"NoAction"
                        then String -> Html
forall a. ToMarkup a => a -> Html
preEscapedToHtml [plain|<option selected>#{option}</option>|]
                        else String -> Html
forall a. ToMarkup a => a -> Html
preEscapedToHtml [plain|<option>#{option}</option>|]
                    renderTableNameSelector :: a -> Html
renderTableNameSelector a
tableName = [hsx|<option>{tableName}</option>|]
                    select2 :: Html
select2 = String -> Html
forall a. ToMarkup a => a -> Html
preEscapedToHtml [plain|
                        <script>
                            $('.select2').select2();
                        </script>
                    |]
            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 Foreign Key Constraint"
            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 }