<? include ("sess.php");?>
<html>
<head>
<title>Un title page</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
 <style type="text/css">
<!--
.menuclick{background-color:#00CC33; color: #FFFFFF; font-size:14px; font-family:Microsoft Sans Serif; font-weight:bold;}
.inputdata{ border:0; border-bottom:thin dashed #999999;  background-color: #FFFFFF; color: #3399CC;font-family: "Microsoft Sans Serif";font-weight: normal; text-align: center; 
font-size:14px;   }
.inputtext{ border:0; border-bottom:thin dashed #999999;  background-color: #FFFFFF; color: #3399CC;font-family: "Microsoft Sans Serif";font-weight: normal; text-align: left; 
font-size:14px;  }
.inputproject{ border:0; border-bottom:thin dashed #999999;  background-color:#E7F4FF; color: #3399CC;font-family: "Microsoft Sans Serif";font-weight: normal; text-align: left; 
font-size:14px;  }
.style2 {
	font-family: "Microsoft Sans Serif";
	font-weight: bold;
	color: #6699FF;
}
.textstyle2 {
	font-family: "Microsoft Sans Serif";
	font-weight: bold;
	color: #999999;
}
.textstyle3 {
	font-family: "Microsoft Sans Serif";
	font-weight: normal;
	color:#FF0000;
}
.text1 {
	font-family: "Microsoft Sans Serif";
	font-weight: bold;
	color: #0099CC;
	text-align:center;
}
.style41 {
	font-family: "Microsoft Sans Serif";
	font-size: 14px;
	color: #000000;
	border-top-color: #FFFFFF;
	border-right-color: #FFFFFF;
	border-left-color: #FFFFFF;
	font-weight: normal;
	background-color: #FFFFFF;
	text-align: center;
	clip: rect(auto,auto,auto,auto);
}

.style3 {color: #FFFFFF}
.style4 {color: #000000}
 .style42 {color: #CCFFCC}
 </style>

</head>
<body bgcolor="#FFFFFF" >
<form action="" method="post" enctype="multipart/form-data" name="form1">
  <br><table width="683" border="0" align="center">
    <tr>
      <td><div align="left">
        <? if($edit==""){?>
        <label>
        <input name="add" type="submit" class="textstyle3" value="เพิ่มสมาชิก" id="add" />
        </label>
        <label>
        <input name="search" type="submit" class="textstyle3" id="search" value="ค้นหาสมาชิก">
        </label>
        <label>
        <? }else {?>
        <a href="index.php?module=Member">
        <input name="button2" type="button" class="textstyle3" value="&lt;&lt; กลับไปจัดการข้อมูลสมาชิก">
        </a>
        <input name="edit_data" type="submit" class="textstyle3" id="edit_data" value="แก้ไขข้อมูล">
        <?  
  $sql_edit=mysql_query("select * from tbl_member where id='$edit' ");
  $data_edit=mysql_fetch_array($sql_edit);
    }?>
        </label>
      </div>
      <label></label></td>
    </tr>
  </table>
<br>
  <table width="683" border="0" align="center">
    <tr>
      <td width="589"><table width="103%" border="0" cellspacing="5" cellpadding="0">
        <tr>
          <td width="48%"><label></label>
            <div align="left"><span class="style41">รหัสนักศึกษา</span><span class="text1"> :</span>
              <label>
              <input name="noID" type="text" id="noID" value="<?=$data_edit[no_id] ?>" />
              </label>
              <label></label>
              <label></label>
              <label></label>
            </div></td>
          <td width="52%"><div align="left" class="textstyle2"><span class="style41">ชื่อ-สกุล</span><span class="text1"> :<span class="style3">.<span class="style42">..</span></span></span>
                <span class="style42">
                <label>                </label>
                </span>
                <label><select name="intro" id="intro">
                  <option value="นาย"  <? if($data_edit[intro]=="นาย"){echo "selected";}?>>นาย</option>
                  <option value="นาง"  <? if($data_edit[intro]=="นาง"){echo "selected";}?>>นาง</option>
                  <option value="นางสาว"  <? if($data_edit[intro]=="นางสาว"){echo "selected";}?>>นางสาว</option>
                </select>
                </label>
          </span>
              <input name="name" type="text" id="name"  value="<?=$data_edit[name] ?>" size="20"/>
          </div></td>
        </tr>
      </table>
        <table width="100%" border="0" cellspacing="5" cellpadding="0">
          <tr>
            <td width="50%" height="38" bordercolor="#FFFFFF"><div align="left" class="style2"><span class="style41">โปรแกรมวิชา</span><span class="text1">: </span>
                <label>
                <select name="type_book" id="select" >
                  <? $sql_select=mysql_query("select* from type_program");
			while($data_select=mysql_fetch_array($sql_select)){
			  ?>
                  <option value="<?=$data_select[id] ?>" <? if($data_edit[depart]==$data_select[id]){echo"selected";} ?>>
                    <?=$data_select[name] ?>
                  </option>
                  <? }?>
                </select>
                </label>
            </div>
            <label></label></td>
            <td width="50%" bordercolor="#FFFFFF" ><div align="left"><span class="style41">สถานะ</span><span class="text1"> :</span><span class="style42">.....</span>
                <select name="status"  id="status">
                  <option value="ระบบพิเศษ">ระบบ กศบป.</option>
                  <option value="ระบบปกติ">ระบบ ปกติ</option>
                </select>
            </div></td>
          </tr>
        </table>
        <table width="100%" border="0" cellspacing="5" cellpadding="0">
          <tr>
            <td width="50%"><span class="style1"><span class="style4">
              <label for="pic"></label>
              </span> </span>
                <label for="pic"></label>
              <div align="left">
                <label for="pic"><span class="style41">ภาพนักศึกษา</span><span class="text1"> :</span></label>
                </label>
     
                <input name="pic" type="file" id="pic" size="14">
            </span></span></div></td>
            <td width="50%"><div align="left"><span class="style41">วันที่สมัคร</span><span class="text1"> :</span>
                <label>
                <input name="date_reg" type="text" id="datepicker" value="<?=$data_edit[date_reg] ?>" size="15">
                </label>
            </div>
            <label></label></td>
          </tr>
        </table></td>
      <td width="84" valign="middle">
      <? if ($edit!=""){?>
      <fieldset>
        <img src="pic/<?=$data_edit[pic] ?>" width="80" height="80">
      </fieldset>  
      <? }?>    </td>
    </tr>
  </table>

  <table width="683" border="0" align="center">
  <tr>
    <td width="651"><fieldset>
    <legend class="textstyle3">ข้อมูลสมาชิก</legend>
    <? include ("config.php");
	if($search){
$sql_search="select * from  tbl_member  where 1 ";
		if($noID){
		$sql_search .="and  no_id LIKE '%$noID' ";
		}
		if($intro){
		$sql_search .="and  intro ='$intro' ";
		}
		if($name){
		$sql_search .="and  name like '%$name%' ";
		}
				if($status){
		$sql_search .="and  status_ ='$status' ";
		}

	}else{
	$sql_search="select * from tbl_member order by date_reg desc ";
	}
	$sql_select=mysql_query($sql_search);
	$num=mysql_num_rows($sql_select);
	?>
    <div align="right"><span class="textstyle3">มีจำนวนสมาชิกทั้งหมด <?=$num; ?> คน</span><br>
    </div>
    <table width="640" border="0" align="center">

      <tr>
        <td width="113" height="30" bgcolor="#E7F4FF" class="inputproject"><div align="center" class="text1"><strong>รหัสนักศึกษา</strong></div></td>
        <td width="148" bgcolor="#E7F4FF" class="inputproject"><div align="center" class="text1"><strong>ชื่อ-สกุล</strong></div></td>
        <td width="130" bgcolor="#E7F4FF" class="inputproject"><div align="center" class="text1"><strong>โปรแกรมวิชา</strong></div></td>
        <td width="76" bgcolor="#E7F4FF" class="inputproject"><div align="center" class="text1"><strong>สถานะ</strong></div></td>
        <td width="70" bgcolor="#66CCFF" class="inputproject"><div align="center" class="text1"><strong>ลบข้อมูล</strong></div></td>
        <td width="77" bgcolor="#E7F4FF" class="inputproject"><div align="center" class="text1"><strong>แก้ไขข้อมูล</strong></div></td>
      </tr>
          <? while ($data=mysql_fetch_array($sql_select)){?>

      <tr>
        <td height="25" bgcolor="#FFFFCC" class="inputtext"><?=$data[no_id] ;?></td>
        <td bgcolor="#FFFFCC" class="inputtext"><? echo"$data[intro] $data[name]"; ?></td>
        <td bgcolor="#FFFFCC" class="inputtext"><?=$data[depart] ;?></td>
        <td bgcolor="#FFFFCC" class="inputtext"><div align="center">
          <?=$data[status_] ;?>
        </div></td>
        <td bgcolor="#FFFFCC" class="inputtext"><div align="center">
           <a href="<? echo" index.php?module=Member& delete=$data[id] & pic_=$data[pic]";?>">
        <img src="images/cancel.gif" width="16" height="16" border="0"></a></div></td>
        <td bgcolor="#FFFFCC" class="inputtext"><div align="center">
        <a href="<? echo" index.php?module=Member& edit=$data[id]";?>"><img src="images/validate.png" width="16" height="16" border="0"></a></div></td>
      </tr>
            <? }?>
    </table>
    </fieldset>
    </td>
    </tr>
</table>
</form>
<?
if($add){
if($pic!=""){
//echo "$pic"; exit();
							 $pic =$_FILES['pic'] ['tmp_name'];
							  $pic_name=$_FILES['pic'] ['name'];
							  $pic_size =$_FILES['pic'] ['size'];
							  $pic_type =$_FILES['pic'] ['type'];
					///	echo $pic_type ; exit();
						if ($pic_type !="image/gif" and $pic_type != "image/jpeg" and $pic_type != "image/pjpeg" 
	  and $pic_type != "image/x-png " and $pic_type != "image/png") {
		echo "<script>alert('ระบบรองรับไฟล์นามสกุลเฉพาะ 	.gif  .jpeg  และ .png เท่านั้น')</script>";
		echo "<script>window.location='index.php?module=Member'</script>";  	 exit();
														
	}
							
	
				$images = $_FILES["pic"]["tmp_name"];
				$new_images = "$noID".$_FILES["pic"]["name"];
				copy($_FILES["pic"]["tmp_name"],"pic/".$_FILES["pic"]["name"]);
				$width=120; //*** Fix Width & Heigh (Autu caculate) ***//
				$size=GetimageSize($images);
				$height=120;
				if($size[2] == 1) {
				$images_orig = imagecreatefromgif($images); //resize ????????? GIF
				} else if($size[2] == 2) {
				$images_orig = imagecreatefromjpeg($images); //resize ????????? JPEG
				}else if($size[2] == 3) {
				$images_orig = imagecreatefrompng($images); //resize ????????? PNG
				}

				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageJPEG($images_fin,"pic/".$new_images);
				ImageDestroy($images_orig);
				ImageDestroy($images_fin);
				unlink("pic/".$_FILES["pic"]["name"]);
			}
$sql="insert into tbl_member values ('','$noID','$intro','$name','$department','$new_images','$status','$date_reg')";
//echo $sql ; exit();
$re=mysql_query($sql);
if($re){
echo "<script>alert('ระบบได้ทำการเพิ่มข้อมูลสมาชิกเรียบร้อยแล้ว')</script>";
echo "<script>window.location='index.php?module=Member'</script>";exit();
}
}
 ?>
<?
if($edit_data){
if($pic!=""){
//echo "$pic"; exit();
							 $pic =$_FILES['pic'] ['tmp_name'];
							  $pic_name=$_FILES['pic'] ['name'];
							  $pic_size =$_FILES['pic'] ['size'];
							  $pic_type =$_FILES['pic'] ['type'];
					///	echo $pic_type ; exit();
						if ($pic_type !="image/gif" and $pic_type != "image/jpeg" and $pic_type != "image/pjpeg" 
	  and $pic_type != "image/x-png " and $pic_type != "image/png") {
		echo "<script>alert('ระบบรองรับไฟล์นามสกุลเฉพาะ 	.gif  .jpeg  และ .png เท่านั้น')</script>";
		echo "<script>window.location='index.php?module=Member'</script>";  	 exit();
														
	}
							
				$images = $_FILES["pic"]["tmp_name"];
				$new_images = "$noID".$_FILES["pic"]["name"];
				copy($_FILES["pic"]["tmp_name"],"pic/".$_FILES["pic"]["name"]);
				$width=120; //*** Fix Width & Heigh (Autu caculate) ***//
				$size=GetimageSize($images);
				$height=120;
				if($size[2] == 1) {
				$images_orig = imagecreatefromgif($images); //resize ????????? GIF
				} else if($size[2] == 2) {
				$images_orig = imagecreatefromjpeg($images); //resize ????????? JPEG
				}else if($size[2] == 3) {
				$images_orig = imagecreatefrompng($images); //resize ????????? PNG
				}

				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageJPEG($images_fin,"pic/".$new_images);
				ImageDestroy($images_orig);
				ImageDestroy($images_fin);
				unlink("pic/".$_FILES["pic"]["name"]);
			}
			if($pic!=""){
$sql="update  tbl_member set no_id='$noID',intro='$intro',name='$name',depart='$department',pic='$new_images',status_='$status',date_reg='$date_reg' where id='$data_edit[id]' ";
}else{
$sql="update  tbl_member set no_id='$noID',intro='$intro',name='$name',depart='$department',status_='$status',date_reg='$date_reg' where id='$data_edit[id]'";
}
//echo $sql ; exit();
$re=mysql_query($sql);
if($re){
echo "<script>alert('ระบบได้ทำการแก้ไขข้อมูลสมาชิกเรียบร้อยแล้ว')</script>";
echo "<script>window.location='index.php?module=Member& edit=$data_edit[id]'</script>";exit();
}
}
 ?>
<? if($delete){
$re=mysql_query("delete from tbl_member where id='$delete' ");
if($re){
unlink("pic/$pic_");
echo "<script>alert('ระบบได้ทำการลบข้อมูลสมาชิกเรียบร้อยแล้ว')</script>";
echo "<script>window.location='index.php?module=Member'</script>";exit();
}
}?>
</body>
</html>