<template>
  <view>
    <cu-custom bgColor="bg-gradual-blueness" :isBack="true">
      <block slot="backText">返回</block>
      <block slot="content">隐患管理</block>
    </cu-custom>
    <view class="form">
      <view class="wui-form-list">
        <view class="de-drawer">
          <view class="cu-bar bg-white" v-if="hiddenForm.hiddenImgs">
            <view class="action">
              隐患照片
            </view>
          </view>
          <view class="cu-form-group" v-if="hiddenForm.hiddenImgs">
            <view class="grid col-4 grid-square flex-sub">
              <view class="bg-img" v-for="(item,index) in hiddenForm.hiddenImgs" :key="index" @tap="ViewImage"
                    :data-id="item.IMGFILES_ID" data-type="0" :data-url="hiddenForm.hiddenImgs[index].FILEPATH">
                <image :src="baseImgPath + hiddenForm.hiddenImgs[index].FILEPATH" mode="aspectFill"></image>
              </view>
            </view>
          </view>
          <view class="cu-bar bg-white" v-if="hiddenForm.hiddenVideos">
            <view class="action">
              隐患视频
            </view>
          </view>
          <view class="cu-form-group" v-if="hiddenForm.hiddenVideos">
            <view class="grid col-4 grid-square flex-sub">
              <view class="bg-img" v-for="(item,vindex) in hiddenForm.hiddenVideos" :key="vindex"
                    style="background-color: #000">
                <image src="/static/icon-apps/video.png" mode="aspectFill" @click="playVideo"
                       :data-src="hiddenForm.hiddenVideos[vindex].FILEPATH"></image>
              </view>
            </view>
          </view>
          <view class="cu-form-textarea">
            <view class="cu-form-title text-hui">隐患描述</view>
            <textarea maxlength="-1" disabled v-model="hiddenForm.HIDDENDESCR" placeholder="请对隐患进行详细描述(必填项)"></textarea>
          </view>
          <view class="cu-form-group">
            <view class="title">隐患部位</view>
            <view class="picker-tree-box">
              <view>{{ hiddenForm.HIDDENPART }}</view>
            </view>
          </view>
          <view class="cu-form-group">
            <view class="title">隐患级别</view>
            <view class="picker-tree-box">
              <view>{{ hiddenForm.HIDDENLEVEL_NAME }}</view>
            </view>
          </view>
          <view class="cu-form-group">
            <view class="title">隐患类型</view>
            <view class="picker-tree-box">
              <view>{{ hiddenForm.HIDDENTYPE_NAME }}</view>
            </view>
          </view>
          <view class="cu-form-group">
            <view class="title">隐患处置：</view>
            <view>限期整改</view>
          </view>
          <view class="cu-form-group">
            <view class="title">整改期限：</view>
            <view>{{ hiddenForm.RECTIFICATIONDEADLINE }}</view>
          </view>
          <view class="cu-form-group">
            <view class="title" style="height: auto;">
              <view>整改部门：</view>
            </view>
            <view class="picker-tree-box">
                {{ hiddenForm.RECTIFICATIONDEPT_NAME }}
            </view>
          </view>
          <view class="cu-form-group">
            <view class="title">整改人：</view>
            <view>{{ hiddenForm.RECTIFICATIONOR_NAME }}</view>
          </view>
          <view class="cu-form-group">
            <view class="title">整改时间：</view>
            <view>{{ hiddenForm.RECTIFICATIONTIME }}</view>
          </view>
          <view class="cu-bar bg-white">
            <view class="action">
              整改照片
            </view>
          </view>
          <view class="cu-form-group">
            <view class="grid col-4 grid-square flex-sub">
              <view class="bg-img" v-for="(item,index) in hiddenForm.zgImgs" :key="index" @tap="ViewImage1"
                    :data-id="item.IMGFILES_ID" data-type="0" :data-url="hiddenForm.zgImgs[index].FILEPATH">
                <image :src="baseImgPath + hiddenForm.zgImgs[index].FILEPATH" mode="aspectFill"></image>
              </view>
            </view>
          </view>
          <view class="cu-form-group">
            <view class="title">验收时间：</view>
            <ruiDatePicker v-if="TabCur == 1" :start="todayDate" fields="minute" :value="CHECKTIME || '请选择'"
                           @change="changeDiscoverDate"></ruiDatePicker>
            <view v-else>{{hiddenForm.CHECKTIME}}</view>
          </view>
          <view class="cu-bar bg-white">
            <view class="action">
              验收照片
            </view>
            <view class="action">
              {{hiddenForm.ysImgs.length}}/4
            </view>
          </view>
          <view class="cu-form-group">
            <view class="grid col-4 grid-square flex-sub">
              <view class="bg-img" v-for="(item,index) in hiddenForm.ysImgs" :key="index" @tap="ViewImage2" :data-id="item.IMGFILES_ID" data-type="0" :data-url="hiddenForm.ysImgs[index].FILEPATH">
                <image v-if="item.IMGFILES_ID" :src="baseImgPath + hiddenForm.ysImgs[index].FILEPATH" mode="aspectFill"></image>
                <image v-else :src="hiddenForm.ysImgs[index].FILEPATH" mode="aspectFill"></image>
                <view class="cu-tag bg-red" @tap.stop="DelImg" data-type="0" :data-index="index" v-if="TabCur == 1">
                  <text class='cuIcon-close'></text>
                </view>
              </view>
              <view class="solids" @tap="ChooseImage(0)" v-if="hiddenForm.ysImgs.length<4 && TabCur == 1">
                <text class='cuIcon-cameraadd'></text>
              </view>
            </view>
          </view>
<!--          <view class="cu-form-group">
            <view class="title">是否进行罚款：</view>
            <radio-group @change="changeRadioGroup($event)">
              <label class="radio"><radio value="1" :checked="hiddenForm.ISPUNISH === '1'" :disabled="TabCur == 2" />是</label>
              <label class="radio"><radio value="2" :checked="hiddenForm.ISPUNISH === '2'" :disabled="TabCur == 2" />否</label>
            </radio-group>
          </view> -->
        </view>
      </view>
      <view class="cu-bar btn-group" style="margin-top: 30upx;">
        <button v-if="TabCur == 1" class="cu-btn bg-blue margin-tb-sm lg" @click="$noMultipleClicks(goSubmit)">验收</button>
        <button v-if="TabCur == 2" class="cu-btn bg-green margin-tb-sm lg" @click="goback">返回</button>
      </view>
    </view>
    <view :class="['cu-modal',{'show':modalShow}]" v-if="modalShow">
      <view class="cu-dialog">
        <view class="cu-bar bg-white justify-end">
          <view class="content">播放视频</view>
          <view class="action" @tap="modalShow = false">
            <text class="cuIcon-close text-red"></text>
          </view>
        </view>
        <video :src="videoSrc" :autoplay="true"></video>
        <view class="cu-bar bg-white justify-end">
          <view class="action">
            <button class="cu-btn line-green text-green" @click="modalShow = false">关闭</button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import {
  basePath, corpinfoId, deptId, loginUser, formatDate, loginSession, baseImgPath, loginUserId
} from '@/common/tool.js';
import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue'

export default {
  components: {
    ruiDatePicker,
  },
  data() {
    return {
      baseImgPath,
      noClick:true,
      HIDDEN_ID: '',
      OUTSOURCED_ID: '',
      TabCur: '',
      hiddenForm: {
        ysImgs:[],
        CHECKTIME :formatDate(new Date(), 'yyyy-MM-dd hh:mm')
      },
      videoSrc:'',
      modalShow:false,
      todayDate:formatDate(new Date(), 'yyyy-MM-dd hh:mm'),
      CHECKTIME: formatDate(new Date(), 'yyyy-MM-dd hh:mm')
    }
  },
  onLoad(e) {
    this.HIDDEN_ID = e.HIDDEN_ID
    this.OUTSOURCED_ID = e.OUTSOURCED_ID
    this.TabCur = e.TabCur
    this.getData()
  },
  methods: {
    getData() {
      uni.showLoading({
        title: "加载中..."
      });//加载中动画
      uni.request({
        url: basePath + '/app/keyprojectcheck/findHidden',
        method: 'POST',
        dataType: 'json',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          HIDDEN_ID: this.HIDDEN_ID,
          OUTSOURCED_ID: this.OUTSOURCED_ID,
        },
        success: (res) => {
          uni.hideLoading();//结束加载中动画
          if (res.data != null) {
            this.hiddenForm = res.data.pd
          }
        }
      })
    },
    ViewImage(e) {
      let files = [];
      for (var i = 0; i < this.hiddenForm.hiddenImgs.length; i++) {
        files.push(this.baseImgPath + this.hiddenForm.hiddenImgs[i].FILEPATH)
      }
      uni.previewImage({
        urls: files,
        current: this.baseImgPath + e.currentTarget.dataset.url
      });
    },
    ViewImage1(e) {
      let files = [];
      for (var i = 0; i < this.hiddenForm.zgImgs.length; i++) {
        files.push(this.baseImgPath + this.hiddenForm.zgImgs[i].FILEPATH)
      }
      uni.previewImage({
        urls: files,
        current: this.baseImgPath + e.currentTarget.dataset.url
      });
    },
    playVideo(e) {
      this.videoSrc = this.baseImgPath + this.hiddenForm.hiddenVideos[0].FILEPATH
      this.modalShow = true
    },
    changeDiscoverDate(e) {
      this.CHECKTIME = e
      this.$forceUpdate();//强制刷新
    },
    ViewImage2(e) {
        let files =[];
        for(var i=0;i<this.hiddenForm.ysImgs.length;i++){
          if(e.currentTarget.dataset.id){
            files.push(this.baseImgPath + this.hiddenForm.ysImgs[i].FILEPATH)
          }else {
            files.push(this.hiddenForm.ysImgs[i].FILEPATH)
          }
        }
        uni.previewImage({
          urls: files,
          current: e.currentTarget.dataset.id ? this.baseImgPath + e.currentTarget.dataset.url : e.currentTarget.dataset.url
        });
    },
    DelImg(e) {
      var _this = this;
      let i=e.currentTarget.dataset.index
      uni.showModal({
        title: '秦港-双基双控',
        content: '确定要删除这张图片吗？',
        cancelColor:"#000000",
        cancelText: '取消',
        confirmText: '确定',
        success: res => {
          if (res.confirm) {
            this.hiddenForm.ysImgs.splice(e.currentTarget.dataset.index, 1)
          }
        }
      })
    },
    ChooseImage(e) {
      var _this = this;
      var ss=4-this.hiddenForm.ysImgs.length;
      uni.chooseImage({
        count: ss, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera','album'], //从相册选择
        success: (res) => {
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            if (e == 0) {
              let img = {};
              img.IMGFILES_ID = '';
              img.FILEPATH = res.tempFilePaths[i];
              this.hiddenForm.ysImgs.push(img)
            } else {
              let img = {};
              img.IMGFILES_ID = '';
              img.FILEPATH = res.tempFilePaths[i];
              this.imgList1.push(img)
            }
          }
        }
      });
    },
    changeRadioGroup(e){
      if(this.TabCur == 2) return
      this.hiddenForm.ISPUNISH = e.detail.value
    },
    goSubmit(){
      console.log(this.CHECKTIME)
      if(!this.CHECKTIME){
        uni.showToast({
          title: `请选择验收时间`,
          icon: 'none'
        })
        return
      }
      if(this.hiddenForm.ysImgs.length === 0){
        uni.showToast({
          title: `请上传验收图片`,
          icon: 'none'
        })
        return
      }
      // if(!this.hiddenForm.ISPUNISH){
      //   uni.showToast({
      //     title: `请选择是否进行罚款`,
      //     icon: 'none'
      //   })
      //   return
      // }
      var _this = this;
      _this.$set(this.hiddenForm,'CHECKTIME', this.CHECKTIME);
      uni.request({
        url: basePath + '/app/keyprojectcheck/check',
        method: 'POST',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          CORPINFO_ID: corpinfoId,
          ..._this.hiddenForm,
          CREATOR: loginUserId,
          OPERATOR: loginUserId,
          HIDDEN_ID: _this.HIDDEN_ID,
        },
        success: (res) => {
          if ("success" == res.data.result) {
            _this.uploadImg(this.hiddenForm.ysImgs)
          } else {
            uni.showToast({
              title: res.data.msaesge,
              duration: 2000
            });
          }
        }
      });
    },
    uploadImg(hiddenList){
      var _this = this;
      uni.showLoading({
        title: '上传中'
      })
      for (let i = 0; i < hiddenList.length; i++) {
        uni.uploadFile({
          url: basePath+'/app/imgfiles/add',
          filePath: hiddenList[i].FILEPATH,
          name: 'FFILE',
          formData: {
            'TYPE': 5,
            'FOREIGN_KEY': _this.HIDDEN_ID
          },
          success: (res) => {
            uni.hideLoading();
          },
          fail: (err) => {
            uni.hideLoading();
            uni.showModal({
              content: err.errMsg,
              showCancel: false
            });
          }
        })
      }
      _this.goback()
    },
    goback(){
      uni.navigateBack({delta: 1});
      uni.hideLoading();
    },
  }
}
</script>

<style scoped>

</style>
