<template>
  <view class="task_page">
    <u-skeleton v-if="skeleton" :class="{'loading': skeleton}" :animate="true" title rows="5" rows-width="92%"
    	rowsHeight="56">
    </u-skeleton>
    <block v-else>
      <view class="card">
        <view class="c_title">{{`任务名称: ${taskInfo.title||''}`}}</view>
        <view>{{taskInfo.content||''}}</view>
      </view>
      <view class="card">
        <view class="c_title">{{`学习照片 ( ${study_photo.length}/5 ) 张`}}</view>
        <view class="file">
          <view class="file_item" v-for="(item, index) in study_photo" :key="'file'+index">
            <image class="image" :src="item" @click="priview(index)"></image>
            <image v-if="taskInfo.status==2" class="del" src="/static/icons/delete.png" @click.stop="deleteFile(index)">
            </image>
          </view>
          <view v-if="taskInfo.status==2" class="file_btn" @click="chooseFile">
            <image src="/static/icons/plus.png"></image>
          </view>
          <view class="file_empty" v-for="k in placeholderLength" :key="'empty'+k"></view>
        </view>
      </view>
      <view class="card">
        <view class="c_title">{{`签到表照片 ( ${sign_in_table?1:0}/1 ) 张`}}</view>
        <view class="file">
          <view v-if="sign_in_table" class="file_item">
            <image class="image" :src="sign_in_table" @click="priviewOne(index)"></image>
            <image v-if="taskInfo.status==2" class="del" src="/static/icons/delete.png"
              @click.stop="deleteFile(index, 'sign_in_table')">
            </image>
          </view>
          <view v-if="taskInfo.status==2" class="file_btn" @click="chooseOneFile">
            <image src="/static/icons/plus.png"></image>
          </view>
          <view v-if="taskInfo.status==2" class="file_empty" v-for="k in 2" :key="'empty'+k"></view>
        </view>
      </view>
      <view class="card" style="margin-bottom: 160rpx;">
        <view class="c_title">培训内容大纲 (50字以上)</view>
        <u--textarea :disabled="taskInfo.status!=2" v-model="study_content" placeholder="请输入内容"
          placeholderStyle="font-size: 22rpx;" count maxlength="500" autoHeight
          style="font-size: 28rpx;background-color: #eee;min-height: 100px;padding-bottom: 50rpx;"></u--textarea>
      </view>
      <mybtn v-if="taskInfo.status==2" text="确认提交" @click="$u.throttle(submit, 1500)"></mybtn>
      <mybtn v-else-if="taskInfo.status==3" text="已完成" :my_btn_disabled="true" @click="showToast('任务已完成')"></mybtn>
      <mybtn v-else-if="taskInfo.status==5" text="已关闭" :my_btn_disabled="true" @click="showToast('任务已关闭!')"></mybtn>
      <!-- <mybtn v-else-if="approve_status==3" text="已驳回,请重新提交" @click="toUpdate"></mybtn> -->
      <mybtn v-else text="已提交,请等待审核" :my_btn_disabled="true" @click="showToast('请耐心等待审核结果')"></mybtn>
    </block>
  </view>
</template>

<script>
  import {
    upLoadImage,
  } from "@/api/file.js";
  import { Toast } from "../../libs/uniApi";
  import { townTaskDetails, serviceTask4Commit } from "@/api/task.js"
  export default {
    data() {
      return {
        skeleton: true,
        other: {
          annex: [], // 图片列表
          is_commit: 0, // 是否提交
          note: "", // 详情描述
          video_annex: [] // 视频列表
        },
        taskInfo: {
          id: '',
          title: '',
          content: '',
          extend: {},
          status: 0
        },
        study_photo: [],
        sign_in_table: '',
        study_content: ''
      }
    },
    onLoad(options) {
      options.task_id ? this.taskInfo.id = options.task_id : null;
      this.initTask();
    },
    onShow() {},
    computed: {
      // 占位长度
      placeholderLength() {
        if (this.taskInfo.status!=2) return this.study_photo.length % 3 == 0 ? 0 : 3 - this.study_photo.length % 3;
        return (this.study_photo.length + 1) % 3 == 0 ? 0 : 3 - (this.study_photo.length + 1) % 3;
      },
    },
    methods: {
      async initTask() {
        this.skeleton = true;
        let res = await townTaskDetails({
          id: this.taskInfo.id
        });
        this.taskInfo = res.data;
        this.sign_in_table = this.taskInfo?.extend?.town_task_type_4?.sign_in_table;
        this.study_content = this.taskInfo?.extend?.town_task_type_4?.study_content;
        this.study_photo = JSON.parse(JSON.stringify(this.taskInfo?.extend?.town_task_type_4?.study_photo || []));
        this.skeleton = false;
      },
      // 提交
      async submit() {
        if (this.study_photo.length < 5) return Toast('学习照片不能低于5张');
        if (!this.sign_in_table) return Toast('签到表照片不能为空');
        // if (this.study_content.trim().length < 50) return Toast('培训内容不得低于50字');
        await serviceTask4Commit({
          id: this.taskInfo.id,
          study_photo: this.study_photo,
          sign_in_table: this.sign_in_table,
          study_content: this.study_content
        });
        this.other.is_commit = 1;
        Toast('提交成功');
        uni.navigateBack();
      },
      showToast(str) {
        Toast(str)
      },
      chooseFile() {
        uni.chooseImage({
          count: 5,
          sizeType: ['compressed'],
          success: async (res) => {
            for (let item of res.tempFiles) {
              let ul = await upLoadImage({
                filePath: item.path,
                name: 'file'
              });
              this.study_photo.push(ul.data.uri);
            }
          }
        });
      },
      priview(index) {
        uni.previewImage({
          urls: this.study_photo,
          current: index,
          longPressActions: {
            itemList: ['删除'],
            itemColor: '#ff0000',
            success(e) {
              if (e.tapIndex == 0) this.deleteFile(e.index);
            }
          },
        })
      },
      chooseOneFile() {
        uni.chooseImage({
          count: 1,
          sizeType: ['compressed'],
          success: async (res) => {
            let ul = await upLoadImage({
              filePath: res.tempFilePaths[0],
              name: 'file'
            });
            this.sign_in_table = ul.data.uri;
          }
        });
      },
      priviewOne(index) {
        uni.previewImage({
          urls: [this.sign_in_table],
          current: index,
          longPressActions: {
            itemList: ['删除'],
            itemColor: '#ff0000',
            success(e) {
              if (e.tapIndex == 0) this.sign_in_table = '';
            }
          },
        })
      },
      // 删除已上传文件
      deleteFile(index, type = 'image') {
        if (type == 'image') this.study_photo.splice(index, 1);
        else if (type == 'sign_in_table') this.sign_in_table = '';
      },
    },
  }
</script>

<style lang="scss">
  .task_page {
    padding-top: 30rpx;

    .card {
      margin: 0 auto;
      margin-bottom: 28rpx;
      width: 694rpx;
      background-color: #fff;
      border-radius: 26rpx;
      padding: 28rpx;

      .c_title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333333;
        margin-bottom: 20rpx;
      }

      .file {
        display: flex;
        justify-content: left;
        flex-wrap: wrap;

        &_item {
          flex-shrink: 0;
          width: 200rpx;
          height: 200rpx;
          margin: 0 auto;
          margin-bottom: 16rpx;
          border: 2px solid #ccc;
          border-radius: 10rpx;
          overflow: hidden;
          position: relative;

          .image {
            width: 100%;
            height: 100%;
          }

          .del {
            position: absolute;
            height: 40rpx;
            width: 40rpx;
            top: 10rpx;
            right: 10rpx;
          }
        }

        &_btn {
          flex-shrink: 0;
          width: 200rpx;
          height: 200rpx;
          margin: 0 auto;
          margin-bottom: 16rpx;
          border: 2px solid #ccc;
          display: flex;
          justify-content: center;
          align-items: center;
          flex-direction: column;
          border-radius: 10rpx;

          image {
            width: 100rpx;
            height: 100rpx;
          }
        }

        &_empty {
          flex-shrink: 0;
          width: 200rpx;
          height: 200rpx;
          margin: 0 auto;
          margin-bottom: 16rpx;
          border: 2px solid transparent;
        }
      }
    }
    
    .loading{
      padding: 28rpx;
    }
  }
</style>