<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 }} 至 {{ 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 v-if="Math.abs(Number(form.APPLY_STATUS)) > 2" 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: 180upx">操作</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>{{ item.STATUS == 1 ? '符合' : '不符合' }}</uni-td>
            </uni-tr>
          </uni-table>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 2" class="cu-form-group">
          <view class="title">危害因素辨识:</view>
<!--          <button class="cu-btn round bg-blue" @click="addHazardIdentification">添加</button>-->
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 2" 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 v-if="Math.abs(Number(form.APPLY_STATUS)) > 2" class="cu-form-group">
          <view class="title">作业安全措施:</view>
<!--          <button class="cu-btn round bg-blue" @click="addWorkSafetyMeasures">添加</button>-->
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 2" 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 v-if="Math.abs(Number(form.APPLY_STATUS)) > 2" class="cu-form-textarea">
          <view class="cu-form-title">作业单位负责人意见:</view>
          <textarea v-model="form.WORK_UNIT_MANAGER_OPINION" disabled placeholder="请输入作业单位负责人意见"></textarea>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 2" class="cu-form-group">
          <view class="title">作业单位负责人签字:</view>
          <view>
            <view>
              <image :src="baseImgPath + form.WORK_UNIT_MANAGER_IMG" style="width: 330upx;height: 260upx"></image>
            </view>
            <view style="text-align: center">{{form.WORK_UNIT_MANAGER_TIME}}</view>
          </view>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 3" class="cu-form-textarea">
          <view class="cu-form-title">作业现场负责人意见:</view>
          <textarea v-model="form.JOB_SITE_MANAGER_OPINION" disabled placeholder="请输入作业单位负责人意见"></textarea>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 3" class="cu-form-group">
          <view class="title">作业现场负责人签字:</view>
          <view>
            <view>
              <image :src="baseImgPath + form.JOB_SITE_MANAGER_IMG" style="width: 330upx;height: 260upx"></image>
            </view>
            <view style="text-align: center">{{form.JOB_SITE_MANAGER_TIME}}</view>
          </view>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 4" class="cu-form-textarea">
          <view class="cu-form-title">管理单位负责人意见:</view>
          <textarea v-model="form.MANAGEMENT_UNIT_OPINION" disabled placeholder="请输入作业单位负责人意见"></textarea>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 4" class="cu-form-group">
          <view class="title">管理单位负责人签字:</view>
          <view>
            <view>
              <image :src="baseImgPath + form.MANAGEMENT_UNIT_IMG" style="width: 330upx;height: 260upx"></image>
            </view>
            <view style="text-align: center">{{form.MANAGEMENT_UNIT_TIME}}</view>
          </view>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 5 && form.COMPLETION_ACCEPTANCE_IMG" class="cu-form-textarea">
          <view class="cu-form-title">完工验收作业单位负责人意见:</view>
          <textarea v-model="form.COMPLETION_ACCEPTANCE_OPINION" disabled placeholder="请输入作业单位负责人意见"></textarea>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 5 && form.COMPLETION_ACCEPTANCE_IMG" class="cu-form-group">
          <view class="title">完工验收作业单位负责人签字:</view>
          <view>
            <view>
              <image :src="baseImgPath + form.COMPLETION_ACCEPTANCE_IMG" style="width: 330upx;height: 260upx"></image>
            </view>
            <view style="text-align: center">{{form.COMPLETION_ACCEPTANCE_TIME}}</view>
          </view>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 6 && form.COMPLETION_ACCEPTANCE_MANAGEMENT_OPINION_IMG" class="cu-form-textarea">
          <view class="cu-form-title">完工验收管理单位负责人意见:</view>
          <textarea v-model="form.COMPLETION_ACCEPTANCE_MANAGEMENT_OPINION" disabled placeholder="请输入作业单位负责人意见"></textarea>
        </view>
        <view v-if="Math.abs(Number(form.APPLY_STATUS)) > 6 && form.COMPLETION_ACCEPTANCE_MANAGEMENT_OPINION_IMG" class="cu-form-group">
          <view class="title">完工验收管理单位负责人签字:</view>
          <view>
            <view>
              <image :src="baseImgPath + form.COMPLETION_ACCEPTANCE_MANAGEMENT_OPINION_IMG" style="width: 330upx;height: 260upx"></image>
            </view>
            <view style="text-align: center">{{form.COMPLETION_ACCEPTANCE_MANAGEMENT_TIME}}</view>
          </view>
        </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(goSubmit,'1')">验收不通过</button>
      <button class="cu-btn bg-blue margin-tb-sm lg" @click="$noMultipleClicks(goSubmit,'2')">验收通过</button>
    </view>-->
    <view class="padding flex flex-direction"></view>
  </view>
</template>

<script>
  import {
    basePath,
    loginSession,
    baseImgPath,
    corpinfoId,
    loginUser,
    loginUserId,
  } from '@/common/tool.js';
  import sign from '@/components/sign/sign.vue';
  import tkiTree from "@/components/select-tree/select-tree.vue"

  export default {
    components: {
      sign,
      tkiTree,
    },
    data() {
      return {
        baseImgPath,
        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: '',
          WORK_UNIT_MANAGER_IMG: '',
          WORK_UNIT_MANAGER_TIME: '',
          JOB_SITE_MANAGER_OPINION: '',
          JOB_SITE_MANAGER_IMG: '',
          JOB_SITE_MANAGER_TIME: '',
          MANAGEMENT_UNIT_OPINION: '',
          MANAGEMENT_UNIT_IMG: '',
          MANAGEMENT_UNIT_TIME: '',
          COMPLETION_ACCEPTANCE_OPINION: '',
          COMPLETION_ACCEPTANCE_MANAGEMENT_DEPARTMENT_NAME: '',
          COMPLETION_ACCEPTANCE_MANAGEMENT_DEPARTMENT_ID: '',
          COMPLETION_ACCEPTANCE_MANAGEMENT_NAME: '',
          COMPLETION_ACCEPTANCE_MANAGEMENT_INDEX: '',
          COMPLETION_ACCEPTANCE_MANAGEMENT_ID: '',
        },
        measureList: [],
        oldHazardIdentification: [],
        HazardIdentification: [],
        oldWorkSafetyMeasures: [],
        WorkSafetyMeasures: [],
        modalShow: false,
        imgList: [],
        treeNode: [],
        completionAcceptanceManagementList: [],
      }
    },
    onLoad(event) {
      this.CONFINEDSPACE_ID = event.id
      console.log(event.id+'22')
      this.getData();
      this.getDept();
      loginSession();
    },
    methods: {
      getData() {
        var _this = this;
        console.log(this.CONFINEDSPACE_ID+'1111')
        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(type) {
        var _this = this;
        let required = true
        if(type === '2') {
          if(this.imgList.length === 0){
            uni.showToast({
              icon: 'none',
              title: '请签字',
              duration: 1500
            });
            required = false
          }
          if (!required) {
            return
          }
          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('$$$')
          }
          this.form.APPLY_STATUS = '5'
          uni.showLoading({
            title: '请稍候'
          })
          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: '5',
              loginUserId: loginUserId
            },
            success: (res) => {
              uni.hideLoading();
              uni.showToast({
                title: '提交成功',
                duration: 1500
              });
              _this.goback()
            },
          });
        }else {
          uni.showLoading({
            title: '请稍候'
          })
          uni.request({
            url: basePath + '/app/confinedspace/editStatus',
            method: 'POST',
            dataType: 'json',
            header: {
              'Content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              ...this.form,
              CONFINEDSPACE_ID: _this.CONFINEDSPACE_ID,
              ACTION_USER: loginUser.NAME,
              VECTORY: '-5',
              loginUserId: loginUserId
            },
            success: (res) => {
              uni.showToast({
                icon: 'none',
                title: '保存成功',
                duration: 2000
              });
              _this.goback()
            },
            fail: (err) => {
              uni.hideLoading();
              uni.showModal({
                content: err.errMsg,
                showCancel: false
              });
            }
          })
        }
      },
      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)
            }
          }
        })
      },
      getDept() {
        var _this = this;
        uni.request({
          url: basePath + '/app/sys/listTree', //部门下拉接口
          method: 'POST',
          dataType: 'json',
          header: {
            'Content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            CORPINFO_ID: corpinfoId,
          },
          success: (res) => {
            if ("success" == res.data.result) {
              _this.treeNode = eval(res.data.zTreeNodes);
            } else {
              uni.showToast({
                title: res.data.message,
                duration: 2000
              });
            }
          }
        });
      },
      showZgTree(ref) {
        this.$refs[ref]._show();
      },
      zgtreeConfirm(e, list) {
        if (list === "completionAcceptanceManagementList") {
          this.$set(this.form,'COMPLETION_ACCEPTANCE_MANAGEMENT_DEPARTMENT_ID',e[0].id)
          this.$set(this.form,'COMPLETION_ACCEPTANCE_MANAGEMENT_DEPARTMENT_NAME',e[0].name)
          this.$set(this.form,'COMPLETION_ACCEPTANCE_MANAGEMENT_INDEX','')
          this.$set(this.form,'COMPLETION_ACCEPTANCE_MANAGEMENT_ID','')
          this.$set(this.form,'COMPLETION_ACCEPTANCE_MANAGEMENT_NAME','')
        }
        this.getUserList(list, e[0].id);
      },
      zgtreeCancel(e) {
        this.isUps = false;
      },
      getUserList(list, DEPARTMENT_ID) {
        //发送 post 请求
        var _this = this;
        uni.request({
          method: 'POST',
          dataType: 'json',
          header: {
            'Content-type': 'application/x-www-form-urlencoded'
          },
          url: basePath + '/app/sys/listUser',
          data: {
            DEPARTMENT_ID, NOMAIN:'1',
            tm: new Date().getTime()
          },
          success: function (res) {
            if ("success" == res.data.result) {
              _this[list] = res.data.userList;
            } else {
              uni.showToast({
                title: res.data.message,
                duration: 2000
              });
            }
          }
        })
      },
      completionAcceptanceManagementChange(e) {
        this.form.COMPLETION_ACCEPTANCE_MANAGEMENT_INDEX = e.detail.value;
        this.form.COMPLETION_ACCEPTANCE_MANAGEMENT_ID = this.completionAcceptanceManagementList[e.detail.value].USER_ID;
        this.form.COMPLETION_ACCEPTANCE_MANAGEMENT_NAME = this.completionAcceptanceManagementList[e.detail.value].NAME;
      },
      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>