<template>
<div class="baseinfo">
<form class="baseinfo-box" @submit='commit'>
<div class="list-input">
  <span>讲座标题</span>
  <input placeholder="请输入讲座标题" name='name' :value="name" @input='name=service.Input($event)' placeholder-style='color:#9B9B9B'>
</div>
<div class="list-input between">
  <span>讲座街道</span>
  <picker :value='street.index' :range='street.columnOne' @change="pickerConfirm($event,'street')">
    <div hover-class='hover'>{{street.label}}</div>
  </picker>
</div>
<div class="list-input between">
  <span>讲座社区</span>
  <picker :disabled="street.label=='点击选择街道'" @tap='warning' :value='community.index' :range='community.columnOne' @change="pickerConfirm($event,'community')">
    <div hover-class='hover'>{{community.label}}</div>
  </picker>
</div>
<div class="list-input between">
  <span>讲座日期</span>
  <picker mode='date' :value='date' @change="pickerConfirm($event,'date')">
    <div hover-class='hover' :class="{unset:date==''}">{{date || '点击选择'}}</div>
  </picker>
</div>
<div class="list-input between">
  <span>讲座时间</span>
  <picker mode='time' :value='time' @change="pickerConfirm($event,'time')">
    <div hover-class='hover' :class="{unset:time==''}">{{time || '点击选择'}}</div>
  </picker>
</div>
<div class='greytitle'>讲座内容</div>
<div class='textarea'>
  <textarea placeholder='请输入讲座内容' name='content' :value='content' @input='content=service.Input($event)' maxlength=10000 />
  <div>{{content.length}}/10000</div>
</div>
<div class='commit'>
  <button form-type='submit' hover-class='fade' :disabled='commiting'>提交</button>
</div>
</form>
</div>
</template>

<script>
export default {
  data() {
    return {
      street: {
        columnOne: [],
        index: 0,
        arrStruct: [],
        label:'点击选择街道'
      },
      community: {
        columnOne: [],
        index: 0,
        arrStruct: [],
        label:'点击选择社区'
      },
      name: '',
      content: '',
      date: '',
      time: '',
      commiting: false
    }
  },
  methods: {
    commit(e, type='first') {
      if(this.commiting){
        return
      }
      console.log(e)
      let inputs = this.service.filter(e.mp.detail.value)
      if(!inputs.allowed){
        return
      }else{
        inputs = inputs.obj
      }
      const street = this.street.label.replace('点击选择街道','')
      const community = this.community.label.replace('点击选择社区','')
      if(!this.service.checkEmptyInput([inputs.name,street,community,this.date,this.time,inputs.content],['讲座标题','街道','社区','讲座日期','讲座时间','讲座内容'])){
        return
      }
      if(!this.service.checkBadWords([inputs.name,inputs.content],['标题','内容'])){
        return
      }
      if(type=='first'){
        wx.showModal({
          title:'',
          content: '确定要发布吗?',
          success: res => {
            if (res.confirm){
              this.execCommit(e, inputs)
            }
          }
        })
      }else{
        this.execCommit(e, inputs)
      }
    },
    execCommit(e,inputs){
      wx.showLoading({title:'正在上传'})
      this.commiting = true
      wx.request({
        url: this.rootUrl + '/lecture/addlect',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        method: 'POST',
        data: {
          scid: this.community.arrStruct[this.community.index].value,
          sessionID: wx.getStorageSync('sessionID'),
          lecture_title: inputs.name,
          lecture_content: inputs.content,
          lecture_date: `${this.date} ${this.time}:00`
        },
        success: res => {
          if(res.statusCode=='500'){
            console.log('eeee')
            this.service.getUnionId(this.rootAvatar,this.rootUrl).then(res=>{
                console.log(res)
                this.commiting = false
                this.commit(e,'again')
            })
          }else{
            console.log(res)
            if(res.data === 1){
              wx.hideLoading()
              wx.showModal({
                title: '发布成功',
                content: '请在我的讲座中查看',
                showCancel: false,
                success: res => {
                  if(res.confirm){
                    this.name = ''
                    this.date = ''
                    this.time = ''
                    this.content = ''
                    this.$set(this.street,'index',0)
                    this.$set(this.street,'label','点击选择街道')
                    this.$set(this.community,'label','点击选择社区')
                    this.commiting = false
                  }
                }
              })
            }else{
              wx.hideLoading()
              wx.showToast({title:'上传失败 请检查上传内容',icon: 'none'})
              this.commiting = false
            }
          }
        },
        fail: res=>{
          wx.hideLoading()
          wx.showToast({title:'上传失败',icon: 'none'})
          this.commiting = false
        }
      })
    },
    warning(){
      if(this.street.label=='点击选择街道'){
        wx.showToast({
          title:'请先选择街道',
          icon: 'none'
        })
      }
    },
    pickerConfirm(e, s) {
      switch (s) {
        case 'time': this.time = e.mp.detail.value;break
        case 'date': this.date = e.mp.detail.value;break
        case 'street': {
          const oldLabel = this.street.label
          this.street.index = e.mp.detail.value
          this.street.label = this.street.columnOne[this.street.index]
          if(oldLabel != this.street.label){
            this.getCommunity()
          }
          break
        }
        case 'community': {
          this.community.index = e.mp.detail.value
          this.community.label = this.community.columnOne[this.community.index]
          break
        }
      }
    },
    getCommunity(){
      wx.showLoading({ title: '加载社区列表' })
      wx.request({
        url: this.rootUrl + '/comm/getbyid/' + this.street.arrStruct[this.street.index].value,
        success: res => {
          this.community.columnOne = res.data.map(v=>v.comm_Name)
          this.community.arrStruct = res.data.map(v=>{return {label:v.comm_Name,value:v.scid}})
          this.community.index = 0
          this.community.label = this.community.columnOne[0]
          wx.hideLoading()
        }
      })
    }
  },
  onLoad() {
    wx.request({
      url: this.rootUrl + '/street/all',
      success: res => {
        this.street.columnOne = res.data.map(v => v.street_Name)
        this.street.arrStruct = res.data.map(v => {
          return { label: v.street_Name, value: v.ssid }
        })
      }
    })
  }
}
</script>
<style lang="stylus" scoped>
.baseinfo
  position absolute
  Height_Width(100%)
  Background()
  .baseinfo-box
    Background()
.textarea
  padding 0 30rpx 30rpx 30rpx
  Font(30rpx, 40rpx)
  background-color #fff
  textarea
    height 200rpx
    width 100%
    border-radius 10rpx
    background #eee
    box-sizing border-box
  div
    margin-top: 15rpx;
    text-align: right;
    color: #888;
    font-size: 28rpx;
</style>

