<?php
/**
 * 领导机构编辑完成
 * $Id: government_edit_result.php,v 1.1 2020/01/03 11:18:46  Exp $
 * @author lixq
 * @package manager.public_html
 */
// 底层包含
require_once("manager_include.inc");

// 登录检查
require_once("check_login.inc");

// 权限检查
if (!checkAuthority("2")) {
	// エラー表示
	$layout_pages = array();
	$layout_pages["left"] = "menu.inc";
	$layout_pages["right"] = "error.inc";
	$message = "权限不足，请联系系统管理员。";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/leftmenu_layout.inc");
	exit;
}

// 参数取得
$id = ParamUtil::getRequestString("id");
$government_id = ParamUtil::getRequestString("government_id");
$government_title = ParamUtil::getRequestString("government_title");
$action_type = ParamUtil::getRequestString("action_type");
$max_count = ParamUtil::getRequestNumber("max_count");
$limit_date = ParamUtil::getRequestString("limit_date");

if($action_type =="new"){
  $government_qr_mst = new GovernmentQrDat();
}else{
  $government_qr_mst = GovernmentQrDat::getById($id);
  if ($government_qr_mst == null) {
    // エラー表示
    $layout_pages = array();
    $layout_pages["left"] = "menu.inc";
    $layout_pages["right"] = "error.inc";
    $layout_pages["menu_clicked"] = "5-2";
    require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/leftmenu_layout.inc");
    exit;
  }
}
$government_mst = GovernmentMst::getById($government_id);

$government_qr_mst->government_id = $government_id;
$government_qr_mst->province = $government_mst->province;
$government_qr_mst->city = $government_mst->city;
$government_qr_mst->district = $government_mst->district;

$government_qr_mst->use_count = 0;
$government_qr_mst->max_count = $max_count;
$government_qr_mst->limit_date = $limit_date;
$government_qr_mst->title = $government_mst->title;
$government_qr_mst->save();

if(empty($government_qr_mst->qr_image)){
  $param = array();
  $param["delete_flg"] = false;
  $government_qr_id = GovernmentQrDat::getList($param,"id","DESC")[0]->id;
  
  //$qrHandler = new GetWxQrHandler(3,'pages/leaderRegister/leaderRegister');
  $qrHandler = new GetWxQrHandler($government_qr_id);
  $qr_img = $qrHandler->get_qrcode();
  if(!$qr_img){
    $error_msg = "二维码未生成，请联系管理员。";
  }else{
    $tmp_file_name = date("YmdHis") . "_" . $government_id . "_" . $government_qr_id. ".jpg";
    if(!is_dir(UPLOAD_TMP_DIR)){
      mkdir($path, 0777, true);
    }
    $file_path = UPLOAD_TMP_DIR . "/" . $tmp_file_name;
    file_put_contents($file_path,$qr_img);
    
    $aliHandler = new AliUploadHandler();
    $url = $aliHandler->uploadImg($tmp_file_name, $file_path);
    $government_qr_mst = GovernmentQrDat::getById($government_qr_id);
    $government_qr_mst->qr_image = $url;
    $government_qr_mst->save();
    @unlink($file_path);
    $error_msg = "二维码已生成！";
  }
}
// 页面表示
$layout_pages = array();
$layout_pages["left"] = "menu.inc";
$layout_pages["right"] = "government_qr_edit_result.inc";
$layout_pages["menu_clicked"] = "5-2";
require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/leftmenu_layout.inc");
exit;