<template>
  <view style="height: 100%">
    <custom-nav-bar position="fixed" color="black" left-text="评价" left-arrow />

    <view class="appraise flex flex-v-center">
      <view class="mr-25">满意度：</view>
      <van-rate :value="form.appraiseStar" :size="22" gutter="36rpx" icon="/images/void-icon2.png"
        void-icon="/images/rate-icon.png" @change="headleChange" />
    </view>

    <view class="division"></view>

    <!-- 描述 -->
    <view class="describe">
      <view>描述</view>
      <textarea class="textarea" placeholder="请输入" @input="headleInput" maxlength="300" :value="form.title"
        data-name="appraiseContent" focus auto-height></textarea>
      <view class="form-image">
        <van-uploader @after-read="afterRead" :file-list="fileList" :deletable="true" max-count="9"
          @delete="imageDelete">
          <image class="camera" src="/static/images/icon9.png"></image>
        </van-uploader>
      </view>
    </view>

    <view class="van-button">
      <van-button v-if="form.appraiseContent === '' && form.appraiseStar === 0"
        custom-style="color:#000;background-color:#D9E2EE;border-radius: 8rpx;" block>
        提交评价
      </van-button>
      <van-button @tap.native="submit" v-if="form.appraiseContent !== '' || form.appraiseStar !== 0"
        custom-style="color:#fff;background-color:#FE3B53;border-radius: 8rpx;" block>
        提交评价
      </van-button>
    </view>
  </view>
</template>

<script>
  // const app = getApp();
  // const { peopleApi } = app.globalData.api;
  import * as peopleApi from '@/api/peopleApi';
  import * as publicApi from '@/api/publicApi';
  export default {
    components: {},
    data() {
      return {
        // 上传图片所需
        appealEnclosure: [],

        fileList: [],
        options: null,

        form: {
          appraiseContent: '',
          appraiseStar: 0,
          title: ''
        },

        name: ''
      };
    },
    onLoad(options) {
      this.setData({
        options
      });
    },
    methods: {
      headleChange(event) {
        this.setData({
          ...this.form,
          ['form.appraiseStar']: event.detail
        });
      },

      // 上传图片
      async afterRead(event) {
        const {
          file
        } = event.detail;
        const res = await getApp().globalData.upload({
          file
        });
        if (res.code !== 0) {
          uni.showToast({
            title: '上传图片失败',
            icon: 'none'
          });
          return false;
        }
        const {
          fileList
        } = this;
        const {
          appealEnclosure
        } = this;
        const url = res.data;
        fileList.push({
          url
        });
        appealEnclosure.push(url);
        this.setData({
          fileList,
          appealEnclosure
        });
      },

      // 清楚对应的image图片
      imageDelete(event) {
        const {
          index
        } = event.detail;
        const Image = this.fileList;
        Image.splice(index, 1);
        this.setData({
          fileList: Image
        });
      },

      headleInput(event) {
        const {
          value
        } = event.detail;
        const {
          name
        } = event.currentTarget.dataset;
        this.form[name] = value;
        this.setData({
          ...this.form,
          [name]: this[name]
        });
      },

      // 提交
      async submit() {
        const {
          appraiseContent,
          appraiseStar
        } = this.form;
        if (appraiseStar === 0 || appraiseContent === '') {
          let toastTitle = '';
          if (appraiseStar === 0) {
            toastTitle = '请输入满意度不能为空';
          } else if (appraiseContent === '') {
            toastTitle = '请输入描述';
          } else {
            toastTitle = '带*属于必填项';
          }
          uni.showToast({
            title: toastTitle,
            icon: 'none'
          });
          return false;
        }
        let params = {
          id: this.options.id,
          appraiseContent: appraiseContent,
          appraiseStar: appraiseStar,
          appraiseImage: this.appealEnclosure
        };
        console.log(params);
        const res = await peopleApi.CrateAppraiseApi(params);
        if (res.code !== 0) {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
          return;
        }
        uni.showToast({
          title: '评论成功',
          icon: 'none'
        });
        uni.redirectTo({
          url: '/packages/people/pages/masses/masses'
        });
      }
    }
  };
</script>
<style lang="less">
  .appraise {
    padding: 60rpx 40rpx;
    font-size: 32rpx;
    color: #333333;
    font-weight: 400;
    line-height: 32rpx;
  }

  .mr-25 {
    margin-right: 25rpx;
  }

  .division {
    height: 20rpx;
    background: #f9f9f9;
  }

  .describe {
    background: #ffffff;
    border-radius: 16px;
    padding: 40rpx 40rpx 150rpx 40rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: #333333;

    .textarea {
      padding: 24rpx 0;
      padding-top: 20rpx;
    }

    .form-image {
      margin: 20rpx 0rpx 50rpx 0;
      display: flex;
      align-items: center;
      flex-wrap: wrap;
    }

    .form-image>view {
      width: 31.3%;
      position: relative;
      margin-right: 13.5rpx;
    }

    .form-image>view>image:nth-child(1) {
      width: 100%;
      height: 200rpx;
      border-radius: 10rpx;
    }

    .form-image>view>image:nth-child(2) {
      position: absolute;
      top: -0rpx;
      right: 0rpx;
      width: 40rpx;
      height: 40rpx;
    }

    .camera {
      width: 160rpx;
      height: 160rpx;
      border-radius: 8rpx;
    }
  }

  .van-button {
    width: 100%;
    position: fixed;
    bottom: 0;
    z-index: 99;
    background-color: #fff;
    padding: 20px 50rpx 50rpx 50rpx;
  }
</style>