<template>
    <div class="page">
        <headNav title="消防监督记录"></headNav>

        <div class="content">
            <div id="file" class="file" v-if="formData">
                <div class="one">
                    <div class="one-title">{{ formData.enforce_org_name }}</div>
                    <div class="one-title">消防监督检查记录</div>
                    <div class="job_no">
                        <span>编号：{{ pdfNumberRes }}</span>
                    </div>
                    <div class="format">
                        <div>检查形式：</div>
                        <div class="box">
                            <van-radio-group disabled v-model="checkBox">
                                <van-radio icon-size="16px" shape="square" name="1">消防监督抽查</van-radio>
                                <van-radio icon-size="16px" shape="square" name="2">公共聚集场所投入使用，营业前消防安全检查</van-radio>
                            </van-radio-group>
                        </div>
                    </div>
                    <van-row class="info">
                        <van-col span="10">
                            <span>被检查单位(场所)名称：</span>
                        </van-col>
                        <van-col span="14" class="showLine">
                            <input class="info-input" v-if="showSureBtn" v-model="formData.enforce_obj_name" placeholder="请输入" />
                            <span v-else class="underline">{{ formData.enforce_obj_name }}</span>
                        </van-col>
                    </van-row>
                    <van-row class="info">
                        <van-col span="4">
                            <span>地 址：</span>
                        </van-col>
                        <van-col span="20" @click="showPickerMap">
                            <span class="underline">{{ formData.enforce_obj && formData.enforce_obj.enforce_obj_address }}</span>
                            <van-icon v-if="showSureBtn" name="location-o" />
                        </van-col>
                    </van-row>
                    <van-row class="info">
                        <van-col span="7">消防安全责任人：</van-col>
                        <van-col span="8" class="showLine">
                            <input class="info-input" v-if="showSureBtn" v-model="formData.enforce_obj.charge_person" placeholder="请输入" />
                            <template v-else>
                                {{ formData.enforce_obj && formData.enforce_obj.charge_person }} &nbsp;&nbsp;
                            </template>
                        </van-col>
                        <van-col span="3">电话：</van-col>
                        <van-col span="6" class="showLine">
                            <input class="info-input" v-if="showSureBtn" v-model="formData.enforce_obj.charge_person_phone" placeholder="请输入" />
                            <template v-else>
                                {{ formData.enforce_obj && formData.enforce_obj.charge_person_phone }}
                            </template>
                        </van-col>
                    </van-row>
                    <van-row class="info">
                        <van-col span="7">消防安全管理人：</van-col>
                        <van-col span="8" class="showLine">
                            <input class="info-input" v-if="showSureBtn" v-model="formData.enforce_obj.manage_person" placeholder="请输入" />
                            <template v-else>
                                {{ formData.enforce_obj && formData.enforce_obj.manage_person }} &nbsp;&nbsp;
                            </template>
                        </van-col>
                        <van-col span="3">电话：</van-col>
                        <van-col span="6" class="showLine">
                            <input class="info-input" v-if="showSureBtn" v-model="formData.enforce_obj.manage_person_phone" placeholder="请输入" />
                            <template v-else>
                                {{ formData.enforce_obj && formData.enforce_obj.manage_person_phone }}
                            </template>
                        </van-col>
                    </van-row>
                    <van-row class="info">
                        <van-col span="5">联 系 人：</van-col>
                        <van-col span="10" class="showLine">
                            <input class="info-input" v-if="showSureBtn" v-model="formData.enforce_obj.legal_person" placeholder="请输入" />
                            <template v-else>
                                {{ formData.enforce_obj && formData.enforce_obj.legal_person }} &nbsp;&nbsp;
                            </template>
                        </van-col>
                        <van-col span="3">电话：</van-col>
                        <van-col span="6" class="showLine">
                            <input class="info-input" v-if="showSureBtn" v-model="formData.enforce_obj.legal_person_phone" placeholder="请输入" />
                            <template v-else>
                                {{ formData.enforce_obj && formData.enforce_obj.legal_person_phone }}
                            </template>
                        </van-col>
                    </van-row>
                    <div class="info info1">
                        <van-radio-group v-model="checkBox" disabled direction="horizontal">
                            <van-radio icon-size="16px" shape="square" name="1">消防安全重点单位</van-radio>
                            <van-radio icon-size="16px" shape="square" name="2">非消防安全重点单位</van-radio>
                        </van-radio-group>
                    </div>
                    <van-row class="info">
                        <van-col span="8">
                            <span>消防监督检查员：</span>
                        </van-col>
                        <van-col span="16" class="showLine">
                            {{ formData.executor_names }}
                            <!-- {{ loginUser && loginUser.userName }} -->
                        </van-col>
                    </van-row>
                    <van-row class="info">
                        <van-col span="5">
                            <span>检查时间：</span>
                        </van-col>
                        <van-col span="10" class="df">
                            <div class="showLine center">{{checkDate[0]}}</div>年
                            <div class="showLine center">{{checkDate[1]}}</div>月
                            <div class="showLine center">{{checkDate[2]}}</div>日
                            <div class="showLine center">{{checkDate[3]}}</div>时
                        </van-col>
                    </van-row>
                    <van-row class="info info1">
                        <van-col span="15">
                            <span>被检查单位随网检查人员（签名）：</span>
                        </van-col>
                        <van-col span="9" class="showLine">&nbsp;&nbsp;</van-col>
                    </van-row>
                    <div class="tips">
                        此记录由消防机构存档
                    </div>
                </div>

                <table class="table" border="1" width="100%" cellpadding="0" cellspacing="0">
                    <template v-for="(page, index) in allNodes">
                        <div :key="index" class="one">
                            <!-- :class="(index == 0 || index == 2)?'one-scale':''" -->
                            <template v-if="index === 0">
                                <tr>
                                    <th width="30" colspan="2"></th>
                                    <th class="brd">监 督 检 查 内 容 和 情 况</th>
                                </tr>
                            </template>
                            <tr v-for="(node, nodeIndex) in page" :key="nodeIndex">
                                <!-- <template v-if="node.childs && node.childs.length > 0"> -->
                                <!-- </template> -->
                                <template v-if="node.isSep">
                                    <template v-if="node.trs">
                                        <td class="td2 td3" :rowspan="node.trs">
                                            <span class="nodeName">{{ node.names[0] }}</span>
                                        </td>
                                    </template>
                                    <td class="td2">
                                        <span class="nodeName">{{ node.names[1] }}</span>
                                    </td>
                                </template>
                                <td v-else colspan="2" class="td">
                                    <span class="nodeName">{{ node.name }}</span>
                                </td>
                                <td class="td1">
                                    <template v-if="node.name == '消防许可及验收备案'">
                                        <div v-if="!showSureBtn" class="custom">
                                            <span>被检查建筑物名称:</span><span class="showLine customInfo">{{customInfo.name}}</span>
                                            <van-radio-group v-model="customInfo.check">
                                                <van-radio icon-size="10px" shape="square" name="0">1998年9月1日之前竣工建筑且此后来改建(含装修、用途变更)</van-radio>
                                                <van-radio icon-size="10px" shape="square" name="1">依法通过消防验收</van-radio>
                                                <van-radio icon-size="10px" shape="square" name="2">依法进行竣工验收消防备案</van-radio>
                                                <van-radio icon-size="10px" shape="square" name="3"><span>其他情况:</span><span class="showLine customInfo">{{customInfo.other}}</span></van-radio>
                                            </van-radio-group>
                                        </div>
                                        <div v-else class="custom">
                                            <van-field class="custom-name" v-model="customInfo.name" label="被检查建筑物名称" placeholder="请输入" />
                                            <van-radio-group v-model="customInfo.check">
                                                <van-radio icon-size="10px" shape="square" name="0">1998年9月1日之前竣工建筑且此后来改建(含装修、用途变更)</van-radio>
                                                <van-radio icon-size="10px" shape="square" name="1">依法通过消防验收</van-radio>
                                                <van-radio icon-size="10px" shape="square" name="2">依法进行竣工验收消防备案</van-radio>
                                                <van-radio icon-size="10px" shape="square" name="3"><van-field class="custom-name" v-model="customInfo.other" label="其他情况" placeholder="请输入" /></van-radio>
                                            </van-radio-group>
                                        </div>
                                    </template>
                                    <template v-for="item in node.childs">
                                        <van-row class="check" :key="item.id">
                                            <van-col span="5" v-if="item.showInvolvedResult">
                                                <van-radio-group disabled value="0">
                                                     <!-- v-if="item.check_result == 0"  -->
                                                    <van-radio icon-size="10px" shape="square" name="0">不涉及</van-radio>
                                                </van-radio-group>
                                            </van-col>
                                            <van-col :span="item.showInvolvedResult ? 19 : 24" class="check-item">
                                                <template v-if="item.showInputFirst">
                                                    <span class="check-name">{{ item.enforce_content }}</span>
                                                    <div class="custom-other" v-if="item.other_enforce_item">
                                                        <div style="display: flex;" v-for="(other, otherInd) in item.other_enforce_item" :key="otherInd">
                                                            <div>{{ other.k }}</div>
                                                            <div class="showLine center">{{ other.v }}</div>
                                                        </div>
                                                    </div>
                                                    <span class="check-name ml10">{{ item.enforce_otherInfo }}</span>
                                                    <div class="custom-option" v-if="item.result_options">
                                                        <van-radio-group disabled direction="horizontal" v-model="item.checkOption">
                                                            <van-radio icon-size="10px" shape="square" v-for="(option, optionInd) in item.result_options" :key="optionInd" :name="option">{{option}}</van-radio>
                                                        </van-radio-group>
                                                    </div>
                                                </template>
                                                <template v-else>
                                                    <template v-if="item.showLine">
                                                        <div class="other-fire1">
                                                            <div class="custom-line"></div>
                                                            <!-- <van-divider /> -->
                                                            <div class="custom-other">
                                                                <div class="custom-option" v-if="item.result_options">
                                                                    <van-radio-group disabled direction="horizontal" v-model="item.checkOption">
                                                                        <van-radio icon-size="10px" shape="square" v-for="(option, optionInd) in item.result_options" :key="optionInd" :name="option">{{option}}</van-radio>
                                                                    </van-radio-group>
                                                                </div>
                                                                <span class="check-name">{{ item.enforce_content }}</span>
                                                            </div>
                                                            <div class="other-fire" v-if="item.other_enforce_item">
                                                                <div class="ml10" v-for="(other, otherInd) in item.other_enforce_item" :key="otherInd">
                                                                    <span class="check-name">{{ other.k }}</span>
                                                                    <span class="showLine">{{ other.v }}</span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </template>
                                                    <template v-else>
                                                        <div class="custom-option" v-if="item.result_options">
                                                            <van-radio-group disabled direction="horizontal" v-model="item.checkOption">
                                                                <span class="check-name">{{ item.enforce_content }}</span>
                                                                <van-radio icon-size="10px" shape="square" v-for="(option, optionInd) in item.result_options" :key="optionInd" :name="option">{{option}}</van-radio>
                                                            </van-radio-group>
                                                        </div>
                                                        <span v-else class="check-name">{{ item.enforce_content }}</span>
                                                        <div class="custom-other" v-if="item.other_enforce_item">
                                                            <div style="display: flex;" v-for="(other, otherInd) in item.other_enforce_item" :key="otherInd">
                                                                <div>{{ other.k }}</div>
                                                                <div class="showLine">{{ other.v }}</div>
                                                            </div>
                                                        </div>
                                                    </template>
                                                </template>
                                            </van-col>
                                        </van-row>
                                        <van-row v-if="node.isAutoFire && item.isLast" :key="item.id + 'other'" class="check-result check-result2">
                                            <van-col span="5" class="check-title">检查情况：</van-col>
                                            <van-col span="18">
                                                <div v-for="(otherFire, otherFireIndex) in autoFireSystemTopResult" :key="otherFireIndex">
                                                    <span>{{otherFire}}</span>
                                                </div>
                                            </van-col>
                                        </van-row>
                                    </template>
                                    <template v-if="node.isMark">
                                        <template v-if="showSureBtn">
                                            <van-field
                                                v-model="remarkInput"
                                                rows="1"
                                                autosize
                                                type="textarea"
                                                placeholder="请输入"
                                            />
                                        </template>
                                        <template v-else>
                                            <div v-html="remarkInputStr"></div>
                                        </template>
                                    </template>
                                    <van-row v-else class="check-result">
                                        <van-col span="6" class="check-title">检查情况：</van-col>
                                        <van-col span="18">
                                            <div v-for="(checkInfo, checkNodesIndex) in node.checkInfos" :key="checkNodesIndex">
                                                <span>{{ checkInfo }}</span>
                                                <!-- <span class="check-result1">{{ checkNodes.result }}</span> -->
                                            </div>
                                        </van-col>
                                    </van-row>
                                </td>
                            </tr>
                        </div>
                    </template>
                </table>
            </div>

            <div class="btn">
                <van-button type="primary" @click="backToExecute">上一步</van-button>
                <van-button type="info" @click="commitRecord">确定</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 { get, put } from "@/utils/http.js";
import { getInfo } from "@/utils/auth";
import { dateFormat, notEmpty, isEmpty, isMobile } from '@/utils/tool';
import { getPdf } from "@/utils/htmlToPDF.js";
import recordSign from '@/mixins/recordSign';
import pdfNumber from '@/mixins/pdfNumber';
import Bus from '@/bus';
import { Notify } from 'vant';
import executeStepMixin from '@/mixins/executeStep';

export default {
    components: {
        headNav,
        "v-mask": mask_overLay,
    },
    inject: ['reload'],
    mixins: [recordSign, pdfNumber, executeStepMixin],
	beforeRouteEnter: (to, from, next) => {
		if (from.name == 'executeTask') {
			next(vm => {
				if (vm._data.formData) {
					vm.reload();
				}
			});
		} else {
			next();
		}
	},
    data () {
        return {
            showOverLay: false,
            tipMsg: "加载中",
            formData: null,
            checkBox: '1',
            checkDate: ['', '', '', ''],
            allNodes: [],
            customInfo: {
                name: '',
                check: '',
                other: ''
            },
            showSureBtn: true,
            autoFireSystemTopResult: [],
            remarkInput: '',
        }
    },
    computed: {
        jobId() {
            return this.$route.params.currentID;
        },
        loginUser() {
            const user = getInfo();
            if (user) {
                return user.userInfo;
            }
            return null;
        },
        remarkInputStr() {
            return this.remarkInput.replace(/\n/g, '<br>')
        }
    },
    mounted () {
        if (this.jobId) {
            this.initDatas();
            document.body.style.fontSize = '24px';
        }
    },
    methods: {
        backToExecute() {
            this.$router.replace(`/special/exec/${this.jobId}`);
        },
        async initDatas() {
            this.showOverLay = true;
            this.loadPdfNumber('record');
            this.formData = await get(`/v2/api/enforce-job/${this.jobId}`);
            if (this.formData.start_time) {
                this.checkDate = dateFormat('YYYY-mm-dd-HH', new Date(this.formData.start_time)).split('-');
            }
            // const books = await get(`/v2/api/enforce-handbook?page=1&size=100000&sort=sort asc&query=enforce_type==0`);
            const books = await get(`/v2/api/enforce_template_handbook/copy/job/${this.jobId}`);
            // const books = await get(`/v2/api/enforce_template_handbook/job/${this.jobId}`);
            // console.log('books: ', books);
            if (books) {
                let list = []; //repeatList = [];
                let set = new Set();
                books.map(book => {
                    if (book.item_type == '0') {
                        if (book.enforce_class && book.enforce_content) {
                            const target = book.enforce_class + '' + book.enforce_content;
                            if (set.has(target)) {
                                const item = this.getCheckResult(book);
                                // if (item.check_result == '2') {
                                    // repeatList.push(book);
                                    this.setupRepeatItemResult(item, list, target);
                                // }
                            }
                            else {
                                set.add(target);
                                list.push(book);
                            }
                            // console.log(target);
                        }
                    }
                    else {
                        list.push(book);
                    }
                });
                this.handleBookData(list);
            }
            this.updateTaskStep(this.jobId, 'RECORD');
            this.showOverLay = false;
        },
        setupRepeatItemResult(item, list, compare) {
            if (item && compare && list && list.length > 0) {
                const res = list.filter(obj => compare == `${obj.enforce_class}${obj.enforce_content}`);
                let arr = [], record = [];
                res.map(obj => {
                    // obj.checkOption = item.check_options || '';
                    const target = this.getCheckResult(obj);
                    if (target) {
                        if (item.check_result == 2 && notEmpty(item.check_options)) {
                            target.check_options = item.check_options;
                            target.check_result = 2;
                        }
                        const targetInput = this.getItemInputItems(target);
                        const itemInput = this.getItemInputItems(item);
                        arr = [];
                        for (let i = 0; i < targetInput.length && i < itemInput.length; i++) {
                            arr.push(`${targetInput[i]} / ${itemInput[i]}`);
                        }
                        target.enforce_job_node_item = arr.join(',');
                        record = target.records || [];
                        if (item.records) {
                            record = [...record, ...item.records];
                            target.records = record;
                        }
                    }
                });
            }
        },
		showPickerMap() {
            if (this.showSureBtn) {
                this.$router.push('/pickPlace');
                const vm = this;
                Bus.$on('changePosition', data => {
                    if (data) {
                        vm.formData.enforce_obj.enforce_obj_address = data.address;
                        vm.formData.enforce_obj.longitude = '' + data.point.lng;
                        vm.formData.enforce_obj.latitude = '' + data.point.lat;
                    }
                });
            }
		},
        getItemInputItems(result) {
            const res = result && result.enforce_job_node_item && result.enforce_job_node_item.replace(/，/g, ',') || '';
            return res.split(',');
        },
        handleBookItem(data) {
            const result = this.getCheckResult(data);
            if (result && (result.check_result == 1 || result.check_result == 2)) {
                data.check_result = result.check_result;
                data.showInvolvedResult = false;
            }
            else {
                data.showInvolvedResult = true;
                data.check_result = 0;
            }
            if (data.enforce_content.indexOf('值班操作人员') >= 0) {
                data.showInputFirst = true;
                data.enforce_content = '值班操作人员';
                data.enforce_otherInfo = '值班记录';
            }
            else {
                data.showInputFirst = false;
            }
            if (data.enforce_content.indexOf('其他自动灭火系统') >= 0) {
                data.showLine = true;
            }
            else {
                data.showLine = false;
            }
            if (data.result_options) {
                const options = []
                data.result_options = data.result_options.replace(/，/g, ',');
                data.result_options.split(',').map(item => {
                    if (item && item.length > 0) {
                        let str = item.trim().replace('/', '');
                        str = str.replace('#', ',');
                        options.push(str);
                    }
                })
                // if (!data.checkOption || data.checkOption.length == 0) {
                data.checkOption = result ? result.check_options : [];
                // }
                data.result_options = options;
            }
            if (data.other_enforce_item) {
                const list = [];
                const resultOthers = this.getItemInputItems(result);
                const others = data.other_enforce_item.replace('，', ',');
                const otherItems = others.split(',');
                otherItems.map((other, index) => {
                    const item = other.replace('/', '');
                    if (item && item.length > 0) {
                        const obj = {
                            k: item,
                            v: ''
                        };
                        if (resultOthers && resultOthers.length > index) {
                            obj.v = resultOthers[index];
                        }
                        if (data.check_result == 0) {
                            obj.v = data.showInputFirst ? '  /' : '不涉及';
                        }
                        list.push(obj);
                    }
                });
                if (data.check_result == 0) {
                    data.showInvolvedResult = false;
                }
                data.other_enforce_item = list
            }
        },
        getCheckResult(data) {
            if (this.formData && this.formData.check_nodes) {
                for (let i = 0; i < this.formData.check_nodes.length; i++) {
                    const node = this.formData.check_nodes[i];
                    const items = node.items || [];
                    for (let j = 0; j < items.length; j++) {
                        const item = items[j];
                        if (data.id === item.item_id) {
                            return item;
                        }
                    }
                }
            }
            return null;
        },
        getCategoryCheck(obj, arr) {
            let res = [];
            const vm = this;
            if (obj && arr && arr.length > 0) {
                obj.isAutoFire = false;
                let haveRight = false, haveError = false;
                const isOtherFire = arr.filter(item => item.enforce_content.indexOf('其他自动灭火系统') >= 0);
                if (isOtherFire.length > 0) {
                    obj.isAutoFire = true;
                    arr.map((item, index) => {
                        item.isLast = index == arr.length - 2;
                        const checkItem = vm.getCheckResult(item);
                        if (checkItem) {
                            if (item.enforce_content.indexOf('其他自动灭火系统') >= 0) {
                                if (!checkItem.check_result || checkItem.check_result == 0) {
                                    obj.checkInfos = ['不涉及'];
                                }
                                if (checkItem.check_result == 1) {
                                    obj.checkInfos = ['未见异常'];
                                }
                                if (checkItem.check_result == 2) {
                                    const checkInfo = vm.getCheckItemRecord(checkItem);
                                    obj.checkInfos = [`${item.enforce_content}-不合格：${checkInfo}`];
                                }
                            }
                            else {
                                // 如果监督检查记录表那里一个大类没有任何一个检查项检查了，检查内容显示不涉及；
                                // 如果一个大类的检查内容都合格，且检查内容未填写，监督检查记录表的检查内容显示未见异常
                                // if (unrelated && (checkItem.check_result == 1 || checkItem.check_result == 2)) {
                                //     unrelated = false;
                                // }
                                // if (allRight && checkItem.check_result != 1) {
                                //     allRight = false;
                                // }
                                const checkInfo = vm.getCheckItemRecord(checkItem);
                                // if (allRight && notEmpty(checkInfo)) {
                                //     allRight = false;
                                // }
                                if (!haveRight && checkItem.check_result == 1 && isEmpty(checkInfo)) {
                                    haveRight = true;
                                }
                                if (!haveError && checkItem.check_result == 2) {
                                    haveError = true;
                                }
                                if (checkItem.check_result == 1 && notEmpty(checkInfo)) {
                                    // 0-不涉及,1-合格,2-不合格
                                    res.push(`${item.enforce_content}-合格：${checkInfo}`);
                                }
                                if (checkItem.check_result == 2) {
                                    res.push(`${item.enforce_content}-不合格：${checkInfo}`);
                                }
                            }
                        }
                        else if (item.enforce_content.indexOf('其他自动灭火系统') >= 0) {
                            obj.checkInfos = ['不涉及'];
                        }
                    });
                    if (!haveError) {
                        if (haveRight) {
                            res = ['未见异常'];
                        }
                        if (res.length == 0) {
                            res = ['不涉及'];
                        }
                    }
                    // if (allRight) {
                    //     res = ['未见异常'];
                    // }
                    // if (unrelated || res.length == 0) {
                    //     res = ['不涉及'];
                    // }
                    vm.autoFireSystemTopResult = res;
                }
                else {
                    arr.map(item => {
                        const checkItem = vm.getCheckResult(item);
                        if (checkItem) {
                            // if (unrelated && (checkItem.check_result == 1 || checkItem.check_result == 2)) {
                            //     unrelated = false;
                            // }
                            // if (allRight && checkItem.check_result != 1) {
                            //     allRight = false;
                            // }
                            const checkInfo = vm.getCheckItemRecord(checkItem);
                            if (!haveRight && checkItem.check_result == 1 && isEmpty(checkInfo)) {
                                haveRight = true;
                            }
                            if (!haveError && checkItem.check_result == 2) {
                                haveError = true;
                            }
                            // if (allRight && notEmpty(checkInfo)) {
                            //     allRight = false;
                            // }
                            // if (checkItem.check_result == 1 || checkItem.check_result == 2) {
                            //     // 0-不涉及,1-合格,2-不合格
                            //     const result = checkItem.check_result == 1 ? '合格' : '不合格';
                            //     res.push(`${item.enforce_content}-${result}：${checkInfo}`);
                            // }
                            if (checkItem.check_result == 1 && notEmpty(checkInfo)) {
                                // 0-不涉及,1-合格,2-不合格
                                res.push(`${item.enforce_content}-合格：${checkInfo}`);
                            }
                            if (checkItem.check_result == 2) {
                                res.push(`${item.enforce_content}-不合格：${checkInfo}`);
                            }
                        }
                    });
                    if (!haveError) {
                        if (haveRight) {
                            res = ['未见异常'];
                        }
                        if (res.length == 0) {
                            res = ['不涉及'];
                        }
                    }
                    // if (allRight) {
                    //     res = ['未见异常'];
                    // }
                    // if (unrelated && res.length == 0) {
                    //     res = ['不涉及'];
                    // }
                    obj.checkInfos = res;
                }
            }
        },
        handleBookData(data) {
            const nodes = [
                [
                    {name: '消防许可及验收备案', childs: [] },
                ],
                [
                    {name: '消防安全管理', childs: []},
                ],
                [
                    {name: '建筑防火', childs: []},
                ],
                [
                    {name: '安全疏散', childs: []},
                    {name: '消防控制室', childs: []},
                ],
                [
                    {name: '消防设施器材-火灾自动报警系统', childs: [], isSep: true, names: ['消防设施器材', '火灾自动报警系统'], trs: 2, },
                    {name: '消防设施器材-消防给水设施', childs: [], isSep: true, names: ['消防设施器材', '消防给水设施'] },
                ],
                [
                    {name: '消防设施器材-自动灭火系统', childs: [], isSep: true, names: ['消防设施器材', '自动灭火系统'], trs: 2, },
                    {name: '消防设施器材-其他设施器材', childs: [], isSep: true, names: ['消防设施器材', '其他设施器材'] },
                ],
                [
                    {name: '其它消防安全管理', childs: []},
                    {name: '备注', childs: [], isMark: true },
                ]
            ];
            const vm = this;
            nodes.map(arr => {
                arr.map(item => {
                    const childs = data.filter(obj => item.name === obj.enforce_class);
                    childs.map(child => {
                        vm.handleBookItem(child);
                    })
                    vm.getCategoryCheck(item, childs);
                    item.childs = childs;
                });
            });
            this.allNodes = nodes;

            if (this.formData.check_nodes) {
                let marks = [];
                // if (repeatItems && repeatItems.length > 0) {
                //     repeatItems.map(item => {
                //         let str = item.enforce_class + ' ' + item.enforce_content;
                //         if (notEmpty(str)) {
                //             const result = this.getCheckResult(item);
                //             const checkInfo = this.getCheckItemRecord(result);
                //             switch (result.check_result) {
                //                 case 1:
                //                     str += ' -合格';
                //                     break;
                //                 case 2:
                //                     str += '-不合格：' + checkInfo;
                //                     break;
                //                 default:
                //                     str += ' -不涉及';
                //                     break;
                //             }
                //             marks.push(str);
                //         }
                //     });
                // }
                this.formData.check_nodes.map(node => {
                    if (node.items) {
                        node.items.map(item => {
                            if (item.item_type == '1') {
                                let str = item.item_content || item.item_name;
                                if (notEmpty(str)) {
                                    const checkInfo = this.getCheckItemRecord(item);
                                    switch (item.check_result) {
                                        case 1:
                                            str += '-合格';
                                            break;
                                        case 2:
                                            str += '-不合格：' + checkInfo;
                                            break;
                                        default:
                                            str += '-不涉及';
                                            break;
                                    }
                                    marks.push(str);
                                }
                            }
                        });
                    }
                });
                this.remarkInput = marks.join('\n');
            }
        },
        getCheckItemRecord(item) {
            let arr = [];
            if (item && item.records && item.records.length > 0) {
                item.records.map(obj => {
                    if (notEmpty(obj.check_desc)) {
                        arr.push(obj.check_desc);
                    }
                });
            }
            return arr.join(' / ');
        },
        async commitRecord() {
            if (isEmpty(this.formData.enforce_obj_name) || this.formData.enforce_obj_name.length > 50) {
                Notify({ type: 'danger', message: '请输入被检查单位(场所)名称[不超过50个字符]' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }
            if (isEmpty(this.formData.enforce_obj.enforce_obj_address)) {
                Notify({ type: 'danger', message: '请选择被检查单位(场所)地址' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }

            if (isEmpty(this.formData.enforce_obj.charge_person) || this.formData.enforce_obj.charge_person.length > 50) {
                Notify({ type: 'danger', message: '请输入消防安全责任人姓名[不超过50个字符]' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }
            if (!isMobile(this.formData.enforce_obj.charge_person_phone)) {
                Notify({ type: 'danger', message: '请输入消防安全责任人11位电话号码' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }
            
            if (isEmpty(this.formData.enforce_obj.manage_person) || this.formData.enforce_obj.manage_person.length > 50) {
                Notify({ type: 'danger', message: '请输入消防安全管理人姓名[不超过50个字符]' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }
            if (!isMobile(this.formData.enforce_obj.manage_person_phone)) {
                Notify({ type: 'danger', message: '请输入消防安全管理人11位电话号码' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }

            if (isEmpty(this.formData.enforce_obj.legal_person) || this.formData.enforce_obj.legal_person.length > 50) {
                Notify({ type: 'danger', message: '请输入联系人姓名[不超过50个字符]' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }
            if (!isMobile(this.formData.enforce_obj.legal_person_phone)) {
                Notify({ type: 'danger', message: '请输入联系人11位电话号码' });
                document.getElementsByClassName('content')[0].scrollTop = 0;
                return;
            }

            // this.updateTaskRecordFile();
            // return;

            this.showOverLay = true;
            this.showSureBtn = false;
            this.tipMsg = "保存单位信息...";
            const objParams = JSON.parse(JSON.stringify(this.formData.enforce_obj));
            await put(`/v2/api/enforce-obj/${this.formData.enforce_obj.id}`, objParams);

            this.tipMsg = "上传pdf中";
            setTimeout(async() => {
                const subData = await getPdf(
                    document.getElementsByClassName("one"),
                    "消防监督检查记录"
                );
                this.startToSign(subData);
            }, 1000);
        },
        updateTaskRecordFile() {
            this.showOverLay = true;
            const json = this.customInfo ? JSON.stringify(this.customInfo) : '';
            const res = this.setupStaticNoticeResult(this.formData, 'record', '', this.remarkInput);
            put(`/v2/api/enforce-job/${this.jobId}`, {
                id: this.dailyId,
                result_file_id: this.formData.result_file_id,
                static_check_result: json,
                static_notice_result: res,
            }).then((res) => {
                this.$router.replace(`/checkResult/${this.jobId}/normal`);
                this.showOverLay = false;
            }).catch(() => {
                this.$toast.fail("上传失败");
            });
        },
    },
}
</script>

<style lang="scss">
.file {
    .van-radio__label--disabled {
        color: #585858;
    }
    .van-radio__icon--disabled.van-radio__icon--checked .van-icon {
        color: #585858;
        border: none;
        margin-right: -4px;
    }
    .van-radio__icon--disabled .van-icon {
        background-color: inherit;
        border-color: #585858;
    }
}
.custom {
    .van-cell {
        font-size: 10px;
        padding: 0;
    }
    .van-cell::after {
        border-bottom: none;
    }
    .van-radio__icon--checked .van-icon {
        background-color: transparent;
        border-color: #323233;
        color: #323233;
    }
    .custom-name {
        .van-cell__title {
            width: 100px;
        }
        .van-field__control {
            width: auto;
        }
        .van-field__body {
            input {
                border-bottom: 1px solid #585858;
            }
        }
    }
    .custom-name:last-child {
        .van-cell__title {
            width: 50px;
        }
    }
}
</style>

<style lang="scss" scoped>
.file {
    background-color: #FFFFFF;
    font-family: arial;
    color: #585858;
    font-size: 10px;
    .one {
        padding: 20px;
        .one-title {
            margin-top: 8px;
            font-size: 16px;
            color: #343435;
            text-align: center;
        }
        .job_no {
            margin-top: 8px;
            display: flex;
            justify-content: flex-end;
            &>span {
                font-size: 14px;
                color: #343435;
            }
        }
        .format {
            margin-top: 20px;
            display: flex;
            .box {
                flex: 1;
            }
        }
        .info {
            margin-top: 10px;
        }
        .info1 {
            margin: 20px 0;
        }
        .tips {
            margin-top: 20px;
        }
    }
    .one {
        margin-top: 20px;
        &>tr:last-child {
            border-bottom: 1px solid #585858;
        }
    }
    .one-scale {
        padding: 40px;
        transform: scale(0.8);
    }
    .table {
        margin-top: 90px;
        border: none;

        tr>td:last-child {
            border-right: 1px solid #585858;
        }
        .brd {
            border-right: 1px solid #585858;
        }

        .td {
            vertical-align: middle;
            text-align: center;
            width: 30px;
            min-width: 30px;
            max-width: 30px;
        }
        .td1 {
            padding: 5px;
            width: calc(100vw - 30px);
        }
        .td2 {
            width: 15px;
            min-width: 15px;
            max-width: 15px;
            vertical-align: middle;
            text-align: center;
        }
        .td3 {
            border-bottom: 1px solid #585858;
        }

        .nodeName {
            font-size: 13px;
            white-space: normal;
            word-break: break-all;
            color: #4d4d4d;
        }
        
        .custom {
            font-size: 10px;
        }
        .custom-option {
            display: flex;
            align-items: center;
        }
        .custom-other {
            display: flex;
            align-items: center;
        }
        .customInfo {
            margin-left: 14px;
            height: 34px;
            line-height: 34px;
        }

        .check {
            display: flex;
            .check-item {
                display: flex;
                flex-wrap: wrap;
            }
        }

        .check-name {
            color: #343435;
            margin-right: 8px;
            font-weight: bolder;
        }

        .check-result {
            // margin-top: 10px;
            .check-title {
                color: #343435;
                font-weight: bolder;
            }
            .check-result1 {
                margin-left: 10px;
            }
        }
        .check-result2 {
            width: 100%;
        }
    }
}

.btn {
    margin: 15px 0;
    display: flex;
    justify-content: space-around;

    button {
        flex: 1;
        margin: 5px 25px;
        height: 40px;
    }
}
.other-fire1 {
    display: flex;
    flex-direction: column;
    width: 100%;
}
.other-fire {
    display: flex;
    flex-direction: column;
}
.custom-line {
    margin: 8px 0 2px -5px;
    width: 103%;
    height: 1px;
    background-color: #585858;
}
.info-input {
    width: 90%;
    border: none;
}
</style>