<template>
  <uni-section title="问题新增" type="line">
    <view class="example">
      <uni-forms ref="customForm" :rules="customRules" :modelValue="form" class="form">
        <uni-forms-item label="标题" required name="title">
          <uni-easyinput v-model="form.title" placeholder="请输入标题" maxlength="50"/>
        </uni-forms-item>
        <uni-forms-item label="正文" required name="text">
		  <uni-easyinput type="textarea" v-model="form.text" :maxlength="1000" @blur="binddata('textDetails', $event.detail.value)" placeholder="请输入正文"></uni-easyinput>
        </uni-forms-item>
		
        <uni-forms-item label="提问类型" required name="typeId" class="askType">
		  <uni-data-select
			  v-model="form.typeId"
			  :localdata="typeList"
			></uni-data-select>
        </uni-forms-item>
        <uni-forms-item label="悬赏" required name="score" >
          <uni-number-box  v-model="form.score" :min="0" :max="99999" label="请输入悬赏积分"></uni-number-box >
        </uni-forms-item>
        <uni-forms-item label="图片" class="path" >
          <upload :imgArr="imageList" name="path" v-model="form.path" 
                  style="position: relative;left: -5%;top: -10%;"
                  @updateImg="updateImg"></upload>
        </uni-forms-item>
      </uni-forms>
      <button type="primary" @click="submitForm('customForm')" >提交</button>
    </view>
  </uni-section>
</template>

<script>
import upload from '@/components/upload/index.vue'
import {listType,addQuestion,verifyScore,getUserInfo} from '@/api/asking/addquestion.js'

export default {
  components: {
    upload
  },
  onReady() {
    // 设置自定义表单校验规则，必须在节点渲染完毕后执行
    this.$refs.customForm.setRules(this.customRules)
    this.getTypeList();
  },
  data() {
    return {
      disabled: false,
      //类型配置
      typeList: [],
      form: {},
      form1: {},
      // 表单校验
      customRules: {
        title: {
          rules: [{
            required: true,
            errorMessage: '标题不能为空'
          }]
        },
        text: {
          rules: [{
            required: true,
            errorMessage: '正文不能为空'
          }]
        },
        typeId: {
          rules: [{
            required: true,
            errorMessage: '提问类型不能为空'
          }]
        },
      },
      imageList: []
    }
  },
  methods: {
    //查询类型配置
    getTypeList() {
      listType(this.queryParams).then(response => {
		  response.data.forEach(v => {
		  	this.typeList.push({
		  		text:v.type,
		  		value:v.id
		  	})
		  })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm(e) {
		this.form.score = this.form.score==null?0:this.form.score;
		this.form1.scoreNum = this.form.score;
		this.form1.userId = getApp().globalData.userId;
		//验证积分
		verifyScore(this.form1).then(res => {
		  if(!res.data){
			  uni.showToast({
			  	title:"您的积分不足"
			  })
		  }else{
			  this.$refs[e].validate().then(res => {
				  this.form.type = this.typeList.filter((item) => {
					return this.form.typeId == item.value;
				  })[0].text;
				  //增加问题
			      addQuestion(this.form).then(res => {
			        uni.navigateBack();
			      });
			  })
		  }
		});
    },

    //图片上传
    updateImg(imgList) {
      this.imageList = imgList;
      this.form.path = this.imageList.join(',');
    }
  }
};
</script>

<style lang="scss" scoped>
	::v-deep .item--uni-forms-item__label text{
		width: fit-content;
		white-space: nowrap;
	}
	::v-deep .uni-section-content{
		padding: 3%;
	}
	
</style>
