#!/usr/bin/perl5 -w
# 漢字コード EUC

#
use SIC;
use SICX::Commander;
use SICX::Commander::Auth;
use SICX::Util::File;
use Cycom::Util::Func;

my %MARGIN = ('mizuho'=>1, 'fvac'=>1, 'koshigaya'=>1 );

my $comm = SICX::Commander->new();

unless (&start($comm)) {
    $comm->error();
}

$comm->finish();

exit(0);

#
sub start {
    my $comm = shift;

    $comm->init_sess() or return 0;

    my $auth = SICX::Commander::Auth->new($comm);
    $auth->init();

    my $job = $comm->{cgi}->param("job") || "";
    $job eq "submit" or $job = "default";
    $job = sprintf("job_%s", $job);
    return &{$job}($comm, $auth);
}

sub render_page {
    my ($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid) = @_;

    print
        $comm->{http}->header();
#        $comm->{html}->header();

    $w_siteid = $def_siteid || $comm->{cgi}->param('siteid') || $siteid || '';
    my $header = &header($comm, $w_siteid);#'HEADER';
    my $footer = &footer($comm, $w_siteid);#'FOOTER';
#    my $header = &header($comm, $siteid);#'HEADER';
#    my $footer = &footer($comm, $siteid);#'FOOTER';

    $comm->auto_html_footer();

    my $ec_html = $ec ? "<p>ec: $ec</p>" : "";
    my $mem_sitepw_check = $mem_sitepw ? " checked" : "";
#$ec_html

    my $margin = '';
    unless ( $MARGIN{$w_siteid} ){
        $margin = qq{ style="margin: 0px"};
    }

    print <<EOF;
<html>
<head>
<title>ログイン</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<link rel="stylesheet" href="/css/default.css" type="text/css"/>
</head>
<body>
<div id="edit_wrapper">
$header
<CENTER>
<div align="left">
<table border="0" width="750" height="100%">
<tr><td align="CENTER">

<form action="$ENV{SCRIPT_URL}" method="POST" style="margin-top:1em;">
<input type="hidden" name="job" value="submit">
<input type="hidden" name="def_siteid" value="$def_siteid">

<table border="0" cellpadding="0" cellspacing="0">
<tr><td bgcolor="#666666">

<table border="0" width="100%" cellpadding="1" cellspacing="1">
<tr><td bgcolor="#ffffff">

<table border="0">
  <tr>
    <td height="25">サイトＩＤ:</td>
    <td><input type="text" name="siteid" value="$siteid"></td>
  </tr>
  <tr>
    <td height="25" nowrap>パスワード:</td>
    <td><input type="password" name="passwd" value="$passwd">　<input type="submit" value="ログイン"></td>
  </tr>
  <tr>
    <td colspan="2" height="25"><input type="checkbox" name="mem_sitepw"$mem_sitepw_check>ID とパスワードを記憶する</td>
  </tr>
</table>

</td></tr>
</table>

</td></tr>
</table>
</form>
</td></tr>
</table>
</div>
</CENTER>
$footer
</div><!-- /edit_wrapper -->
EOF
}

sub job_default {
    my ($comm, $auth) = @_;

    my ($siteid, $passwd) = ("", "");
    my $mem_sitepw = $auth->get_data("mem_sitepw") || 0;
    if ($mem_sitepw) {
        $siteid = $auth->get_data("siteid") || $comm->{cgi}->param('siteid') || '';
        if ($siteid) {
            $passwd = $auth->get_data("passwd") || "";
        }
    }
    my $def_siteid = $siteid || '';

    my $ec = $comm->{cgi}->param("ec") || "";

    &render_page($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid);

    return 1;
}

sub job_submit {
    my ($comm, $auth) = @_;

    my $cgi = $comm->{cgi};

    my $siteid = $cgi->param("siteid") || "";

#2014/04/07 hojo
    $comm->{site}->{siteid} = $siteid;
    undef $comm->{conn};
    $comm->conn();


    my $passwd = $cgi->param("passwd") || "";
    my $mem_sitepw = $cgi->param("mem_sitepw") ? 1 : 0;

    my $ec = "";
    if (is_valid_id($siteid) and is_valid_id($passwd)) {
        my $bool;

        unless (($bool) = $auth->by_site($siteid, $passwd)) {
            $comm->auto_html_footer();
            return 0;
        }

        if ($bool) {
            $auth->clear_login();
            $auth->set_data(
                siteid => $siteid,
                passwd => $passwd,
                mem_sitepw => $mem_sitepw
            );
            $auth->apply_data_to_sess();

            $comm->auto_save_sess();

            print
                $comm->{http}->redirect(
                    sprintf("%s?siteid=%s",
                        '/cgi-edit/sites/menu.cgi',
                        $siteid
                    )
                );

            return 1;
        }
        else {
            $ec = $auth->get_status("ec");
        }

    }
    else {
        $ec = "invalid_site_param";
    }

    my $def_siteid = $cgi->param("def_siteid") || "";
    &render_page($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid);

    return 1;
}

#
sub header {
    my $comm = shift;
    my $siteid = shift;

    my $html = include_html($comm,'header', siteid=>$siteid);

    return $html;
}

sub footer {
    my $comm = shift;
    my $siteid = shift;
    #
    my $html = include_html($comm,'footer', siteid=>$siteid);

    return $html;
}

#
exit(0);
