import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { AtTextarea }  from 'taro-ui'
import LFormGroup from '@/c/lform/formGroup'
import './add.scss'

class Index extends Component {
  constructor (props) {
    super(props)
    this.state = {
      question_cont: '',
      formObj: {},
    }
  }
  config = {
    navigationBarTitleText: '我要提问',
  }

  componentWillMount () {}

  commentChange (question_cont) {
    this.setState({
      question_cont
    })
  }

  saveHandle () {
    const { question_cont, formObj } = this.state
    if (question_cont && formObj.question_tag) {
      Taro.api.news.apiquestionadd({
        question_tag: formObj.question_tag.join(','),
        question_cont: question_cont,
      }).then(res => {
        Taro.$msgConfirm('提交成功~', () => {
          Taro.redirectTo({
            url: '/pagesMore/news/index?nav=1'
          })
        }, () => {
          Taro.redirectTo({
            url: '/pagesMore/news/index?nav=1'
          })
        })
      })
    } else {
      Taro.$msg('请输入提问的类型和内容')
    }
  }

  baseFormChange (key, val) {
    let { formObj } = this.state
    formObj[key] = val
    this.setState({
      formObj
    })
  }

  render () {
    const { question_cont, formObj } = this.state
    const dictData = Taro.getStorageSync('dictData')
    const questionTagMoreOptions = {arr: dictData.question_tag}
    return (
      <View className="l-box">
        <View className="l-floor-pos">
          <LFormGroup
            val={formObj.question_tag}
            moreRows={true}
            defineContentClassName="border2"
            valStr="question_tag"
            keyStr="问题类型"
            typeStr="multiSelect"
            moreOptions={questionTagMoreOptions}
            bc={this.baseFormChange.bind(this)}
          />
        </View>
        <View className="comment-box">
          <View className="cb-title">提问内容</View>
          <AtTextarea
            value={question_cont}
            onChange={this.commentChange.bind(this)}
            maxLength={300}
            height={300}
            placeholder='你想问的...'
          />
        </View>
        <Navigator url={'/pagesMore/news/index?nav=1'} className="scoped-more">先看看其它人说些啥{'>'}</Navigator>
        <View className="l-floor-footer t2">
          <View className="lff-flex">
            <View className="lff-btn full" onClick={this.saveHandle.bind(this)}>提交问题</View>
          </View>
        </View>
      </View>
    )
  }
}

export default Index
