<template>
  <view class="content p-10">
    <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">{{ form.APPLY_USER_NAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">申请时间：</view>
            <view slot="value">{{ form.APPLICATION_DATE }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">作业证编号：</view>
            <view slot="value">{{ form.JOB_NUMBER }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">作业类型：</view>
            <view slot="value">{{ form.JOB_TYPE_NAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">动火许可证：</view>
            <view slot="value">
              <u-button type="primary" text="查看" size="mini" class="bth-mini" @click="fnNavigator"></u-button>
            </view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <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">{{ form.projectCompetent_USER_NAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">确认时间：</view>
            <view slot="value">{{ form.projectCompetent_OPERATTIME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">是否确认通过：</view>
            <view slot="value">{{ form.projectCompetent_TYPE == '2' ? '通过' : '不通过' }}</view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <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">{{ form.projectPreliminarily_USER_NAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">初审时间：</view>
            <view slot="value">{{ form.projectPreliminarily_OPERATTIME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">初审结果：</view>
            <view slot="value">{{ form.projectPreliminarily_TYPE == '2' ? '通过' : '不通过' }}</view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <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">{{ form.projectPreliminarily_TYPE == '2' ? '通过' : '不通过' }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">签字：</view>
            <view slot="label" class="mt-10">
              <u--image :showLoading="true" :src="$store.state.filePath + form.projectPreliminarily_PRINCIPAL"
                        width="70" height="70" mode="scaleToFill"
                        @click="previewImage($store.state.filePath + form.projectPreliminarily_PRINCIPAL)"></u--image>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">签字时间：</view>
            <view slot="value">{{ form.projectPreliminarily_OPERATTIME }}</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 meetingImgList" :key="index" class="ml-10 mt-10">
                  <u--image :showLoading="true" :src="$store.state.filePath + item.FILEPATH" width="70" height="70"
                            mode="scaleToFill"
                            @click="previewImage($store.state.filePath + item.FILEPATH,meetingImgList)"></u--image>
                </view>
              </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 appendixImgList" :key="index" class="ml-10 mt-10">
                  <u--image :showLoading="true" :src="$store.state.filePath + item.FILEPATH" width="70" height="70"
                            mode="scaleToFill"
                            @click="previewImage($store.state.filePath + item.FILEPATH,appendixImgList)"></u--image>
                </view>
              </view>
            </view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <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">{{ form.inspectorGeneral_TYPE == '2' ? '通过' : '不通过' }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">签字：</view>
            <view slot="label" class="mt-10">
              <u--image :showLoading="true" :src="$store.state.filePath + form.inspectorGeneral_PRINCIPAL" width="70"
                        height="70" mode="scaleToFill"
                        @click="previewImage($store.state.filePath + form.inspectorGeneral_PRINCIPAL)"></u--image>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">签字时间：</view>
            <view slot="value">{{ form.inspectorGeneral_OPERATTIME }}</view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <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">{{ form.supervision_TYPE == '2' ? '通过' : '不通过' }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">签字：</view>
            <view slot="label" class="mt-10">
              <u--image :showLoading="true" :src="$store.state.filePath + form.supervision_PRINCIPAL" width="70"
                        height="70" mode="scaleToFill"
                        @click="previewImage($store.state.filePath + form.supervision_PRINCIPAL)"></u--image>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">签字时间：</view>
            <view slot="value">{{ form.supervision_OPERATTIME }}</view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <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">{{ form.committee_TYPE == '2' ? '通过' : '不通过' }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">初审人员：</view>
            <view slot="value">{{ form.committee_USER_NAME }}</view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title">初审时间：</view>
            <view slot="value">{{ form.committee_OPERATTIME }}</view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <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">
              <u-radio-group v-model="pd.isChange" placement="row">
                <u-radio label="通过" name="2"></u-radio>
                <view class="ml-10">
                  <u-radio label="不通过" name="1"></u-radio>
                </view>
              </u-radio-group>
            </view>
          </u-cell>
          <u-cell v-if="pd.isChange === '2'">
            <view slot="title" class="title">上传会议纪要：</view>
            <view slot="label" class="mt-10">
              <u-upload
                  ref="aaa"
                  uploadIcon="plus"
                  :fileList="fileList"
                  @afterRead="afterRead"
                  @delete="deletePic"
                  name="1" multiple
                  :maxCount="4"></u-upload>
            </view>
          </u-cell>
          <u-cell v-if="pd.isChange === '2'">
            <view slot="title" class="title">上传附件：</view>
            <view slot="label" class="mt-10">
              <u-upload
                  uploadIcon="plus"
                  :fileList="fileList1"
                  @afterRead="afterRead1"
                  @delete="deletePic1"
                  multiple
                  :maxCount="4"
              ></u-upload>
            </view>
          </u-cell>
          <u-cell v-if="pd.isChange === '2'">
            <view slot="title" class="title">(港股安委办主任)安全总监部门：</view>
            <view slot="value">
              <u--text :text="pd.safetyDirector_dept_id ==''?'请选择':pd.safetyDirector_dept_name"
                       @click="showTree"></u--text>
              <tki-tree ref="tkitree" :range="deptRange" rangeKey="name" selectParent @confirm="confirmTree"/>
            </view>
          </u-cell>
          <u-cell v-if="pd.isChange === '2'">
            <view slot="title" class="title">(港股安委办主任)安全总监：</view>
            <view slot="value">
              <u--text :text="pd.safetyDirector_user_id ==''?'请选择':pd.safetyDirector_user_name"
                       @click="showPicker"></u--text>
              <u-picker :show="show" :columns="columns" keyName="NAME" @cancel="show = false"
                        @confirm="confirmPicker"></u-picker>
            </view>
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <view class="mt-10">
      <u-button type="primary" text="提交" @click="$u.debounce(submit, 1000,true)"></u-button>
    </view>
  </view>
</template>

<script>
import tkiTree from "@/components/tki-tree/tki-tree.vue"
import {
  getHotWorkGoEdit,
  getImgfilesListByKeyAndType,
  getDept,
  getDeptUser,
  approveHotWorkSupervision,
  setImgFilesListAdd
} from '../../../api/index'

export default {
  components: {tkiTree},
  data() {
    return {
      form: {},
      fileList: [],
      fileList1: [],
      pd: {
        safetyDirector_dept_id: '',
        safetyDirector_dept_name: '',
        safetyDirector_user_id: '',
        safetyDirector_user_name: ''
      },
      range: [],
      show: false,
      columns: [],
      HOTWORKAPPLICATION_ID: '',
      meetingImgList: [],
      appendixImgList: [],
      deptRange: []
    }
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo
    }
  },
  onLoad(event) {
    this.HOTWORKAPPLICATION_ID = event.HOTWORKAPPLICATION_ID

    this.getDate()
    this.getImgFilesList('203', 'meetingImgList')
    this.getImgFilesList('204', 'appendixImgList')
    this.getDept()
  },
  methods: {
    async getDate() {
      let resData = await getHotWorkGoEdit({HOTWORKAPPLICATION_ID: this.HOTWORKAPPLICATION_ID});
      this.form = resData.pd
    },
    async getImgFilesList(type, listName) {
      let imgPd = {'FOREIGN_KEY': this.HOTWORKAPPLICATION_ID, 'TYPE': type}
      let resData = await getImgfilesListByKeyAndType({...imgPd});
      this[listName] = resData.imgs
    },
    async getDept() {
      let resData = await getDept({'DEPARTMENT_ID': '0'});
      this.deptRange = JSON.parse(resData.zTreeNodes)
    },
    async getDeptUser(detpId) {
      let resData = await getDeptUser({'DEPARTMENT_ID': detpId});
      this.$set(this.columns, 0, resData.userList)
    },
    fnNavigator() {
      uni.$u.route({
        url: '/pages/super-hot/hot-work-permit/index',
        params: {
          HOTWORKAPPLICATION_ID: this.HOTWORKAPPLICATION_ID
        }
      })
    },
    showTree() {
      this.$refs.tkitree._show()
    },
    confirmTree(e) {
      this.pd.safetyDirector_dept_id = e[0].id
      this.pd.safetyDirector_dept_name = e[0].name
      this.pd.safetyDirector_user_id = ''
      this.pd.safetyDirector_user_name = ''
      this.getDeptUser(this.pd.safetyDirector_dept_id)
    },
    showPicker() {
      this.show = true
    },
    confirmPicker(e) {
      this.pd.safetyDirector_user_id = e.value[0].USER_ID
      this.pd.safetyDirector_user_name = e.value[0].NAME
      this.show = false;
    },
    afterRead(event) {
      this.fileList = [...this.fileList, ...event.file]

    },
    deletePic(event) {
      this.fileList = this.fileList.filter(item => item.url !== event.file.url)
    },
    afterRead1(event) {
      if (!this.fileList1) this.$set(this, 'fileList1', [])
      this.fileList1 = [...this.fileList1, ...event.file]
    },
    deletePic1(event) {
      this.fileList1 = this.fileList1.filter(item => item.url !== event.file.url)
    },
    previewImage(current) {
      uni.previewImage({
        urls: [current],
        current
      });
    },
    async submit() {
      var _this = this
      if (this.pd.isChange === '2') { // 审批通过
        console.log(_this.fileList)
        console.log(_this.fileList1)
        if (_this.fileList.length == 0) {
          uni.showToast({
            icon: "none",
            title: '请上传会议纪要',
            duration: 1500
          });
          return false
        }
        if (_this.fileList1.length == 0) {
          uni.showToast({
            icon: "none",
            title: '请上传上传附件',
            duration: 1500
          });
          return false
        }


        if (_this.pd.safetyDirector_user_id == '') {
          uni.showToast({
            icon: "none",
            title: '请选择安全总监',
            duration: 1500
          });
          return false
        }
      }
      await approveHotWorkSupervision({
        'TYPE': _this.pd.isChange,
        'HOTWORKAPPLICATION_ID': _this.HOTWORKAPPLICATION_ID,
        'STATE': '36',
        'safetyDirector_USER_ID': _this.pd.safetyDirector_user_id,
        'loginUserId': _this.userInfo.USER_ID
      });
      console.log(_this.fileList)
      if (_this.fileList.length > 0) {
        await this.uploadImg('fileList', '205')
        await this.uploadImg('fileList1', '206')
      }
      uni.$u.route({
        type: 'navigateBack'
      })
    },
    async uploadImg(listName, imgType) {
      let imgFiles1 = []
      for (let i = 0; i < this[listName].length; i++) {
        imgFiles1.push({
          uri: this[listName][i].url,
          FFILE: this[listName][i].url,
          name: 'file' + i
        })
      }
      let formData1 = {
        'TYPE': imgType,
        'FOREIGN_KEY': this.HOTWORKAPPLICATION_ID,
        'CORPINFO_ID': this.userInfo.CORPINFO_ID,
      }
      await setImgFilesListAdd({files: imgFiles1, formData: formData1})
    },
    goBack() {

    }
  },
}
</script>

<style scoped>

</style>
