require_once '../system/Class_ERROR.php';
require_once '../system/login.inc.php';
// Class_PWD::valid_auth(AOKI_AUTH_ACCOUNT,$login['staff_auth']);
// $login_auth = $login['staff_auth'];
$dao = new Class_mp_account();
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $phase = 'input';
    if (isset($_GET['id']) && $_GET['id'] != '') {
        $forms = $dao->get($_GET['id']);
        //print_r($forms);
        $forms["id"] = $_GET['id'];
    }
} else {
    $forms = $_POST;
    if ($forms['mode'] == 'input') {
        $err = new Class_ERROR();
        $chk = $dao->get_checks();
        foreach ($chk as $kcol => $chkval) {
            $err_mes[$kcol] = $err->check($forms[$kcol], $chkval);
        }
        // ログインIDチェック
        if ($err_mes['login_id'] == '') {
            if ($forms['id'] == '') {
                $where = sprintf(" login_id = '%s'", $forms['login_id']);
            } else {
                $where = sprintf(" login_id = '%s' AND id <> %s", $forms['login_id'], $forms['id']);
            }
            if ($dao->exits($where)) {
                $err->clear = false;
                $err_mes['login_id'] = "<p class='error'>このログインIDは重複しています</p>";
            }
require_once '../system/mdao/Class_mp_service_category.php';
require_once '../system/mdao/Class_mp_promise_spec.php';
require_once '../system/mdao/Class_mp_spec_fee.php';
$dao = new Class_mp_service();
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $phase = 'input';
    if (isset($_GET['id']) && $_GET['id'] != '') {
        $service_forms = $dao->get($_GET['id']);
        $forms['id'] = $_GET['id'];
    }
} else {
    $forms = $_POST;
    $values = array();
    if ($forms['mode'] == 'input') {
        // check service items
        $err = new Class_ERROR();
        $service_chk = $dao->get_checks();
        foreach ($service_chk as $kcol => $chkval) {
            $err_mes[$kcol] = $err->check($forms[$kcol], $chkval);
        }
        $err_mes["kinds"] = $err->check($forms['kinds'], array("EXIST"));
        // reset values
        $smarty->assign('forms', $forms);
        if ($err->clear) {
            $cols = $dao->get_cols();
            foreach ($cols as $kcol => $val) {
                $values[$kcol] = $forms[$kcol];
            }
            $values['category_id'] = $forms['category_id'];
            if (isset($forms['service_fee']) && is_numeric($forms['service_fee'])) {
                $values['service_fee'] = $forms['service_fee'];
     $forms = $_POST;
 }
 $account_id = $forms['account_id'];
 $account_dao = new Class_mp_account($corporate_dao->get_db());
 $forms['mutirow'] = array();
 // get tantou_id array
 if (!empty($forms['tantou_id'])) {
     foreach ($forms['tantou_id'] as $k => $v) {
         if ($forms['tantou_name'][$k] != '' && $forms['mail_address'][$k] != '') {
             $forms['mutirow'][] = array("id" => $v, "tantou_name" => $forms['tantou_name'][$k], "mail_address" => $forms['mail_address'][$k]);
         }
     }
 }
 if ($forms['mode'] == 'input') {
     // check corporate items
     $err = new Class_ERROR();
     $corporate_chk = $corporate_dao->get_checks();
     foreach ($corporate_chk as $kcol => $chkval) {
         $err_mes[$kcol] = $err->check($forms[$kcol], $chkval);
     }
     //郵便番号判定
     $err_mes['post_code'] = $err->zipcode_check($forms['region'] . '-' . $forms['branch']);
     //電話番号判定
     $err_mes['tel'] = $err->tel_check($forms['area_code'] . '-' . $forms['office_number'] . '-' . $forms['called_number']);
     $index = 0;
     $tantou_dao = new Class_mp_corporate_tantou($corporate_dao->get_db());
     // get tantou checked items
     $tantou_chk = $tantou_dao->get_checks();
     // check each tantou item
     foreach ($forms['mutirow'] as $k => $v) {
         if ($v['tantou_name'] != '' && $v['mail_address'] != '') {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $forms = $_POST;
    // 看是否設置了cookie
    if (isset($_COOKIE['login_id']) && isset($_COOKIE['login_pass'])) {
        //$id = $_COOKIE['login_id'];
        //$pass = $_COOKIE['login_pass'];
    } else {
        $id = $forms['login_id'];
        $pass = $forms['login_pass'];
    }
    $keep = $forms['keep_login'];
    if ($forms['keep_login'] != '') {
        //setcookie("login_id",$id,time()+300);
        //setcookie("login_pass",$pass,time()+300);
    }
    $oerr = new Class_ERROR('', '');
    // check login_id and login_pass
    $errchk = array("EXIST");
    $err_mes['login_id'] = $oerr->check($id, $errchk);
    $err_mes['login_pass'] = $oerr->check($pass, $errchk);
    $msg_bg = '<p class="caution">';
    $msg_ed = '</p>';
    // login_id err
    if ($err_mes['login_id']) {
        $err_msg = $msg_bg . "IDは" . $err_mes['login_id'] . $msg_ed;
    }
    // login_pass err
    if ($err_mes['login_pass']) {
        $passmsg = $msg_bg . "パスワードは" . $err_mes['login_pass'] . $msg_ed;
        if ($err_msg) {
            $err_msg = $err_msg . $passmsg;
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $phase = 'input';
    // get category data
    $sql = "select * from mp_service_category";
    $forms = $dao->get_rows($sql);
} else {
    $forms = $_POST;
    // set $forms['mutirow'] array
    if (isset($forms['category_id'])) {
        foreach ($forms['category_id'] as $k => $v) {
            $forms['mutirow'][] = array("id" => $v, "category_name" => $forms['category_name'][$k]);
        }
    }
    if ($forms['mode'] == 'input') {
        // check $forms['mutirow'] items
        $err = new Class_ERROR();
        $index = 0;
        $category_chk = $dao->get_checks();
        foreach ($forms['mutirow'] as $k => $v) {
            $err_mes['category_name'][$index] = $err->check($v['category_name'], $category_chk['category_name']);
            $index = $index + 1;
        }
        // verification check
        // $temp = 1;
        // foreach ($forms ['mutirow'] as $k => $v) {
        // $num = 0;
        // foreach ($forms ['mutirow'] as $kk => $vv){
        // if($v['category_name'] == $vv['category_name']){
        // $num = $num + 1;
        // }
        // }
                $promise_spec_forms[$k]['minute'] = substr($nowdatetime, 14, 2);
            }
            //$profile_name_arr [] = $profile_data ['profile_name'];
        }
        //print_r($promise_spec_forms);
        $forms['id'] = $_GET['id'];
    }
} else {
    $forms = $_POST;
    if (isset($forms['spec_id'])) {
        foreach ($forms['spec_id'] as $k => $v) {
            $forms['mutirow'][] = array("id" => $forms['pro_id'][$k], "promise_id" => $forms['promise_id'], "spec_name" => $forms['spec_name'][$k], "spec_id" => $forms['spec_id'][$k], "profile_name" => $forms['profile_name'][$k], "profile_id" => $forms['profile_id'][$k], "service_name" => $forms['service_name'][$k], "service_id" => $forms['service_id'][$k], "day" => $forms['day'][$k], "hour" => $forms['hour'][$k], "minute" => $forms['minute'][$k], "before_mail" => $forms['before_mail' . $k], "overtime_have" => $forms['overtime_have' . $k], "overtime_fee" => $forms['overtime_fee'][$k], "traffic_fee_have" => $forms['traffic_fee_have' . $k], "traffic_fee" => $forms['traffic_fee'][$k], "traffic_fee_detail" => $forms['traffic_fee_detail'][$k], "live_fee_have" => $forms['live_fee_have' . $k], "live_fee" => $forms['live_fee'][$k], "live_fee_detail" => $forms['live_fee_detail'][$k], "other_fee_have" => $forms['other_fee_have' . $k], "other_fee" => $forms['other_fee'][$k], "other_fee_name" => $forms['other_fee_name'][$k], "other_fee_detail" => $forms['other_fee_detail'][$k]);
        }
    }
    if ($forms['mode'] == 'input') {
        $err = new Class_ERROR();
        $promise_chk = $promise_dao->get_checks();
        /*
        foreach ( $promise_chk as $kcol => $chkval ) {
        	$err_mes [$kcol] = $err->check ( $forms [$kcol], $chkval );
        }
        */
        $operation_history_dao = new Class_mp_promise_operation_history($promise_dao->get_db());
        $index = 0;
        $promise_spec_dao = new Class_mp_promise_spec($promise_dao->get_db());
        $promise_spec_chk = $promise_spec_dao->get_checks();
        $dublipe_flag = false;
        $specidarr = array();
        if (count($forms['mutirow']) > 0) {
            foreach ($forms['mutirow'] as $k => $v) {
                $err_mes['spec_name'][$index] = $err->check($v['spec_name'], $promise_spec_chk['spec_name']);
 // get traffic_fee_id arr
 if (isset($forms['traffic_fee_id'])) {
     foreach ($forms['traffic_fee_id'] as $k => $v) {
         $forms['mutirow'][] = array("id" => $v, "traffic_name" => $forms['traffic_name'][$k], "traffic_fee" => $forms['traffic_fee'][$k], "traffic_memo" => $forms['traffic_memo'][$k], "d_time" => $forms['d_time'][$k]);
     }
 }
 // set $forms['mutirow_fee'] array
 if (isset($forms['spec_fee_id'])) {
     foreach ($forms['spec_fee_id'] as $k => $v) {
         $forms['mutirow_fee'][] = array("id" => $v, "service_id" => $forms['service_id'][$k], "servers_menu" => $forms['servers_menu'][$k], "spec_fee" => $forms['spec_fee'][$k], "servers_fee" => $forms['servers_fee'][$k]);
     }
 }
 if ($forms['mode'] == 'input') {
     // check specialist items
     $values = array();
     $err = new Class_ERROR();
     $specialist_chk = $specialist_dao->get_checks();
     $forms["interlingua"] = str_replace(' ', ' ', $forms["interlingua"]);
     foreach ($specialist_chk as $kcol => $chkval) {
         $err_mes[$kcol] = $err->check($forms[$kcol], $chkval);
     }
     //郵便番号判定
     $err_mes['post_code'] = $err->zipcode_check($forms['region'] . "-" . $forms['branch']);
     //電話番号判定
     $err_mes['tel'] = $err->tel_check($forms['area_code'] . "-" . $forms['office_number'] . "-" . $forms['called_number']);
     //携帯電話番号判定
     $err_mes['phone'] = $err->tel_check($forms['cell1'] . '-' . $forms['cell2'] . '-' . $forms['cell3']);
     //FAX番号判定
     $err_mes['fax'] = $err->fax_check($forms['fax1'] . '-' . $forms['fax2'] . '-' . $forms['fax3']);
     //年月日判定
     if (!empty($forms['birthday_year']) && !empty($forms['birthday_month']) && !empty($forms['birthday_day'])) {