<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>
                </view>
                <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">动火单位作业负责人确认</view>
                </view>
                <view class="cu-form-group">
                    <view class="title">动火单位作业负责人:</view>
                    {{ form.projectCompetent_USER_NAME }}
                </view>
                <view class="cu-form-group">
                    <view class="title">确认时间:</view>
                    {{ form.projectCompetent_OPERATTIME }}
                </view>

                <view class="cu-form-group">
                    <view class="title">是否通过:</view>
                    {{form.projectCompetent_TYPE =='2'?'通过':'不通过'}}
                </view>
            </view>
            <view class="wui-form-list">
                <view class="cu-form-group">
                    <view class="title">项目主管人员初审</view>
                </view>
                <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.projectPreliminarily_DEPT_NAME || '请选择' }}
                                </view>
                            </view>
                            <tki-tree ref="tkiTree6" :selectParent=true :range="treeNode" rangeKey="name"
                                      @confirm="zgtreeConfirm($event,'projectPreliminarilyList')"
                                      @cancel="zgtreeCancel"></tki-tree>
                        </block>
                        <block v-else>{{ form.projectPreliminarily_DEPT_NAME }}</block>
                    </view>
                    <view class="cu-form-group" v-if="form.projectPreliminarily_DEPT_NAME">
                        <view class="title">项目主管部门负责人:</view>
                        <picker @change="projectChange" :disabled="forbidEdit" :value="form.projectPreliminarily_INDEX"
                                :range="projectPreliminarilyList" range-key="NAME">
                            <view class="picker">
                                {{ form.projectPreliminarily_USER_NAME || '请选择' }}
                            </view>
                        </picker>
                    </view>
                    <view class="cu-form-group" v-if="form.projectPreliminarily_PHONE">
                        <view class="title">项目主管部门负责人电话:</view>
                        {{ form.projectPreliminarily_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,
        loginUser
    } 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: {
                    TYPE: '1',
                    HOTWORKAPPLICATION_ID: '',
                    projectPreliminarily_DEPT_ID: '',
                    projectPreliminarily_DEPT_NAME: '',
                    projectPreliminarily_USER_ID: '',
                    projectPreliminarily_USER_NAME: '',
                    projectPreliminarily_PHONE: '',
                    projectPreliminarily_INDEX: '',
                },
                treeNode: [],
                projectPreliminarilyList: []
            }
        },
        onLoad(event) {
            this.form.HOTWORKAPPLICATION_ID = event.id
            this.getData();
            loginSession();
            this.getDept();
            this.getDeptCharge(loginUser);
        },
        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 === "projectPreliminarilyList") {
                    this.form.projectPreliminarily_DEPT_ID = e[0].id;
                    this.form.projectPreliminarily_DEPT_NAME = e[0].name;
                    this.form.projectPreliminarily_USER_ID = '';
                    this.form.projectPreliminarily_USER_NAME = '';
                    this.form.projectPreliminarily_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.projectPreliminarily_INDEX = e.detail.value;
                this.form.projectPreliminarily_USER_ID = this.projectPreliminarilyList[e.detail.value].USER_ID;
                this.form.projectPreliminarily_USER_NAME = this.projectPreliminarilyList[e.detail.value].NAME;
                this.form.projectPreliminarily_PHONE = this.projectPreliminarilyList[e.detail.value].USERNAME;
                this.$forceUpdate();
            },
            goToDelay(id) {
                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) {
                var _this = this;
                let required = true
                if (STATUS == 1) {
                    if (this.form.TYPE == 2) {
                        if (!this.form.projectPreliminarily_DEPT_ID) {
                            uni.showToast({
                                icon: 'none',
                                title: '请选择动火项目主管部门',
                                duration: 1500
                            });
                            required = false
                        }
                        if (!this.form.projectPreliminarily_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
                        });
                    }
                })
            },
            getDeptCharge(loginUser){ //获取部门的主要负责人
                //就是获取人员信息
                var _this = this;
                uni.request({
                    method: 'POST',
                    dataType: 'json',
                    header: {
                        'Content-type': 'application/x-www-form-urlencoded'
                    },
                    url: basePath + '/app/sys/goUserByLeaderOrCharge',
                    data: {
                        DEPARTMENT_ID:loginUser.DEPARTMENT_ID,
                        USERNAME:loginUser.USERNAME,
                        tm: new Date().getTime()
                    },
                    success: function (res) {
                        if ("success" == res.data.result){
                            _this.form.projectPreliminarily_DEPT_ID = res.data.departPerson.DEPARTMENT_ID;
                            _this.form.projectPreliminarily_DEPT_NAME =res.data.departPerson.deptName;
                            _this.form.projectPreliminarily_USER_ID = res.data.departPerson.USER_ID;
                            _this.form.projectPreliminarily_USER_NAME = res.data.departPerson.NAME;
                            _this.form.projectPreliminarily_PHONE = res.data.departPerson.USERNAME;
                            _this.getUserList('projectPreliminarilyList', res.data.departPerson.DEPARTMENT_ID);
                        } else {
                            uni.showToast({
                                title: res.data.message,
                                duration: 2000
                            });
                        }
                    }
                })
            },
            goback() {
                uni.navigateBack({
                    delta: 1
                });
                uni.hideLoading();
            },
        }
    }
</script>

<style>
    .right_icon {
        position: relative;
        padding-right: 36 upx;
        width: 200 upx;
        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: -28 upx;
        bottom: 0;
        right: -22 upx;
        margin: auto;
    }

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

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

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

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

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

    .checkbox-item:last-child {
        border-bottom: none;
    }

    .selected {
        display: flex;
        align-items: center;
        height: 100 upx;
    }

    .selected .radio {
        transform: scale(0.8);
        margin-right: 10 upx;
    }
</style>