<template>
  <view class="content">
    <view class="card">
      <u-cell-group :border="false">
        <u-cell>
          <view slot="title" class="title required">检查题目：</view>
          <view slot="value">
            <u-radio-group v-model="form.INSPECTION_SUBJECT" placement="row" disabled>
              <u-radio label="安全" name="安全"></u-radio>
              <u-radio label="环保" name="环保" class="ml-10"></u-radio>
              <u-radio label="综合" name="综合" class="ml-10"></u-radio>
            </u-radio-group>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">被检查单位：</view>
          <view slot="value">
            <text>{{ form.INSPECTED_CORPINFO_ID_NAME }}</text>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">被检查单位现场负责人：</view>
          <view slot="value">
            <text>{{ form.INSPECTED_SITEUSER_NAME }}</text>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">检查场所：</view>
          <view slot="value">
            <u--input v-model="form.INSPECTION_PLACE" border="none" placeholder="请输入检查场所" inputAlign="right"
                      disabled></u--input>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">检查类型：</view>
          <view slot="value">
            <text>{{ form.INSPECTION_TYPE_NAME }}</text>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">检查开始时间：</view>
          <view slot="value">
            <text>{{ form.INSPECTION_TIME_START }}</text>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">检查结束时间：</view>
          <view slot="value">
            <text>{{ form.INSPECTION_TIME_END }}</text>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">记录填写时间：</view>
          <view slot="value">
            <text>{{ form.CREATTIME }}</text>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">检查情况：</view>
          <view slot="label" class="title">
            <view class="pr mt-10" v-for="(item,index) in form.situationList" :key="item.id">
              <u--textarea v-model="item.SITUATION" placeholder="请输入检查情况" disabled></u--textarea>
              <!-- 检查情况图片上传 -->
              <view class="mt-10">
                <view class="title mb-10">图片：</view>
                <u-upload
                    :fileList="item.IMAGE"
                    :disabled="true"
                    :deletable="false"
                    multiple
                    :maxCount="1">
                </u-upload>
              </view>
            </view>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">检查人员：</view>
        </u-cell>
        <view class="title-none">
          <u-cell>
            <view slot="value" style="width: 100%">
              <view class="pr mt-10" v-for="(item,index) in form.inspectorList" :key="item.id">
                <u-cell-group :border="false" class="border">
                  <view class="title-show">
                    <u-cell>
                      <view slot="title">{{ index + 1 }}.检查人员单位</view>
                      <view slot="value">
                        <text>{{ item.INSPECTION_DEPARTMENT_NAME }}</text>
                      </view>
                    </u-cell>
                  </view>
                  <view class="title-show">
                    <u-cell :border="false">
                      <view slot="title">{{ index + 1 }}.检查人员</view>
                      <view slot="value">
                        <text>{{ item.INSPECTION_USER_NAME }}</text>
                      </view>
                    </u-cell>
                  </view>
                </u-cell-group>
              </view>
            </view>
          </u-cell>
        </view>
        <u-cell>
          <view slot="title" class="title">发现问题 </view>
        </u-cell>
        <view class="title-none">
        <u-cell>
          <view slot="value" style="width: 100%">
            <uni-table border stripe emptyText="暂无更多数据">
              <uni-tr>
                <uni-th align="center">序号</uni-th>
                <uni-th align="center">隐患部位</uni-th>
                <uni-th align="center">隐患描述</uni-th>
                <uni-th align="center">操作</uni-th>
              </uni-tr>

              <uni-tr v-for="(item,index) in form.hiddenList" :key="item.HIDDEN_ID">
                <uni-td>{{ index + 1 }}</uni-td>
                <uni-td>{{ item.HIDDENPART_NAME ? item.HIDDENPART_NAME : item.HIDDENPART }}</uni-td>
                <uni-td>{{ item.HIDDENDESCR }}</uni-td>
                <uni-td>
                  <view class="flex-between">
                    <u-icon name="eye-fill" @tap="showHidden(item, index)"></u-icon>
                  </view>
                </uni-td>
              </uni-tr>
            </uni-table>
          </view>
        </u-cell>
        </view>
        <template v-if="type === 'verify'">
          <u-cell>
            <view slot="title" class="title">检查人意见：</view>
            <view slot="label" class="mt-10">
              <u--textarea v-model="form.INSPECTION_USER_OPINION" placeholder="请输入检查人意见"></u--textarea>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title flex-between">
              签字：
            </view>
            <view slot="value">
              <u-button type="primary" text="手写添加" size="mini" @click="signShow = true"></u-button>
            </view>
            <view slot="label" class="mt-10">
              <u--image v-show="form.INSPECTION_USER_SIGN_IMG" :showLoading="true" :src="form.INSPECTION_USER_SIGN_IMG"
                        width="100%" height="80px" mode="scaleToFill"
                        @click="previewImage(form.INSPECTION_USER_SIGN_IMG)"></u--image>
              <sign v-if="signShow" :signShow.sync="signShow" @confirm="signConfirm"></sign>
            </view>
          </u-cell>
          <u-cell :border="false">
            <view slot="label" class="flex-between">
              <u-button type="primary" class="bth" text="通过" @click="$u.debounce(fnSubmit(1), 1000,true)"></u-button>
              <u-button type="error" class="bth ml-10" text="不通过"
                        @click="$u.debounce(fnSubmit(-1), 1000,true)"></u-button>
            </view>
          </u-cell>
        </template>
        <view v-for="(item, index) in esipList" :key="index">
          <template v-if="item.INSPECTION_USER_SIGN_IMG">
            <u-cell>
              <view slot="title" class="title">检查人意见：</view>
              <view slot="label" class="mt-10">
                <text>{{ item.INSPECTION_USER_OPINION }}</text>
              </view>
            </u-cell>
            <u-cell>
              <view slot="title" class="title">签字：</view>
              <view slot="label" class="mt-10">
                <u--image :showLoading="true" mode="scaleToFill"
                          :src="$store.state.filePath + item.INSPECTION_USER_SIGN_IMG" width="100%" height="80px"
                          @click="previewImage($store.state.filePath + item.INSPECTION_USER_SIGN_IMG)"></u--image>
              </view>
            </u-cell>
            <u-cell>
              <view slot="title" class="title">审核状态：</view>
              <view slot="value">
                <text>同意</text>
              </view>
            </u-cell>
          </template>
        </view>
      </u-cell-group>
    </view>
    <u-popup :show="popupShow" @close="popupShow = false" mode="right" :closeOnClickOverlay="false"
             :customStyle="{width:'85vw',overflowY:'auto'}">
      <view class="card">
        <u-cell-group :border="false">
          <u-cell>
            <view slot="title" class="title required">隐患照片：</view>
            <view slot="label" class="mt-10">
              <u-upload
                  disabled
                  deletable
                  uploadIcon="plus"
                  :fileList="hiddenForm.hiddenImgs"
                  multiple
                  :maxCount="4"
              ></u-upload>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title flex-between">
              隐患视频：
              <u-icon v-if="hiddenForm.hiddenVideos.length !== 0 && hiddenForm.hiddenVideos[0].url"
                      name="play-circle-fill" size="30" @click="previewVideo(hiddenForm.hiddenVideos[0].url)"></u-icon>
            </view>
            <view slot="label" class="mt-10">
              <u-upload
                  disabled
                  deletable
                  accept="video"
                  uploadIcon="plus"
                  :fileList="hiddenForm.hiddenVideos"
                  multiple
                  :maxCount="1"
              ></u-upload>
              <u-modal :show="modalShow" title="视频播放" confirmText="关闭" @confirm="modalShow = false">
                <view>
                  <video v-if="modalShow" :src="videoUrl" :autoplay="true"></video>
                </view>
              </u-modal>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患描述：</view>
            <view slot="label" class="mt-10">
              <u--textarea v-model="hiddenForm.HIDDENDESCR" placeholder="请输入隐患描述" disabled></u--textarea>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患部位：</view>
            <view slot="value">
              <text>{{ hiddenForm.HIDDENPART_NAME ? hiddenForm.HIDDENPART_NAME : hiddenForm.HIDDENPART }}</text>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患级别：</view>
            <view slot="value">
              <text>{{ hiddenForm.HIDDENLEVEL_NAME }}</text>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">是否是相关方：</view>
            <view slot="value">
              <u-radio-group v-model="hiddenForm.ISRELEVANT" placement="row" disabled="false">
                <u-radio label="是" name="1" class="ml-10"></u-radio>
                <u-radio label="否" name="2" class="ml-10"></u-radio>
              </u-radio-group>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患类型：</view>
            <view slot="value">
              <text>{{ hiddenForm.HIDDENTYPE_NAME }}</text>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患发现时间：</view>
            <view slot="value">
              <text>{{ hiddenForm.DISCOVERYTIME }}</text>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患上报位置：</view>
            <view slot="label" class="mt-10">
              <view>经度:{{ hiddenForm.LONGITUDE }}</view>
              <view>纬度:{{ hiddenForm.LATITUDE }}</view>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患位置描述：</view>
            <view slot="label" class="mt-10">
              <u--textarea v-model="hiddenForm.POSITIONDESC" placeholder="请输入隐患位置描述" disabled></u--textarea>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title required">隐患发现人：</view>
            <view slot="value">
              <text>{{ hiddenForm.CREATOR_NAME }}</text>
            </view>
          </u-cell>
          <u-cell :border="false">
            <view slot="label" class="flex-between">
              <u-button type="info" text="取消" @click="popupClose"></u-button>
            </view>
          </u-cell>
        </u-cell-group>
      </view>
    </u-popup>
  </view>
</template>

<script>
import {getSafetyenvironmentalGoEdit, setSafetyenvironmentalVerify} from "@/api";
import sign from "../../../components/sign/sign";

export default {
  components: {
    sign,
  },
  data() {
    return {
      form: {
        INSPECTION_SUBJECT: '',
        INSPECTED_DEPARTMENT_NAME: '',
        INSPECTED_DEPARTMENT_ID: '',
        INSPECTED_SITEUSER_NAME: '',
        INSPECTED_SITEUSER_ID: '',
        INSPECTION_PLACE: '',
        INSPECTION_TYPE_NAME: '',
        INSPECTION_TIME_START: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
        INSPECTION_TIME_END: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
        CREATTIME: '',
        situationList: [{id: Math.random(), INSPECTION_SITUATION_ID: '', SITUATION: '', IMAGE: []}],
        inspectorList: [{
          id: Math.random(),
          inspectionUserShow: false,
          INSPECTION_INSPECTOR_ID: '',
          INSPECTION_DEPARTMENT_ID: '',
          INSPECTION_DEPARTMENT_NAME: '',
          INSPECTION_USER_ID: '',
          INSPECTION_USER_NAME: ''
        }],
        hiddenList: [],
        INSPECTION_USER_OPINION: '',
        INSPECTION_USER_SIGN_IMG: ''
      },
      rules: {
        'INSPECTION_USER_OPINION': {required: true, message: '请输入检查人意见'},
        'INSPECTION_USER_SIGN_IMG': {required: true, message: '请签字'},
      },
      INSPECTION_ID: '',
      type: '',
      INSPECTION_USER_ID: '',
      INSPECTION_INSPECTOR_ID: '',
      popupShow: false,
      hiddenForm: {
        hiddenImgs: [],
        hiddenVideos: [],
        HIDDENDESCR: '',
        HIDDENPART_NAME: '',
        HIDDENPART: '',
        HIDDENLEVEL_NAME: '',
        HIDDENLEVEL: '',
        HIDDENTYPE_NAME: '',
        HIDDENTYPE: '',
        DISCOVERYTIME: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
        LONGITUDE: '',
        LATITUDE: '',
        CREATOR_NAME: '',
        CREATOR: '',
      },
      signShow: false,
      modalShow: false,
      videoUrl: '',
      esipList: [] //审核过成意见
    }
  },
  onLoad(event) {
    this.INSPECTION_ID = event.INSPECTION_ID
    this.type = event.type
    this.INSPECTION_INSPECTOR_ID = event.INSPECTION_INSPECTOR_ID
    this.INSPECTION_USER_ID = event.INSPECTION_USER_ID
    if (this.INSPECTION_ID) {
      this.fnGetSafetyenvironmentalGoEdit()
    }
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo
    }
  },
  methods: {
    async fnGetSafetyenvironmentalGoEdit() {
      let resData = await getSafetyenvironmentalGoEdit({INSPECTION_ID: this.INSPECTION_ID,'CORPINFO_ID': ''})
      for (let i = 0; i < resData.pd.hiddenList.length; i++) {
        resData.pd.hiddenList[i].id = resData.pd.hiddenList[i].HIDDEN_ID
        for (let j = 0; j < resData.pd.hiddenList[i].hiddenImgs.length; j++) {
          resData.pd.hiddenList[i].hiddenImgs[j].url = this.$store.state.filePath + resData.pd.hiddenList[i].hiddenImgs[j].FILEPATH
        }
        for (let j = 0; j < resData.pd.hiddenList[i].hiddenVideos.length; j++) {
          resData.pd.hiddenList[i].hiddenVideos[j].url = this.$store.state.filePath + resData.pd.hiddenList[i].hiddenVideos[j].FILEPATH
        }
      }
      for (let i = 0; i < resData.pd.situationList.length; i++) {
        if (resData.pd.situationList[i].IMAGE_PATH) {
          resData.pd.situationList[i].IMAGE = [{ url: this.$store.state.filePath + resData.pd.situationList[i].IMAGE_PATH}];
        }
      }
      this.form = resData.pd
      this.esipList = resData.esipList
    },
    async fnSubmit(INSPECTION_STATUS) {
      for (const key in this.rules) {
        if (!this.form[key]) {
          uni.showToast({
            title: this.rules[key].message,
            icon: "none"
          })
          return;
        }
      }
      console.log(this.form.INSPECTION_USER_SIGN_IMG)
      await setSafetyenvironmentalVerify({
        filePath: this.form.INSPECTION_USER_SIGN_IMG,
        name: 'FFILE',
        formData: {
          INSPECTION_STATUS,
          INSPECTION_ID: this.INSPECTION_ID,
          INSPECTION_INSPECTOR_ID: this.INSPECTION_INSPECTOR_ID,
          INSPECTION_USER_ID: this.INSPECTION_USER_ID,
          INSPECTION_USER_OPINION: this.form.INSPECTION_USER_OPINION,
          INSPECTION_USER_SIGN_TIME: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
          ACTION_USER_ID: this.userInfo.USER_ID,
          ACTION_ID: this.userInfo.USER_ID,
        },
      })
      uni.showToast({
        title: '核查成功',
      })
      uni.$u.route({
        type: 'navigateBack'
      })
    },
    showHidden(item) {
      this.hiddenForm = item
      this.popupShow = true
    },
    popupClose() {
      this.popupShow = false
    },
    signConfirm({path}) {
      this.form.INSPECTION_USER_SIGN_IMG = path
    },
    previewImage(current) {
      if (false) {
        uni.previewImage({
          urls: [current],
          current
        });
      }
    },
    previewVideo(videoUrl) {
      this.modalShow = true;
      this.videoUrl = videoUrl;
    }
  },
}
</script>

<style scoped>
.title {
  min-width: 150rpx;
}
</style>
