<?php
/**
 * 视频编辑
 * $Id: media_edit_input.php,v 1.1 2020/01/03 11:18:46  Exp $
 * @author lixq
 * @package manager.public_html
 **/

?>
<div id="change">
	<p class="edit_title">视频上传</p>
	<form id="form" name="media_upload" action="media_edit_result.php" method="post" enctype="multipart/form-data">
		<input name="post_max" type="hidden" value="<?=ini_get('post_max_size')?>">
		<input name="id" type="hidden" value="<?=$id?>">
		<input name="upload_max" type="hidden" value="<?=ini_get('upload_max_filesize')?>">
		<input type="hidden"  name="parent_category_id" value="<?=$parent_category_id?>"/>
		<input type="hidden"  name="sub_category_id" value="<?=$sub_category_id?>"/>
	<div class="edit_content">
		<p>视频名称</p>
		<el-input v-model="title" name="title" class="input_200"></el-input><br />
		<p>视频标签</p>
		<el-input v-model="media_tags" name="media_tags" class="input_200 popselect" readonly onfocus="doCheckPopup('media_upload','media_tags','tags','pop_tag_list.php');"></el-input>
		<input type="hidden" name="tags" value="<?=$tags?>" />
		<el-button type="primary" class="height_30" @click="selectClear('media_upload', 'media_tags', 'tags');">清空</el-button>
		<el-button type="primary" class="height_30" @click="popAddTag()">添加标签</el-button>
		<br />
		<p>讲师姓名</p>
		<el-input v-model="teacher" name="teacher" class="input_200"></el-input><br />
		<p>讲师介绍</p>
		<textarea v-model="teacher_profile" name="teacher_profile" rows="5"></textarea><br />
		<p>视频封面图</p>
		<a href='javascript:void(0);' class="el-button el-button--primary blueButton">选择文件</a>
			<input id="image_file" class="myFileUpload" name="front_image" type="file" accept="image/*"/>
		<div id="img_thumbnail">
    	<img id="imgshow" height="150px" alt="学校封面图" />
		</div>
		<p>视频类型</p>
		<template>
  		<el-radio v-model="media_type" name="media_type" label="0">单个</el-radio>
  		<el-radio v-model="media_type" name="media_type" label="1">合集</el-radio>
		</template>
		<div v-if="is_category_show">
		<p>一级分类名称</p>
			<el-select v-model="parent_category_id" size="small">
        <el-option
            v-for="item in parent_category_list"
            :key="item.id"
            :label="item.title"
            :value="item.id">
        </el-option>
    	</el-select>
    <p>二级分类名称</p>
			<el-select v-model="sub_category_id" size="small">
        <el-option
            v-for="item in new_sub_category_list"
            :key="item.id"
            :label="item.title"
            :value="item.id">
        </el-option>
    	</el-select>	
		</div>
		<div v-if="is_course_show">
  		<p>选择合集</p>
  		<el-input v-model="course_name" name="course_name" class="input_200 popselect" readonly onfocus="doRadioPopup('media_upload','course_name','course_id','pop_media_list.php');"></el-input>
  		<input type="hidden" name="course_id" value="<?=$course_id?>" />
  		<el-button type="primary" class="height_30" @click="selectClear('media_upload', 'course_name', 'course_id');">清空</el-button>
  		<el-button type="primary" class="height_30" @click="popAddCourse()">创建合集</el-button>
  		<br />
		</div>
		<p>视频种类</p>
		<template>
  		<el-radio v-model="media_kind" name="media_kind" label="false">志愿免费</el-radio>
  		<el-radio v-model="media_kind" name="media_kind" label="true">公益收费</el-radio>
		</template>
		<div v-if="is_price_show">
  		<p>收费价格</p>
  		<el-input v-model="price" name="price" type="number" class="input_200"></el-input><br />
  		<br />
		</div>
		<p>课程简介</p>
		<textarea v-model="comment" name="comment" rows="5"></textarea><br />
		
		<br/><br/>
		<el-button type="primary" @click="submit()" class="edit_btn">编辑</el-button>
		<el-button type="primary" @click="back()" class="edit_btn">返回</el-button><br/><br/>
	</div>
	</form>
</div>

<script type="text/javascript">
	var v = new Vue({
		el:'#change',
		data:{
		  title:'<?=$title?>',
		  media_tags:'<?=$tags?>',
		  media_type:'<?=$media_type?>',
		  teacher:'<?=$teacher?>',
		  parent_category_id:<?=$parent_category_id?>,
			sub_category_id:'<?=$sub_category_id?>',
			teacher_profile:'<?=$teacher_profile?>',
			comment:'<?=$comment?>',
		  media_kind:'<?=$media_kind?>',
		  price:'<?=$price?>',
		  course_name:'<?=$course_name?>',
		  is_category_show:<?=$media_type == '1'?'false':'true'?>,
		  is_course_show:<?=$media_type == '0'?'false':'true'?>,
		  is_price_show:<?=$media_kind == 'true'?'true':'false'?>,
		  front_image:'<?=$front_image?>',
		  sub_category_list:<?=json_encode($sub_category_list)?>,
			new_sub_category_list:<?=json_encode($new_sub_category_list)?>,
			parent_category_list:<?=json_encode($parent_category_list)?>,
		},
		watch:{
		  media_type(val,oldval){
				if(val == "0"){
				  this.is_course_show = false;
				}else{
					this.is_course_show = true;
				}
		  },
		  media_kind(val,oldval){
				if(val == 'false'){
				  this.is_price_show = false;
				}else{
					this.is_price_show = true;
				}
		  },
		  parent_category_id(val,oldval){
        var new_list = [];
        this.sub_category_list.map(function(item) {
          if (item.parent_id == val) {
            new_list.push(item);
          }
        });
      	this.new_sub_category_list = new_list;
      	this.sub_category_id = this.new_sub_category_list[0]["id"];
      }
		},
		methods:{
		  back:function(){
			  window.history.go(-1);
			},
			submit:function(){
				if(!this.title){
					this.$message({
		            type: 'error',
		            message: '请输入视频名称!'
		          });
		          return;
				}
				if($("input[name='tags']").val().length < 1){
					this.$message({
		            type: 'error',
		            message: '请选择视频标签!'
		          });
		          return;
				}
				if(this.media_type == '0' && !this.parent_category_id){
				  this.$message({
            type: 'error',
            message: '请选择视频一级分类名称!'
          });
          return;
				}
				if(this.media_type == '0' && !this.sub_category_id){
				  this.$message({
            type: 'error',
            message: '请选择视频二级分类名称!'
          });
          return;
				}
				if(this.media_type == '1' && $("input[name='course_id']").val() == '0'){
				  this.$message({
            type: 'error',
            message: '请选择合集名称!'
          });
          return;
				}
				if(this.media_kind == 'true' && !this.price){
				  this.$message({
            type: 'error',
            message: '请输入收费价格!'
          });
          return;
				}
				if(this.media_kind == 'true' && !isPointNumber(this.price)){
				  this.$message({
            type: 'error',
            message: '请输入正确格式收费价格!'
          });
          return;
				}
				$("input[name='parent_category_id']").val(this.parent_category_id);
				$("input[name='sub_category_id']").val(this.sub_category_id);
				$('#form').submit();
			},
			
		}
	})
	window.onload = function(){
  	$('#image_file').change(function(){
      var file = $('#image_file').get(0).files[0];
      if(file.type !== 'image/png'&&file.type !=='image/gif'&&file.type !=='image/jpg'&&file.type !=='image/jpeg'){
        v.$message({
          type: 'error',
          message: '上传文件只能是图片格式!'
        });
        return;
      }
			if(file.size / 1024 / 1024 > 2){
			  v.$message({
          type: 'error',
          message: '上传文件大小不能超过 2MB!'
        });
        return;
			}
      var reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload=function(e){
        $('#imgshow').get(0).src = e.target.result;
      }
      $('#img_thumbnail').css("visibility","visible");
    })
    if(<?=$front_image?1:0 ?>){
		  $('#imgshow').get(0).src = '<?=$front_image?>';
		  $('#img_thumbnail').css("visibility","visible");
		}
	}
</script>


