<template>
    <div class="page">
        <headNav title="检查总结"></headNav>

        <div class="content">
            <van-cell-group title="检查结果">
                <div class="result">
                    <span :class="haveError?'FontColorError':'FontColorSuccess'">{{ haveError ? '检查异常' : '检查正常'}}</span>
                </div>
            </van-cell-group>
            <van-cell-group title="检查判断">
                <div class="check">
                    <template v-if="haveError">
                        <van-checkbox shape="square" v-model="checked" disabled>部分检查情况异常，责令改正</van-checkbox>
                    </template>
                    <template v-else>
                        <van-checkbox shape="square" disabled>检查情况良好，未发现异常隐患情况</van-checkbox>
                    </template>
                </div>
            </van-cell-group>
            <div class="mg">
                <van-button round block type="info" @click="saveCheck">确定</van-button>
            </div>
        </div>

        <v-mask :showOverLay="showOverLay" :tipMsg="tipMsg"></v-mask>
    </div>
</template>

<script>
import headNav from "@/components/headNav.vue";
import mask_overLay from "@/components/overLay_xy.vue";
import { put, post } from '@/utils/http.js';
import { dateFormat, isEmpty } from '@/utils/tool';

export default {
    components: {
        headNav,
        "v-mask": mask_overLay,
    },
    computed: {
        taskId() {
            return this.$route.params.ID
        },
    },
    data () {
        return {
            info: null,
            showOverLay: false,
            tipMsg: '加载中...',
            haveError: false,
            checked: true,
        }
    },
    mounted () {
        if (this.taskId) {
            this.loadTaskDatas();
        }
    },
    methods: {
        async loadTaskDatas() {
            this.showOverLay = true;
            const res = await this.$http.get(`/v2/api/enforce-job/${this.taskId}`);
            if (res.data) {
                this.info = res.data;
                let flag = false;
                if (res.data.check_nodes) {
                    const nodes = res.data.check_nodes;
                    const bookRes = await this.$http.get("/v2/api/enforce-handbook?page=1&size=100000&sort=sort asc&query=enforce_type==1");
                    const bookList = bookRes && bookRes.data && bookRes.data.data || [];
                    for (let index = 0; index < nodes.length; index++) {
                        const node = nodes[index];
                        if (node.items) {
                            for (let j = 0; j < node.items.length; j++) {
                                const item = node.items[j];
                                if (item.item_type == '1') {
                                    
                                }
                                else {
                                    // const bookRes = await this.$http.get(`/v2/api/enforce-handbook/${item.item_id}`);
                                    const bookFilter = bookList.filter(book => book.id == item.item_id);
                                    if (bookFilter.length > 0) {
                                        const book = bookFilter[0];
                                        if (item.check_options && book.result_problem_options) {
                                            flag = book.result_problem_options.indexOf(item.check_options) >= 0;
                                            if (flag) {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (flag) {
                            break;
                        }
                    }
                }
                this.haveError = flag;
                this.showOverLay =  false;
            }
        },
        saveCheck() {
            if (isEmpty(this.taskId)) {
                return;
            }
            this.tipMsg = '提交中...';
            this.showOverLay = true;
            const finishDate = new Date();
            put(`/v2/api/enforce-job/${this.taskId}`, {
                immediately_file_id: this.info.result_file_id,
                finish_time: finishDate,
                job_status: 'ARCHIVED',
            }).then(() => {
                if (this.haveError) {
                    const url = process.env.VUE_APP_API + '/v2/api/file/' + this.info.result_file_id + '/open';
                    const name = this.info.enforce_org_name || '';
                    // const receiver_id = this.info.enforce_obj && this.info.enforce_obj. || '';
                    const date = dateFormat('YYYY年mm月dd日', finishDate);
                    const content = `根据《中华人民共和国消防法》第五十三条的规定，我${name}于${date}对你单位（场所）进行消防监督检查，即刻查看检查结果：${url}`;
                    post(`/v2/api/enforce-job/${this.taskId}/enforce-message`, {
                        id: this.taskId,
                        obj_id:this.info.enforce_obj_id,
                        message_type:'RECTIFY',
                        receiver_type: 'SMS',
                        message_content:content,
                        send_time: finishDate
                    }).then(res => {
                        this.$toast.success('提交成功');
                        this.$router.back();
                    }).catch(e => {
                        this.$toast.fail('提交失败');
                    });
                }
                else {
                    this.$toast.success('提交成功');
                    this.$router.back();
                }
            }).catch(e => {
                this.$toast.fail('提交失败');
            });
        },
    }
}
</script>

<style lang="scss">
.mg {
    margin: 16px;
}
.result {
    display: flex;
    padding: 20px 0;
    font-size: 18px;
    font-weight: bolder;
    display: flex;
    justify-content: center;
}
.check {
    padding: 8px 16px;
    font-size: 14px;
}
</style>