module LoggedIn.Home.View.Table exposing ( view ) import Date exposing (Date) import Dict exposing (..) import String exposing (append) import FontAwesome import View.Color as Color import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import Dialog import Dialog.AddPayment.Model as AddPayment import Dialog.AddPayment.View as AddPayment import Tooltip import Msg exposing (Msg) import LoggedData exposing (LoggedData) import LoggedIn.Msg as LoggedInMsg import LoggedIn.Home.Model as Home import LoggedIn.View.Format as Format import View.Date as Date import Model.Payment as Payment exposing (..) import Model.PaymentCategory as PaymentCategory import Model.Translations exposing (getMessage) import Model.User exposing (getUserName) view : LoggedData -> Home.Model -> Payments -> Frequency -> Html Msg view loggedData homeModel payments frequency = let visiblePayments = payments |> List.drop ((homeModel.currentPage - 1) * perPage) |> List.take perPage in div [ class "table" ] [ div [ class "lines" ] ( headerLine loggedData frequency :: List.map (paymentLine loggedData homeModel frequency) visiblePayments ) , if List.isEmpty visiblePayments then div [ class "emptyTableMsg" ] [ text <| getMessage loggedData.translations "NoPayment" ] else text "" ] headerLine : LoggedData -> Frequency -> Html Msg headerLine loggedData frequency = div [ class "header" ] [ div [ class "cell category" ] [ text <| getMessage loggedData.translations "Name" ] , div [ class "cell cost" ] [ text <| getMessage loggedData.translations "Cost" ] , div [ class "cell user" ] [ text <| getMessage loggedData.translations "Payer" ] , div [ class "cell user" ] [ text <| getMessage loggedData.translations "PaymentCategory" ] , case frequency of Punctual -> div [ class "cell date" ] [ text <| getMessage loggedData.translations "Date" ] Monthly -> text "" , div [ class "cell" ] [] , div [ class "cell" ] [] , div [ class "cell" ] [] ] paymentLine : LoggedData -> Home.Model -> Frequency -> Payment -> Html Msg paymentLine loggedData homeModel frequency payment = div [ class "row" ] [ div [ class "cell name" ] [ text payment.name ] , div [ classList [ ("cell cost", True) , ("refund", payment.cost < 0) ] ] [ text (Format.price loggedData.conf payment.cost) ] , div [ class "cell user" ] [ payment.userId |> getUserName loggedData.users |> Maybe.withDefault "−" |> text ] , div [ class "cell category" ] ( let mbCategory = PaymentCategory.search payment.name loggedData.paymentCategories |> Maybe.andThen (\category -> Dict.get category loggedData.categories) in case mbCategory of Just category -> [ span [ class "tag" , style [("background-color", category.color)] ] [ text category.name ] ] Nothing -> [] ) , case frequency of Punctual -> div [ class "cell date" ] [ span [ class "shortDate" ] [ text (Date.shortView payment.date loggedData.translations) ] , span [ class "longDate" ] [ text (Date.longView payment.date loggedData.translations) ] ] Monthly -> text "" , div [ class "cell button" ] [ let currentDate = Date.fromTime loggedData.currentTime category = PaymentCategory.search payment.name loggedData.paymentCategories in AddPayment.button loggedData (AddPayment.initialClone loggedData.translations currentDate category payment) "ClonePayment" (FontAwesome.clone Color.chestnutRose 18) (Just (getMessage loggedData.translations "Clone")) ] , div [ class "cell button" ] [ if loggedData.me /= payment.userId then text "" else let category = PaymentCategory.search payment.name loggedData.paymentCategories in AddPayment.button loggedData (AddPayment.initialEdit loggedData.translations category payment) "EditPayment" (FontAwesome.pencil Color.chestnutRose 18) (Just (getMessage loggedData.translations "Edit")) ] , div [ class "cell button" ] [ if loggedData.me /= payment.userId then text "" else let dialogConfig = { className = "deletePaymentDialog" , title = getMessage loggedData.translations "ConfirmPaymentDelete" , body = always <| text "" , confirm = getMessage loggedData.translations "Confirm" , confirmMsg = always <| Msg.Dialog <| Dialog.UpdateAndClose <| Msg.DeletePayment payment.id , undo = getMessage loggedData.translations "Undo" } in button ( Tooltip.show Msg.Tooltip (getMessage loggedData.translations "Delete") ++ [ onClick (Msg.Dialog <| Dialog.Open dialogConfig) ] ) [ FontAwesome.trash Color.chestnutRose 18 ] ] ]