<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_USER_NAME }}
        </view>
        <view class="cu-form-group">
          <view class="title">申请日期:</view>
          {{ form.APPLICATION_DATE }}
        </view>

        <view class="cu-form-group">
          <view class="title">作业编号:</view>
          {{form.JOB_NUMBER}}
        </view>
        <view class="cu-form-group">
          <view class="title">作业类型:</view>
          {{ form.JOB_TYPE_NAME }}
        </view>
        <view class="cu-form-group">
          <view class="title">动火许可证:</view>
          <button class="cu-btn bg-blue margin-tb-sm lg" @click="goToDelay()">查看</button>
        </view>
      </view>
      <view class="wui-form-list">
        <view class="cu-form-group ">
          <view class="title text-hui">是否确认通过</view>
          <radio-group class="selected"  @change="radioType" :disabled="forbidEdit">
            <view class="group mr20">
              <radio class='radio' :disabled="forbidEdit" value="2" :checked="form.TYPE==2"></radio>
              <text>通 过</text>
            </view>
            <view class="group">
              <radio class='radio' :disabled="forbidEdit" value="1" :checked="form.TYPE==1"></radio>
              <text>不通过</text>
            </view>
          </radio-group>
        </view>
        <div v-if="form.TYPE ==2">
          <view class="cu-form-group">
            <view class="title">项目主管部门:</view>
            <block v-if="!forbidEdit">
              <view class="picker-tree-box">
                <view class="picker-tree" @tap="showZgTree('tkiTree6')">
                  {{ form.projectSupervisor_DEPT_NAME || '请选择' }}
                </view>
              </view>
              <tki-tree ref="tkiTree6" :selectParent=true :range="treeNode" rangeKey="name" @confirm="zgtreeConfirm($event,'projectSupervisorList')"
                        @cancel="zgtreeCancel"></tki-tree>
            </block>
            <block v-else>{{ form.projectSupervisor_DEPT_NAME }}</block>
          </view>
          <view class="cu-form-group" v-if="form.projectSupervisor_DEPT_NAME">
            <view class="title">项目主管人员:</view>
            <picker @change="projectChange" :disabled="forbidEdit" :value="form.projectSupervisor_INDEX"
                    :range="projectSupervisorList" range-key="NAME">
              <view class="picker">
                {{ form.projectSupervisor_USER_NAME || '请选择' }}
              </view>
            </picker>
          </view>
          <view class="cu-form-group" v-if="form.projectSupervisor_PHONE">
            <view class="title">项目主管人员电话:</view>
            {{ form.projectSupervisor_PHONE }}
          </view>
        </div>
      </view>
    </view>

    <view class="cu-bar btn-group" style="margin-top: 30upx;">
      <button v-if="!forbidEdit" class="cu-btn bg-blue margin-tb-sm lg" @click="goSubmitPerson('1')">提交</button>
      <button v-if="forbidEdit" class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goback)">返回</button>
    </view>
    <view class="padding flex flex-direction"></view>
  </view>
</template>

<script>
  import {
    basePath,
    loginUserId,
    loginSession,
    corpinfoId,
    baseImgPath
  } 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,
        forbidEdit: false,
        form: {
          HOTWORKAPPLICATION_ID: '',
          projectSupervisor_DEPT_ID:'',
          projectSupervisor_DEPT_NAME:'',
          projectSupervisor_USER_ID:'',
          projectSupervisor_USER_NAME:'',
          projectSupervisor_PHONE:'',
          projectSupervisor_INDEX:'',
        },
        treeNode: [],
         projectSupervisorList:[]
      }
    },
    onLoad(event) {
      this.form.HOTWORKAPPLICATION_ID = event.id
      this.getData();
      loginSession();
      this.getDept();
    },
    methods: {
      getData() {
        var _this = this;
        uni.showLoading({
          title: '请稍候'
        })
        uni.request({
          url: basePath + '/app/hotworkapplication/goEdit',
          method: 'POST',
          header: {
            'Content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            HOTWORKAPPLICATION_ID: _this.form.HOTWORKAPPLICATION_ID,
          },
          success: (res) => {
            if ("success" == res.data.result) {
              this.form = res.data.pd;
              this.form.TYPE = '1'
              let HOT_WORK_OPERATOR_NAME = res.data.pd.HOT_WORK_OPERATOR_NAME
              this.form.HOT_WORK_OPERATOR_NAME = HOT_WORK_OPERATOR_NAME.substring(0, HOT_WORK_OPERATOR_NAME.length - 1).split(',');
              uni.hideLoading();
            } else if ("exception" == res.data.result) {
              uni.showToast({
                title: '错误',
                duration: 2000
              });
            }
          }
        });
      },
      getDept(){
        let _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();
      },
      zgtreeCancel(e) {
        this.isUps = false;
      },
      zgtreeConfirm(e,list) {
        if (list === "projectSupervisorList") {
          this.form.projectSupervisor_DEPT_ID= e[0].id;
          this.form.projectSupervisor_DEPT_NAME= e[0].name;
          this.form.projectSupervisor_USER_ID= '';
          this.form.projectSupervisor_USER_NAME= '';
          this.form.projectSupervisor_INDEX= '';
          this.$forceUpdate();
        }
        this.getUserList(list,e[0].id);
      },
      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
              });
            }
          }
        })
      },
      projectChange(e){
        this.form.projectSupervisor_INDEX = e.detail.value;
        this.form.projectSupervisor_USER_ID = this.projectSupervisorList[e.detail.value].USER_ID;
        this.form.projectSupervisor_USER_NAME = this.projectSupervisorList[e.detail.value].NAME;
        this.form.projectSupervisor_PHONE = this.projectSupervisorList[e.detail.value].USERNAME;
        this.$forceUpdate();
      },
      goToDelay() {
        uni.navigateTo({
          url: '/pages/application/eight-assignments/super-hotwork/detail?id=' + this.form.HOTWORKAPPLICATION_ID
        });
      },
      radioType(e){
        this.form.TYPE = e.detail.value
        this.$forceUpdate();
      },
      goSubmitPerson(STATUS) {
        console.info('STATUS+'+STATUS)
        var _this = this;
        let required = true
        if (STATUS == 1) {
          console.info(this.form)
          if (this.form.TYPE == '2') {
            if (!this.form.projectSupervisor_DEPT_ID) {
              uni.showToast({
                icon: 'none',
                title: '请选择动项目主管部门',
                duration: 1500
              });
              required = false
            }
            if (!this.form.projectSupervisor_USER_ID) {
              uni.showToast({
                icon: 'none',
                title: '请选择部门人员',
                duration: 1500
              });
              required = false
            }
          }
          if (!required) {
            return
          }
        }

        uni.showLoading({
          title: '请稍候'
        })
        uni.request({
          url: basePath + "/app/hotworkapplication/editOpinion",
          method: 'POST',
          dataType: 'json',
          header: {
            'Content-type': 'application/x-www-form-urlencoded'
          },

          data: {
            ...this.form,
            CORPINFO_ID: corpinfoId,
            loginUserId:loginUserId
          },
          success: (res) => {
            uni.showToast({
              icon: 'none',
              title: '保存成功',
              duration: 2000
            });
            _this.goback()
          },
          fail: (err) => {
            uni.hideLoading();
            uni.showModal({
              content: err.errMsg,
              showCancel: false
            });
          }
        })
      },
      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;
  }
  .selected{
    display: flex;
    align-items: center;
    height: 100upx;
  }
  .selected .radio{
    transform:scale(0.8);
    margin-right: 10upx;
  }
</style>