<?php
/**
 * 此文件用来编辑表{{$Page.TableName}}中的记录。
 *
 * copyright
 * @author          {{$Page.Author}}
 * @package         {{$Page.PackageName}}
 * @version         {{$IdTag}}
 */
/* 初始化系统。 */
require_once("{{$Page.IncludePath}}Include/InitAdmin.php");
langLoadTable("{{$Page.TableName}}");

/* 用户提交了数据，检查，保存。*/
if($_POST)
{
    {{if $Page.FieldsToPHP}}
    {{foreach from=$Page.FieldsToPHP key=FieldName item=InputParam}}
    {{if $InputParam.Type=="Checkbox"}}
    {{$CFG.GEN.EmptyLine}}
    /* 如果变量以复选框的形式传递，使用逗号连接起来。*/
    $_POST["{{$FieldName}}"] = join(",", $_POST["{{$FieldName}}"]);
    {{/if}}
    {{/foreach}}
    {{/if}}
    
    /* 检查数据。 */
    {{foreach from=$Page.Fields.Required key=FieldName item=Field}}
    {{if $Field.DataRule=="Length"}}
    {{$CFG.GEN.EmptyLine}}
    $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}", {{$Field.Size}});
    $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
    {{elseif $Field.DataRule != "None"}}
    {{$CFG.GEN.EmptyLine}}
    $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}");
    $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
    {{/if}}
    {{/foreach}}
    {{foreach from=$Page.Fields.Optional key=FieldName item=Field}}
    {{if $Field.DataRule != "None"}}
    {{$CFG.GEN.EmptyLine}}
    if(!empty($_POST["{{$FieldName}}"]))
    {
        {{if $Field.DataRule=="Length"}}
        {{$CFG.GEN.EmptyLine}}
        $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}", {{$Field.Size}});
        $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
        {{elseif $Field.DataRule != "None"}}
        {{$CFG.GEN.EmptyLine}}
        $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}");
        $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
        {{/if}}
        {{$CFG.GEN.EmptyLine}}
    }
    {{/if}}
    {{$CFG.GEN.EmptyLine}}
    {{/foreach}}

    /* 如果有错误，提示用户。 */
    if(!empty($ErrorMsgList))
    {
        $MyJS->alert(join("\\n", $ErrorMsgList));
        $MyJS->goto("Back");
        exit;
    }

    /* 更新记录。*/
    $SQL = "UPDATE {{$Page.TableName}} {{$Page.UpdateString}}
             WHERE {{$Page.WhereString}} LIMIT 1";
    if(!$MyDB->query($SQL))
    {
        die($MyDB->errorMsg());
    }
    else
    {
        {{foreach from=$Page.IdField item=FieldName}}
        {{$CFG.GEN.EmptyLine}}
        $MyTPL->assign("{{$FieldName}}", $_REQUEST["{{$FieldName}}"]);
        {{/foreach}}
        {{$CFG.GEN.EmptyLine}}
        $MyTPL->assign("Saved", true);
        $MyTPL->display($_CFG["TPL"]);
        exit;
    }
}

if({{$Page.JudgeEmpty}})
{
    $MyJS->alert($_LANG["MustSet"] . {{$Page.AlertInfo}});
    $MyJS->goto("Back");
    exit;
}
else
{
    $Item = $MyDB->getAll("SELECT * FROM {{$Page.TableName}} WHERE {{$Page.WhereString}}");
    $Item = $Item[0];
}
{{if $Page.FieldsToPHP}}

{{foreach from=$Page.FieldsToPHP key=FieldName item=InputParam}}
{{if $InputParam.Table}}
/* 查找{{$FieldName}}列表从{{$InputParam.Table}}中。*/
$SQL        = "SELECT {{$InputParam.Fields.Value}} AS Value, {{$InputParam.Fields.Label}} AS Label FROM {{$InputParam.Table}}";
$ReaultID   = $MyDB->query($SQL) or die($MyDB->errorMsg());
while($Item = $ReaultID->fetchItem())
{
    ${{$FieldName}}List[$Item['Value']] = $Item["Label"];
}
$MyTPL->assign("{{$FieldName}}List", html{{$InputParam.Type}}(${{$FieldName}}List, "{{$FieldName}}", "Reverse", $Item["{{$FieldName}}"]));
{{elseif $InputParam.List}}
/* 创建{{$FieldName}}列表。*/
{{foreach from=$InputParam.List key=ItemValue item=ItemLabel}}
${{$FieldName}}List["{{$ItemValue}}"] = "{{$ItemLabel}}";
{{/foreach}}
$MyTPL->assign("{{$FieldName}}List", $MyHtml->select(${{$FieldName}}List, "{{$FieldName}}", "Reverse", $Item["{{$FieldName}}"]));
/* 赋值{{$FieldName}}，通过_GET方式获得。*/
$MyTPL->assign("{{$FieldName}}", $_GET["{{$FieldName}}"]);
{{elseif $InputParam.Func}}
/* 赋值{{$FieldName}}，通过{{$InputParam.Func}}创建。*/
$MyTPL->assign("{{$FieldName}}List", htmlSelect({{$InputParam.Func}}(), "{{$FieldName}}"));
{{/if}}
{{/foreach}}
{{/if}}

/* 赋值，显示模板。*/
{{foreach from=$Page.IdField item=FieldName}}
$MyTPL->assign("{{$FieldName}}", $Item["{{$FieldName}}"]);
{{/foreach}}
$MyTPL->assign("PageTitle", $_LANG["TableLabel"] . $_LANG["AddPage"]);
$MyTPL->assign("Item",      $Item);
$MyTPL->display($_CFG["TPL"]);
?>