<template>
  <view class="body">
    <u-navbar :custom-back="router" title="添加心愿"></u-navbar>
    <view class="form">
      <view class="formItem">
        <view class="title">标题</view>
        <view class="input">
          <u-input v-model="title" :maxlength="20" placeholder="请输入标题，不超过20字" type="type"/>
        </view>
      </view>
      <view class="formItem">
        <view class="title">内容</view>
        <view class="input">
          <u-input v-model="content" :auto-height="true" :height="500" :maxlength="500"
                   placeholder="请输入内容，内容不超过500字"
                   type="textarea"/>
        </view>
      </view>
      <view v-if="doYouWantToEnableAnonymity" class="formItem noBorder">
        <view class="input">
          <u-checkbox-group>
            <u-checkbox v-model="anonymous">匿名</u-checkbox>
          </u-checkbox-group>
        </view>
      </view>
    </view>
    <view class="btn-box">
        <u-button type="primary" shape="circle" @click="submit" style="width: 80%;">提交</u-button>
    </view>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      title: '',
      content: '',
      anonymous: false,
      doYouWantToEnableAnonymity: false,
      schoolId:'',
    };
  },
 async onLoad() {
    await this.getSchool();
    this.$u.api.CheckNM({
      schoolId: this.schoolId
    }).then(res => {
      if (res.data=== true||res=== true) {
        
        this.doYouWantToEnableAnonymity = true;
      }else{

        this.doYouWantToEnableAnonymity = false;
      }
    })
  },
  methods: {
    router() {
      uni.navigateBack({
        delta: 1
      });
    },
            // 获取学校
    async getSchool() {
        const req = {
          userId: this.vuex_user.id,
        };
        const res = await this.$u.apiList.MyEducations(req);
        console.log(res,'res---');
        const findRow = res.find(x=>x.isSelected === true)
        if(findRow){
          this.schoolId = findRow.schoolId
        }
        console.log(this.schoolId, 'this.schoolId--')
        // this.schoolId = res.filter(function (x) {
        //   return x.isSelected === true
        // })[0]?.schoolId
    },
    submit() {
      if (this.title == '') {
        /* uni.showToast({
          title: '标题不能为空',
          icon: 'none'
        }); */
        this.$refs.uToast.show({
          title: '标题不能为空',
          type: 'error'
        });
        return;
      }
      if (this.content == '') {
        /* uni.showToast({
          title: '内容不能为空',
          icon: 'none'
        }); */
        this.$refs.uToast.show({
          title: '内容不能为空',
          type: 'error'
        });
        return;
      }
      uni.showLoading({
        title: '提交中'
      });

      this.$u.api.PushWishDan({
        userId: this.vuex_user.id,
        title: this.title,
        context: this.content,
        isNM: this.anonymous,
        // schoolId: this.vuex_user.schoolId
        schoolId: this.schoolId
      }).then(res => {
        uni.hideLoading();
       /*  uni.showToast({
          title: '提交成功',
          icon: 'none'
        }); */
        this.$refs.uToast.show({
          title: '提交成功',
          type: 'success'
        });
        setTimeout(() => {
          uni.navigateBack({
            delta: 1
          });
        }, 1000);

      }).catch(e=>{
        /* uni.showToast({
          title: e.error || '提交失败',
          icon: 'none'
        }); */
        this.$refs.uToast.show({
          title: e.error || '提交失败',
          type: 'error'
        });
      })
    }
  }
}
</script>

<style lang="scss">
.body {
  height: 100vh;
  background-color: #FFFFFF;
}

.form {
  padding: 0 24rpx;

  .formItem {
    padding: 24rpx 0 0;
    border-bottom: 0.01rem solid #EEEEEE;

    .title {
      font-weight: 400;
      font-size: 0.16rem;
      color: #000000;
      line-height: 0.24rem;
    }

    .input {
      font-weight: 400;
      font-size: 0.16rem;
      color: rgba(0, 0, 0, 0.4);
    }
  }
}

.noBorder {
  border: none !important;
}
// 按钮
.btn-box {
  position: fixed;
  width: 100%;
  bottom: 0;
  height: 150rpx;
  border-top: 1px solid #f1f2f6;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
}
</style>
