<template>
  <view class="content">
    <view class="card">
      <view class="view-title">
        <u--text text="隐患信息" bold></u--text>
      </view>
      <view class="mt-10">
        <u-cell-group :border="false">
          <u-cell>
            <view slot="title" class="title">隐患来源:</view>
            <view slot="value">
              <template v-if="data.SOURCE == '1'">隐患快报</template>
              <template v-else-if="data.SOURCE == '2'">清单排查</template>
              <template v-else-if="data.SOURCE == '3'">清单排查</template>
              <template v-else-if="data.SOURCE == '4'">安全环保检查(监管端)</template>
              <template v-else-if="data.SOURCE == '5'">安全环保检查(企业端)</template>
              <template v-else-if="data.SOURCE == '8'">点检隐患</template>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患描述:</view>
            <view slot="value">{{ data.HIDDENDESCR }}</view>
          </u-cell>
          <template v-if="data.SOURCE == '2'">
            <u-cell>
              <view slot="title" class="title">风险点(单元):</view>
              <view slot="value">{{ data.RISK_UNIT }}</view>
            </u-cell>
            <u-cell>
              <view slot="title" class="title">辨识部位:</view>
              <view slot="value">{{ data.IDENTIFICATION }}</view>
            </u-cell>
            <u-cell>
              <view slot="title" class="title">存在风险:</view>
              <view slot="value">{{ data.RISK_DESCR }}</view>
            </u-cell>
            <u-cell>
              <view slot="title" class="title">风险分级:</view>
              <view slot="value">{{ data.LEVEL }}</view>
            </u-cell>
            <u-cell>
              <view slot="title" class="title">检查内容:</view>
              <view slot="value">{{ data.CHECK_CONTENT }}</view>
            </u-cell>
          </template>
          <u-cell>
            <view slot="title" class="title">隐患级别:</view>
            <view slot="value">{{ data.HIDDENLEVELNAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患类型:</view>
            <view slot="value">{{ data.HIDDENTYPENAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患状态:</view>
            <view slot="value">
              <template v-if="data.STATE == '1'">未整改</template>
              <template v-else-if="data.STATE == '2'">已整改</template>
              <template v-else-if="data.STATE == '3'">已验收</template>
              <template v-else-if="data.STATE == '4'">已验收</template>
              <template v-else-if="data.STATE == '8'">特殊处置审核</template>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患部位:</view>
            <view slot="value">{{ data.hregionName ? data.hregionName : data.HIDDENPART }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患位置描述:</view>
            <view slot="value">{{ data.POSITIONDESC }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">是否是相关方:</view>
            <view slot="value">{{ data.ISRELEVANT === '1' ? '是' : '否' }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">整改类型:</view>
            <view slot="value">
              <template v-if="data.RECTIFICATIONTYPE == '1'">立即整改</template>
              <template v-else-if="data.RECTIFICATIONTYPE == '2'">限期整改</template>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">位置:</view>
            <view slot="label" class="mt-10">
              <view>经度:{{ data.LONGITUDE }}</view>
              <view>纬度:{{ data.LATITUDE }}</view>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患图片:</view>
            <view slot="label" class="mt-10">
              <view style="display: flex;flex-wrap: wrap">
                <view v-for="(item,index) in hImgs" :key="index" class="ml-10 mt-10">
                  <u--image :showLoading="true" :src="item" width="80px" height="80px"
                            @click="previewImage(hImgs,item)"></u--image>
                </view>
              </view>
            </view>
          </u-cell>
          <u-cell v-if="videoUrl">
            <view slot="title" class="title">隐患视频</view>
            <view slot="value">
              <u-icon name="play-circle-fill" size="30" @click="modalShow = true"></u-icon>
            </view>
          </u-cell>
          <u-modal :show="modalShow" title="视频播放" confirmText="关闭" @confirm="modalShow = false">
            <view>
              <video v-if="modalShow" :src="videoUrl" :autoplay="true"></video>
            </view>
          </u-modal>
          <u-cell v-for="(item,index) in hiddenFindPeople" :key="index">
            <view slot="title" class="title">隐患发现人</view>
            <view slot="value">{{ item.NAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">发现时间</view>
            <view slot="value">{{ data.CREATTIME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">整改负责人</view>
            <view slot="value">{{ data.oldUserName }}</view>
          </u-cell>
          <u-cell v-if="data.RECTIFICATIONTYPE == '2'">
            <view slot="title" class="title">整改完成期限</view>
            <view slot="value">{{ data.oldRectificationDeadline }}</view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>


    <view class="card" v-for="(item,index) in hiddenExamineList" :key="index">
      <view class="view-title">
        <u--text text="确认信息" bold v-if="item.TYPE === 4"></u--text>
        <u--text text="延期信息" bold v-else-if="item.TYPE === 2"></u--text>
        <u--text text="特殊处理审核信息" bold v-else-if="item.TYPE === 1"></u--text>
      </view>
      <view class="mt-10" v-if="item.TYPE === 4">
        <u-cell-group :border="false">
          <u-cell>
            <view slot="title" class="title">隐患级别</view>
            <view slot="value">{{ item.editLevelName }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">整改负责人</view>
            <view slot="value">{{ item.editUserName }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">整改完成期限</view>
            <view slot="value">{{ item.spare3 }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患确认人</view>
            <view slot="value">{{ data.conUserName }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">隐患确认时间</view>
            <view slot="value">{{ data.CONFIRM_TIME }}</view>
          </u-cell>
        </u-cell-group>
      </view>
      <view class="mt-10" v-if="item.TYPE === 2">
        <u-cell-group :border="false">
          <u-cell>
            <view slot="title" class="title">申请延期日期</view>
            <view slot="value">{{ item.DELAY_TIME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">审核人</view>
            <!--          <view slot="value" v-if="pd.conUserName">{{ pd.conUserName }}</view>-->
            <!--          <view slot="value" v-if="pd.conUserName">{{ pd.conUserName }}</view>-->
          </u-cell>
          <u-cell>
            <view slot="title" class="title">处置方案</view>
            <view slot="value">{{ item.DISPOSAL_PLAN }}</view>
          </u-cell>
          <u-cell v-if="item.DISPOSAL_FILE">
            <view slot="title" class="title">方案附件</view>
            <view slot="label" class="mt-10">
              <text class="u-line-1">{{ item.DISPOSAL_FILE }}</text>
              <u-button type="primary" text="下载" size="mini"
                        @click="downloadAppendix(item.DISPOSAL_FILE)"></u-button>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">延期审核状态</view>
            <view slot="value">
              <template v-if="item.STATE === -1">待审核</template>
              <template v-else-if="item.STATE === 1">通过</template>
              <template v-else-if="item.STATE === 2">未通过</template>
            </view>
          </u-cell>
          <u-cell v-if="item.STATE === 1">
            <view slot="title" class="title">审核时间</view>
            <view slot="value">{{ item.OPERATTIME }}</view>
          </u-cell>
        </u-cell-group>
      </view>
      <view class="mt-10" v-if="item.TYPE === 1">
        <u-cell-group :border="false">
          <u-cell>
            <view slot="title" class="title">无法整改原因</view>
            <view slot="value">{{ item.EXAMINE }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">特殊处置审核状态</view>
            <view slot="value">
              <template v-if="item.STATE === -1">待审核</template>
              <template v-else-if="item.STATE === 2">通过</template>
              <template v-else-if="item.STATE === 1">未通过</template>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">处置方案</view>
            <view slot="value">{{ item.DISPOSAL_PLAN }}</view>
          </u-cell>
          <u-cell v-if="item.DISPOSAL_FILE">
            <view slot="title" class="title">方案附件</view>
            <view v-if="item.DISPOSAL_FILE" slot="label" class="mt-10">
              <!--            <text class="u-line-1">{{ item.DISPOSAL_FILE }}</text>-->
              <u-button type="primary" text="下载" size="mini"
                        @click="downloadAppendix(item.DISPOSAL_FILE)"></u-button>
            </view>
          </u-cell>
          <u-cell v-if="item.STATE !== -1">
            <view slot="title" class="title">审核时间</view>
            <view slot="value">{{ item.OPERATTIME }}</view>
          </u-cell>
          <u-cell v-if="item.STATE!==-1">
            <view slot="title" class="title">审核人</view>
            <view slot="value">{{ data.conUserName }}</view>
          </u-cell>
          <u-cell v-if="item.TYPE === '2'">
            <view slot="title" class="title">是否更换整改负责人</view>
            <view slot="value">
              <template v-if="item.spare1 === '1'">是</template>
              <template v-if="item.spare1 === '0'">否</template>
            </view>
          </u-cell>
          <u-cell v-if="item.STATE !== '-1'">
            <view slot="title" class="title">是否更换整改负责人</view>
            <view slot="value">
              <template v-if="item.spare2 === undefined">是</template>
              <template v-else>否</template>
            </view>
          </u-cell>
          <u-cell v-if="item.spare1 === '1'">
            <view slot="title" class="title">整改负责人</view>
            <view slot="value">{{ item.NEWRECTIFICATIONOR }}</view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <view class="card" v-if="data.STATE >= 2 && data.STATE != 8">
      <view class="view-title">
        <u--text text="整改信息" bold></u--text>
      </view>
      <u-cell-group :border="false" class="mt-10">
        <u-cell>
          <view slot="title" class="title">整改描述:</view>
          <view slot="value">{{ data.RECTIFYDESCR }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">整改部门:</view>
          <view slot="value">{{ data.RECTIFICATIONDEPTNAME }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">整改人:</view>
          <view slot="value">{{ data.RECTIFICATIONORNAME }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">整改时间:</view>
          <view slot="value">{{ data.RECTIFICATIONTIME }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">投入资金:</view>
          <view slot="value">{{ data.INVESTMENT_FUNDS }}元</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">整改后图片:</view>
          <view slot="label" class="mt-10">
            <view style="display: flex;flex-wrap: wrap">
              <view v-for="(item,index) in rImgs" :key="index" class="ml-10 mt-10">
                <u--image :showLoading="true" :src="item" width="80px" height="80px"
                          @click="previewImage(rImgs,item)"></u--image>
              </view>
            </view>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">整改方案:</view>
          <view slot="value">
            <template v-if="data.HAVESCHEME == 0">无</template>
            <template v-else-if="data.HAVESCHEME == 1">有</template>
          </view>
        </u-cell>
      </u-cell-group>
    </view>
    <view class="card" v-if="hs && hs.HIDDENSCHEME_ID && data.HAVESCHEME==1">
      <view class="view-title">
        <u--text text="整改方案" bold></u--text>
      </view>
      <u-cell-group :border="false" class="mt-10">
        <u-cell>
          <view slot="title" class="title">排查日期:</view>
          <view slot="value">{{ hs.SCREENINGDATE }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">隐患清单:</view>
          <view slot="value">{{ hs.LISTNAME }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">治理标准要求:</view>
          <view slot="value">{{ hs.GOVERNSTANDARDS }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">治理方法:</view>
          <view slot="value">{{ hs.GOVERNMETHOD }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">经费和物资的落实:</view>
          <view slot="value">{{ hs.EXPENDITURE }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">负责治理人员:</view>
          <view slot="value">{{ hs.PRINCIPAL }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">工时安排:</view>
          <view slot="value">{{ hs.PROGRAMMING }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">时限要求:</view>
          <view slot="value">{{ hs.TIMELIMITFOR }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">工作要求:</view>
          <view slot="value">{{ hs.JOBREQUIREMENT }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">其他事项:</view>
          <view slot="value">{{ hs.OTHERBUSINESS }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">方案图片:</view>
          <view slot="label" class="mt-10">
            <view style="display: flex;flex-wrap: wrap">
              <view v-for="(item,index) in sImgs" :key="index" class="ml-10 mt-10">
                <u--image :showLoading="true" :src="item" width="80px" height="80px"
                          @click="previewImage(sImgs,item)"></u--image>
              </view>
            </view>
          </view>
        </u-cell>
      </u-cell-group>
    </view>
    <view class="card" v-if="data.STATE >= 4 && data.STATE != 8">
      <view class="view-title">
        <u--text text="验收信息" bold></u--text>
      </view>
      <u-cell-group :border="false" class="mt-10" v-for="(item,index) in checkList" :key="index">
        <u-cell>
          <view slot="title" class="title">验收描述:</view>
          <view slot="value">{{ item.CHECKDESCR }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">是否合格:</view>
          <view slot="value">
            <template v-if="item.STATUS == '1'">是</template>
            <template v-else-if="item.STATUS == '0'">否</template>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">验收部门:</view>
          <view slot="value">{{ item.CHECKDEPTNAME }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">验收人:</view>
          <view slot="value">{{ item.CHECKORNAME }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">验收时间:</view>
          <view slot="value">{{ item.CHECK_TIME }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">验收图片:</view>
          <view slot="label" class="mt-10">
            <view style="display: flex;flex-wrap: wrap">
              <view v-for="(items,index1) in item.cImgs" :key="index1" class="ml-10 mt-10">
                <u--image :showLoading="true" :src="filePath + '/' +items.FILEPATH" width="80px" height="80px"
                          @click="previewImages(item.cImgs,items)"></u--image>
              </view>
            </view>
          </view>
        </u-cell>
      </u-cell-group>
    </view>
    <view class="card" v-if="type === 'acceptance'">
      <view class="view-title">
        <u--text text="安全环保检查验收" bold></u--text>
      </view>
      <u-cell-group :border="false" class="mt-10">
        <u-cell>
          <view slot="title" class="title required">是否合格:</view>
          <view slot="value">
            <u-radio-group v-model="form.FINAL_CHECK" placement="row">
              <u-radio label="是" name="1"></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="label" class="mt-10">
            <u--textarea v-model="form.FINAL_CHECKDESCR" placeholder="请输入验收意见"></u--textarea>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">验收时间:</view>
          <view slot="value">
            <text @click="finalChecktimeShow = true">{{ form.FINAL_CHECKTIME || '请选择' }}</text>
            <u-datetime-picker :show="finalChecktimeShow" :value="form.FINAL_CHECKTIME"
                               :minDate="Number(new Date())" mode="date"
                               @cancel="finalChecktimeShow = false"
                               @confirm="finalChecktimeConfirm"></u-datetime-picker>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">验收图片:</view>
          <view slot="label" class="mt-10">
            <u-upload
                uploadIcon="plus"
                :fileList="form.check_imgList"
                @afterRead="afterRead"
                @delete="deletePic"
                multiple
                :maxCount="4"
            ></u-upload>
          </view>
        </u-cell>
        <u-cell :border="false">
          <view slot="label">
            <u-button type="primary" text="提交" @click="$u.debounce(fnSubmit, 1000, true)"></u-button>
          </view>
        </u-cell>
      </u-cell-group>
    </view>
  </view>
</template>

<script>

import {getHiddenGoEdit, setFinalCheck, setImgFilesAdd, setVideoAdd} from "../../../api";

export default {
  data() {
    return {
      INSPECTION_ID: '',
      HIDDEN_ID: '',
      type: '',
      data: {},
      hImgs: [],
      videoUrl: '',
      modalShow: false,
      hiddenFindPeople: [],
      hiddenExamineList: [],
      rImgs: [],
      hs: {},
      sImgs: [],
      checkList: [],
      cImgs: [],
      finalChecktimeShow: false,
      form: {
        FINAL_CHECK: '',
        FINAL_CHECKDESCR: '',
        FINAL_CHECKTIME: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
        check_imgList: []
      },
      rules: {
        'FINAL_CHECK': {required: true, message: '请选择是否合格'},
        'FINAL_CHECKDESCR': {required: true, message: '请输入验收意见'},
        'FINAL_CHECKTIME': {required: true, message: '请选择验收时间'},
      }
    }
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo
    },
    filePath() {
      return this.$store.state.filePath
    }
  },
  onLoad(event) {
    this.INSPECTION_ID = event.INSPECTION_ID
    this.HIDDEN_ID = event.HIDDEN_ID
    this.type = event.type
    this.getData()
  },
  methods: {
    async getData() {
      let resData = await getHiddenGoEdit({
        HIDDEN_ID: this.HIDDEN_ID
      })
      this.data = resData.pd;
      this.hs = resData.hs;
      this.hiddenFindPeople = resData.hiddenFindPeople || []
      this.hiddenExamineList = resData.hiddenExa || []
      this.checkList = resData.checkList
      resData.hiddenExa && resData.hiddenExa.forEach(item => {
        if (item.TYPE === 4) {
          this.data.oldUserName = item.oldUserName
          this.data.oldDeptName = item.oldDeptName
          this.data.oldLevelName = item.oldLevelName
          this.data.oldRectificationDeadline = item.spare3
        }
      })
      for (let i = 0; i < resData.hImgs.length; i++) {
        this.hImgs.push(this.$store.state.filePath + resData.hImgs[i].FILEPATH)
      }
      for (let i = 0; i < resData.rImgs.length; i++) {
        this.rImgs.push(this.$store.state.filePath + resData.rImgs[i].FILEPATH)
      }
      for (let i = 0; i < resData.cImgs && resData.cImgs.length; i++) {
        this.cImgs.push(this.$store.state.filePath + resData.cImgs[i].FILEPATH)
      }
      for (let i = 0; i < resData.sImgs.length; i++) {
        this.sImgs.push(this.$store.state.filePath + resData.sImgs[i].FILEPATH)
      }
      if (resData.hiddenVideo.length > 0) this.videoUrl = this.$store.state.filePath + resData.hiddenVideo[0].FILEPATH
    },
    previewImage(list, current) {
      console.log(list)
      console.log(current)
      uni.previewImage({
        urls: list,
        current
      });
    },
    previewImages(list, current) {
      let url_list = []
      for (let i = 0; i < list.length; i++) {
        url_list.push(this.$store.state.filePath + list[i].FILEPATH)
      }
      let _current = this.filePath + current.FILEPATH
      uni.previewImage({
        urls: url_list,
        _current
      });
    },
    downloadAppendix(DISPOSAL_FILE) {
      uni.downloadFile({
        url: this.$store.state.filePath + DISPOSAL_FILE,
        success: (res) => {
          uni.openDocument({
            filePath: res.tempFilePath,
          });
        },
      });
    },
    finalChecktimeConfirm(e) {
      this.form.FINAL_CHECKTIME = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
      this.finalChecktimeShow = false;
    },
    afterRead(event) {
      this.form.check_imgList = [...this.form.check_imgList, ...event.file]
    },
    deletePic(event) {
      this.form.check_imgList = this.form.check_imgList.filter(item => item.url !== event.file.url)
    },
    async fnSubmit() {
      for (const key in this.rules) {
        if (!this.form[key]) {
          uni.showToast({
            title: this.rules[key].message,
            icon: "none"
          })
          return;
        }
      }
      // if (this.form.check_imgList.length === 0) {
      //   uni.showToast({
      //     title: '请上传验收图片',
      //     icon: "none"
      //   })
      //   return;
      // }
      uni.showLoading({
        title: '加载中'
      });
      await setFinalCheck({
        HIDDEN_ID: this.HIDDEN_ID,
        INSPECTION_ID: this.INSPECTION_ID,
        FINAL_CHECKOR: this.userInfo.USER_ID,
        ...this.form,
        loading: false
      })
      let PromiseArr = []
      for (let i = 0; i < this.form.check_imgList.length; i++) {
        let imgFiles = []
        imgFiles.push({
          uri: this.form.check_imgList[i].url,
          FFILE: this.form.check_imgList[i].url,
        })
        PromiseArr.push(this.fnSetVideoAdd(imgFiles))
      }
      Promise.all(PromiseArr).then(() => {
        uni.hideLoading()
        uni.showToast({
          title: '验收成功',
        })
        uni.$u.route({
          type: 'navigateBack'
        })
      }).catch((e) => {
        uni.showToast({
          title: '验收失败!',
          icon: 'error',
          duration: 2000
        });
      })
    },
    async fnSetVideoAdd(imgFiles) {
      console.log(imgFiles)
      return await setImgFilesAdd({
        files: imgFiles,
        formData: {
          FOREIGN_KEY: this.HIDDEN_ID,
          TYPE: '50'
        },
        loading: false
      })
    }
  }
}
</script>

<style scoped>

</style>