<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="cu-form-group">
          <view class="title">管理单位:</view>
          {{ form.APPLY_DEPARTMENT_NAME }}
        </view>
        <view class="cu-form-group">
          <view class="title">作业单位:</view>
          {{ form.WORK_UNIT_NAME }}
        </view>
        <view class="cu-form-group">
          <view class="title">作业证编号:</view>
          {{ form.WORK_PERMIT_NUMBER }}
        </view>
        <view class="cu-form-group">
          <view class="title">作业地点:</view>
          {{ form.WORK_LOCATION }}
        </view>
        <view class="cu-form-group" v-if="form.WORK_LONGITUDE">
          <view class="title">地点坐标经度:</view>
          <view>{{form.WORK_LONGITUDE}}</view>
        </view>
        <view class="cu-form-group" v-if="form.WORK_LATITUDE">
          <view class="titletitle">地点坐标纬度:</view>
          <view >{{form.WORK_LATITUDE}}</view>
        </view>
        <view class="cu-form-group">
          <view class="title">受限空间名称及编号</view>
          {{ form.RESTRICTED_SPACE_NAME }}
        </view>
        <view class="cu-form-group">
          <view class="title">作业内容:</view>
          {{ form.CONTENTS_OF_HOMEWORK }}
        </view>
        <view class="cu-form-group">
          <view class="title">主要介质:</view>
          {{ form.MAIN_MEDIUM }}
        </view>
        <view class="cu-form-group">
          <view class="title">主要危害因素:</view>
          {{ form.MAJOR_RISK_FACTORS }}
        </view>
        <view class="cu-form-group">
          <view class="title">作业人:</view>
          {{ form.WORKER }}
        </view>
        <view class="cu-form-group">
          <view class="title">监护人:</view>
          {{ form.GUARDIAN }}
        </view>
        <view class="cu-form-group">
          <view class="title">作业开始时间:</view>
          {{ form.WORK_START_DATE }}
        </view>
        <view class="cu-form-group">
          <view class="title">作业结束时间:</view>
          {{ form.WORK_END_DATE }}
        </view>
        <view class="cu-form-group">
          <view class="title">采样分析:</view>
          <button class="cu-btn bg-green margin-tb-sm sm" @click="$noMultipleClicks(goToDetail,CONFINEDSPACE_ID)">采样结果</button>
        </view>
        <view style="padding: 20upx">
          <uni-table border stripe emptyText="暂无更多数据">
            <uni-tr>
              <uni-th align="center" style="font-weight: bold;width: 100upx">序号</uni-th>
              <uni-th align="center" style="font-weight: bold">安全措施</uni-th>
              <uni-th align="center" style="font-weight: bold;width: 200upx">操作</uni-th>
            </uni-tr>
            <uni-tr v-for="(item,index) in measureList" :key="item.PROTECTIVE_MEASURES">
              <uni-td>{{ index + 1 }}</uni-td>
              <uni-td>{{ item.PROTECTIVE_MEASURES }}</uni-td>
              <uni-td>
                <radio-group @change="changeRadioGroup($event,index)">
                  <radio value="1" />符 合
                  <radio value="0" />不符合
                </radio-group>
              </uni-td>
            </uni-tr>
          </uni-table>
        </view>
        <view class="cu-form-group">
          <view class="title">危害因素辨识:</view>
          <button class="cu-btn round bg-blue" @click="addHazardIdentification">添加</button>
        </view>
        <view class="ace-content" v-show="oldHazardIdentification && oldHazardIdentification.length > 0">
          <view class="add_pard_item" v-for="(item,index) of oldHazardIdentification" :key="index">
            <view class="cu-form-textarea">
              <textarea disabled v-model="oldHazardIdentification[index]"></textarea>
            </view>
          </view>
        </view>
        <view class="ace-content">
          <view class="add_pard_item" v-for="(item,index) of HazardIdentification" :key="index">
            <view class="add_pard_del" @click="removeHazardIdentification(index)">
              <text class="cuIcon-roundclosefill text-red f40"></text>
            </view>
            <view class="cu-form-textarea">
              <textarea placeholder="请输入危害因素辨识..." v-model="HazardIdentification[index]"></textarea>
            </view>
          </view>
        </view>
        <view class="cu-form-group">
          <view class="title">作业安全措施:</view>
          <button class="cu-btn round bg-blue" @click="addWorkSafetyMeasures">添加</button>
        </view>
        <view class="ace-content" v-show="oldWorkSafetyMeasures && oldWorkSafetyMeasures.length > 0">
          <view class="add_pard_item" v-for="(item,index) of oldWorkSafetyMeasures" :key="index">
            <view class="cu-form-textarea">
              <textarea disabled v-model="oldWorkSafetyMeasures[index]"></textarea>
            </view>
          </view>
        </view>
        <view class="ace-content">
          <view class="add_pard_item" v-for="(item,index) of WorkSafetyMeasures" :key="index">
            <view class="add_pard_del" @click="removeWorkSafetyMeasures(index)">
              <text class="cuIcon-roundclosefill text-red f40"></text>
            </view>
            <view class="cu-form-textarea">
              <textarea placeholder="请输入作业安全措施..." v-model="WorkSafetyMeasures[index]"></textarea>
            </view>
          </view>
        </view>
        <view class="cu-form-textarea">
          <view class="cu-form-title">作业单位负责人意见:</view>
          <textarea v-model="form.WORK_UNIT_MANAGER_OPINION" placeholder="请输入作业单位负责人意见"></textarea>
        </view>
        <view class="cu-form-group" style="border: none;">
          <view class="title">签字</view>
          <button class="cu-btn bg-green shadow" @tap="modalShow = true" data-target="Modal">手写签字</button>
        </view>
        <view class="cu-bar bg-white" v-show="imgList && imgList.length > 0">
          <view class="action">
            签字照片
          </view>
        </view>
        <view class="cu-form-group" v-show="imgList && imgList.length > 0">
          <view class="grid col-4 grid-square flex-sub">
            <view style="width: 60%;" class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" data-type="0" :data-url="imgList[index].filePath">
              <image :src="imgList[index].filePath" mode="aspectFill"></image>
              <view class="cu-tag bg-red" @tap.stop="DelImg" data-type="0" :data-index="index">
                <text class='cuIcon-close'></text>
              </view>
            </view>
          </view>
        </view>
        <view class="cu-modal" :class="{'show':modalShow}">
          <sign @confirm="subCanvas" @cancel="modalShow = false"></sign>
        </view>
      </view>
    </view>
    <view class="cu-bar btn-group" style="margin-top: 30upx;">
      <button class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goback)">返回</button>
      <button class="cu-btn bg-blue margin-tb-sm lg" @click="$noMultipleClicks(goSubmit)">确认</button>
    </view>
    <view class="padding flex flex-direction"></view>
  </view>
</template>

<script>
import {
  basePath,
  loginSession,
  loginUser,
  loginUserId,
} from '@/common/tool.js';
import sign from '@/components/sign/sign.vue';

export default {
  components: {
    sign,
  },
  data() {
    return {
      noClick: true,
      CONFINEDSPACE_ID: '',
      form: {
        APPLY_DEPARTMENT_ID: '',
        APPLY_DEPARTMENT_NAME: '',
        APPLY_USER_ID: '',
        APPLY_USER_NAME: '',
        WORK_UNIT_ID: '',
        WORK_UNIT_NAME: '',
        WORK_PERMIT_NUMBER: '',
        WORK_LOCATION: '',
        RESTRICTED_SPACE_INDEX: '',
        RESTRICTED_SPACE_NAME: '',
        RESTRICTED_SPACE_ID: '',
        CONTENTS_OF_HOMEWORK: '',
        MAIN_MEDIUM: '',
        MAJOR_RISK_FACTORS: '',
        WORKER: '',
        GUARDIAN: '',
        WORKING_TIME: '',
        WORK_UNIT_MANAGER_OPINION: '',
      },
      measureList: [],
      oldHazardIdentification: [],
      HazardIdentification: [],
      oldWorkSafetyMeasures: [],
      WorkSafetyMeasures: [],
      modalShow: false,
      imgList: [],
    }
  },
  onLoad(event) {
    this.CONFINEDSPACE_ID = event.id
    this.getData();
    loginSession();
  },
  methods: {
    getData() {
      uni.showLoading({
        title: '请稍候'
      })
      uni.request({
        url: basePath + '/app/confinedspace/goEdit',
        method: 'POST',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          CONFINEDSPACE_ID: this.CONFINEDSPACE_ID,
        },
        success: (res) => {
          if ("success" === res.data.result) {
            this.form = res.data.pd;
            this.measureList = res.data.measureList;
            this.oldHazardIdentification = res.data.pd.HARM && res.data.pd.HARM.split('$$$')
            this.oldWorkSafetyMeasures = res.data.pd.SAFTETY && res.data.pd.SAFTETY.split('$$$')
            uni.hideLoading();
          } else if ("exception" === res.data.result) {
            uni.showToast({
              title: '错误',
              duration: 2000
            });
          }
        }
      });
    },
    goSubmit() {
      var _this = this;
      let required = true
      for (let i = 0; i < this.measureList.length; i++) {
        if(!this.measureList[i].STATUS){
          uni.showToast({
            icon: 'none',
            title: '第'+(i+1)+'项未勾选',
            duration: 1500
          });
          return;
        }
      }
      if(!this.form.WORK_UNIT_MANAGER_OPINION){
        uni.showToast({
          icon: 'none',
          title: '请输入作业单位负责人意见',
          duration: 1500
        });
        return;
      }
      if(this.imgList.length === 0){
        uni.showToast({
          icon: 'none',
          title: '请签字',
          duration: 1500
        });
        required = false
      }
      if (!required) {
        return
      }
      uni.showLoading({
        title: '请稍候'
      })
      var HARM = ''
      if (this.HazardIdentification.length > 0) {
        HARM = '作业单位负责人:' + [...this.HazardIdentification,...this.oldHazardIdentification].join('$$$')
      } else {
        HARM = [...this.oldHazardIdentification].join('$$$')
      }
      var SAFTETY = ''
      if (this.WorkSafetyMeasures.length > 0) {
        SAFTETY = '作业单位负责人:' + [...this.WorkSafetyMeasures,...this.oldWorkSafetyMeasures].join('$$$')
      } else {
        SAFTETY = [...this.oldWorkSafetyMeasures].join('$$$')
      }
      uni.uploadFile({
        url: basePath + '/app/confinedspace/editStatus',
        filePath: this.imgList[0].filePath,
        name: 'FFILE',
        formData: {
          ...this.form,
          CONFINEDSPACE_ID: _this.CONFINEDSPACE_ID,
          measureList:JSON.stringify(this.measureList),
          HARM: HARM,
          SAFTETY: SAFTETY,
          ACTION_USER:loginUser.NAME,
          VECTORY:'2',
          loginUserId: loginUserId
        },
        success: (res) => {
          uni.hideLoading();
          uni.showToast({
            title: '提交成功',
            duration: 1500
          });
          _this.goback()
        },
      });
    },
    changeRadioGroup(e,index){
      this.$set(this.measureList[index], 'STATUS', e.detail.value)
    },
    addHazardIdentification() {
      this.HazardIdentification.push('')
    },
    removeHazardIdentification (i) {
      var _this = this;
      uni.showModal({
        title: '提示',
        cancelText: '确认',
        confirmText: '取消',
        content: '确定删除危害因素辨识吗?',
        success: function (res) {
          if (res.cancel) {
            _this.HazardIdentification.splice(i, 1)
          }
        }
      });
    },
    addWorkSafetyMeasures() {
      this.WorkSafetyMeasures.push('')
    },
    removeWorkSafetyMeasures (i) {
      var _this = this;
      uni.showModal({
        title: '提示',
        cancelText: '确认',
        confirmText: '取消',
        content: '确定删除作业安全措施吗?',
        success: function (res) {
          if (res.cancel) {
            _this.WorkSafetyMeasures.splice(i, 1)
          }
        }
      });
    },
    subCanvas (e) {
      this.imgList.splice(0, this.imgList.length);
      this.imgList.push(e);
      this.modalShow = false;
    },
    ViewImage (e) {
      let files = [];
      for (var i = 0; i < this.imgList.length; i++) {
        files.push(this.imgList[i].filePath)
      }
      uni.previewImage({
        urls: files,
        current: 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.imgList.splice(i, 1)
          }
        }
      })
    },
    goToDetail(id) {
      uni.navigateTo({
        url: '/pages/application/high-risk-work/limited-space/gas-analysis/detail?id=' + id + '&type=view'
      });
    },
    goback() {
      uni.navigateBack({
        delta: 1
      });
      uni.hideLoading();
    },
  }
}
</script>

<style>
.right_icon {
  position: relative;
  padding-right: 36upx;
  width: 200upx;
  text-align: right;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.right_icon ::after {
  font-family: cuIcon;
  display: block;
  content: "\e6a3";
  position: absolute;
  font-size: 17px;
  color: #8799a3;
  line-height: 50px;
  width: 30px;
  text-align: center;
  top: -28upx;
  bottom: 0;
  right: -22upx;
  margin: auto;
}

.modal_content {
  display: flex;
  text-align: left;
}

.modal_left {
  flex-basis: 50%;
  padding: 50upx 20upx;
  border-right: 1px solid #000000;
}

.modal_right {
  flex-basis: 50%;
  padding: 50upx;
}

.modal_left_title, .modal_right_title {
  font-size: 16px;
  color: #000000;
  font-weight: 700;
}

.checkbox-item {
  display: flex;
  align-items: center;
  padding: 0 30upx;
  min-height: 100upx;
  background-color: #ffffff;
  border-bottom: 1upx solid #ddd;
}

.checkbox-item:last-child {
  border-bottom: none;
}
</style>