<template>
    <div>
        <LHeader :text="text"></LHeader>
        <van-form @submit="onSubmit" :scroll-to-error="true" :show-error="false" validate-trigger="onSubmit">
            <div class="title">
                <img src="@/assets/accidentIcon/bookmark.svg" alt="" class="title-img" />
                巡查内容
            </div>
            <div class="content">应建立地下有限空间作业安全生产责任制、安全生产规章制度和操作规程。</div>
            <div class="title">
                <img src="@/assets/accidentIcon/bookmark.svg" alt="" class="title-img" />
                巡查地点
            </div>
            <div class="content">西南厂区一车间西北角</div>

            <div class="list">
                <div class="title">
                    <img src="@/assets/accidentIcon/bookmark.svg" alt="" class="title-img" />
                    异常上报
                </div>
                <van-field name="hdPicture1" label="异常照片">
                    <template #input>
                        <van-uploader @delete="deleteFile" multiple :max-count="5" upload-text="最多上传五个"
                            v-model="form.abnormalPics">
                            <template slot="default">
                                <div class="content-upload">
                                    <img src="@/assets/upload/pic.png" alt="" style="width: 0.64rem; height: 0.64rem" />
                                    <span class="van-uploader__upload-text">最多上传五个</span>
                                </div>
                            </template>
                        </van-uploader>
                    </template>
                </van-field>
                <van-field label="异常说明">
                    <template #input>
                        <van-field v-model="form.abnormalDesc" name="abnormalDesc" label="" type="textarea" rows="3"
                            autosize placeholder="请输入" style="padding-left: 0; padding-top: 0" />
                    </template>
                </van-field>
                <van-field readonly v-model="form.assessmentLevel" required name="assessmentLevel" label="风险等级"
                    placeholder="请选择" @click="seletGrade" :rules="[{ required: true, message: '风险等级不能为空' }]" />
                <div style="margin: 16px 16px 0">
                    <van-button :loading="loadingStatus" loading-text="加载中..." round block type="info"
                        native-type="submit">提交</van-button>
                </div>
            </div>
        </van-form>
        <div style="margin: 10px 16px 0px; padding-bottom: 16px">
            <van-button round block type="warning" @click.native="cancel">取消</van-button>
        </div>
    </div>
</template>

<script>
import LHeader from "@/components/header.vue";
import { getFun, postFun } from "@/service/table.js";
export default {
    name: "addCurrent",
    components: {
        LHeader,
    },
    data() {
        return {
            form: {},
            text: "异常上报"
        };
    },
    mounted() {
        const formData = sessionStorage.getItem('addDanger');
        if (formData) {
            const { level, score } = this.$store.state.assessModel.result
            this.form = JSON.parse(formData);
            this.form.assessmentLevel = level;
            sessionStorage.removeItem('addDanger')
        }
    },
    methods: {
        //风险等级
        seletGrade(name) {
            this.saveFormData()
            this.$router.push('/assess/model/step1')
        },
        saveFormData() {
            sessionStorage.setItem("addDanger", JSON.stringify(this.form));
        },
        jsonToFormData(config) {
            const formData = new FormData();
            //循环传入的值转换formData
            Object.keys(config).forEach((key) => {
                if (key == 'abnormalPics') {
                    config[key].forEach(val => {
                        formData.append('abnormalPics[]', val.file)
                    })
                } else {
                    formData.append(key, config[key]);
                }
            });
            return formData;
        },
        onSubmit() {
            var formDataJson = this.jsonToFormData(this.form);
            postFun('/risk/currentRiskAbnormal/save', formDataJson)
        }
    },
};
</script>
<style lang="less" scoped>
.title {
    height: 1rem;
    font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
    font-weight: 700;
    line-height: 1rem;

    .title-img {
        position: relative;
        top: 32%;
        transform: translateY(-50%)
    }
}

.content {
    background: #fff;
    padding: 14px 20px;
    line-height: 21px;
    font-size: 14px;
}

.content-upload {

    width: 2.13333rem;
    height: 2.13333rem;
    background: #f7f8fa;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;

}

/* @import url(); 引入css类 */
.con-list {
    padding: 0;
    background-color: #f0f1f5;

    .van-cell-group--inset {
        margin: 0;
        margin-bottom: 0.26667rem;
        padding: 0.25rem;
        font-size: 0.4rem;
        position: relative;
        border-radius: 4%;
        box-shadow: 0px 0px 10px 2px #f3f3f3;
        width: 90%;
        margin: 0.4rem auto;

        .van-row {
            font-size: 0.4rem;
            line-height: 0.8rem;
            margin-bottom: 0;
        }

        .van-overlay {
            position: absolute;

            .wrapper {
                display: flex;
                align-items: center;
                justify-content: space-evenly;
                height: 100%;
            }
        }
    }
}

.dialogContain {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
}
</style>
