<template>
  <div class="main_box screen1" v-loading="loading">
    <el-breadcrumb separator-class="el-icon-arrow-right" class="ml40 xw995">
      <el-breadcrumb-item>资讯管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{name:'course'}">资讯列表</el-breadcrumb-item>
      <el-breadcrumb-item>新建课程</el-breadcrumb-item>
    </el-breadcrumb>  
    <el-form :model="articleModel" :rules="rules" ref="editor" class="from_box clr">
      <el-form-item prop="Title" class="from_item">
        <label>标题</label>
        <img src="/static/img/common/main.png">
        <el-input placeholder="请填标题" v-model="articleModel.Title"></el-input>
      </el-form-item>
     <el-form-item prop="SubTitle" class="from_item">
        <label>副标题</label>
        <img src="/static/img/common/main.png">
        <el-input placeholder="请填副标题" v-model="articleModel.SubTitle"></el-input>
      </el-form-item>
      <el-form-item prop="Keyword" class="from_item">
        <label class="noimh">关键字</label>
        <el-input  v-model="articleModel.Keyword" type="number"></el-input>
      </el-form-item>
      <el-form-item prop="AccessCount" class="from_item">
        <label>访问人数：</label>
        <img src="/static/img/common/main.png">
        <el-input placeholder="请填访问人数：：" v-model="articleModel.AccessCount" type="number"></el-input>
      </el-form-item>
      <el-form-item prop="OnLine" class="from_item">
        <label>发布状态:</label>
        <img src="/static/img/common/main.png">
        <div>
            <el-switch  v-model="articleModel.Publish"  :active-value="1" :inactive-value="0"> </el-switch>
        </div>        
      </el-form-item> 
      <el-form :inline="true">
        <el-form-item prop="Thumb" class="from_item">
        <label>课程封面：</label>
        <div class="uploadimg_box ">
          <img :src="articleModel.Thumb" v-if="articleModel.Thumb!=''" class="uploadimg" @click="upload(setCover)"  />
          <span class="ubloadbtn" v-else>
            <img src="/static/img/common/imgadd.png" @click="upload(setCover)" >
          </span>
          <p class="imgTip" @click="upload(setCover)" >点击更换</p>
          <p class="zhu">注：图片大小不可超过2M
          </p>
        </div>
      </el-form-item>
      </el-form>  
    </el-form>
    <div class="editor-container">
         <UE v-model="articleModel.Content" :config='config' ref="ue"></UE>
    </div>
    <div class="handel_box clr">
      <router-link :to="{name:'course'}" tag="input" type="button" value="返回" class="cursor"></router-link>

      <input type="button" value="保存" @click="save('editor')" class="small cursor">

    </div>
  </div>
</template>
<script>
let that;
export default {
 components: {  
 
  },
  data() {
    return {    
       articleModel:{
           Publish:0,
           id:0,
           Thumb:'',     
       },
      loading: false,
      rules: {
        Title: [this.$vaild.required("标题")],     
        SubTitle: [this.$vaild.required("副标题")],      
      //  Banner: [this.$vaild.required("课程主图")],
       // Cover: [this.$vaild.required("课程封面")],        
      },  
     config:{
            serverUrl:this.$api.baseURL+"ueditor",
            UEDITOR_HOME_URL: "/static/ueditor/",
            autoHeightEnabled: false,
            initialFrameHeight: 500,
     },
     options:[]
    }
  },
  created() {   
    var query=this.$route.query;    
      if(query.optype==2){
        this.getInfo(query.id);
      }
  },
  methods: {
    getInfo(id){
      this.$api.get("course/v1/article/Get",{id:id}).then(res=>{this.articleModel=res.Data});
    },
    save(formName) {
      var that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {         
          if (that.$route.query.optype=="1") {         
            that.add();
          } else if(that.$route.query.optype=="2") {
            that.edit();
          }
        } 
      })
    },
    add(){
     this.$api.post("course/v1/article/post",this.articleModel).then(res=>{
            this.$router.push({name:"course"})
     })
    },
    edit(){
     this.$api.post("course/v1/article/put",this.articleModel).then(res=>{
            this.$router.push({name:"article"})
     })
    },
    upload(callback) {     
      this.$api.upload('oss/v1/ImageCloud/upload').then(res=>{ 
        callback&&callback(res.Data);
      });
    },
    setCover(ret) {
     this.articleModel.Thumb=ret.Url;
    }
  }
};
</script>
