<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>
                <van-field label="风险因素" required readonly>
                    <template #input>
                        <van-field readonly v-model="form.factor" label="" autosize placeholder="请输入" style="padding: 0" />
                    </template>
                </van-field>
                <van-field label="风险类型" readonly required>
                    <template #input>
                        <van-field readonly v-model="form.type" label="" autosize placeholder="请输入" style="padding: 0" />
                    </template>
                </van-field>
                <van-field label="风险等级" required>
                    <template #input>
                        <van-field readonly v-model="form.level" label="" autosize placeholder="请输入" style="padding: 0" />
                    </template>
                </van-field>
                <van-field label="管控措施" readonly required>
                    <template #input>
                        <van-field readonly v-model="form.measuresProject" label="" autosize placeholder="请输入"
                            style="padding: 0" />
                    </template>
                </van-field>
            </div>
            <div class="title">
                <img src="@/assets/accidentIcon/bookmark.svg" alt="" class="title-img" />
                巡查地点
            </div>
            <van-field label="巡查地点" required>
                <template #input>
                    <van-field v-model="form.riskLocation" name="abnormalDesc" label="" autosize placeholder="请输入"
                        style="padding: 0" />
                </template>
            </van-field>

            <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" :after-read="afterRead"></van-uploader>
                    </template>
                </van-field>
                <van-field label="异常说明" required>
                    <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 required readonly v-model="form.assessmentLevel" name="assessmentLevel" label="风险等级"
                    placeholder="请选择" @click="seletGrade" />
                <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: {
                abnormalPics: []
            },
            text: "异常上报"
        };
    },
    // 退出前，将测评模型数据清空
    beforeRouteLeave(to, _, next) {
        if (to.name != 'assessModelStep1') {
            this.$store.commit('SET_ASSESS_STEP2', "")
            this.$store.commit('SET_ASSESS_STEP1', "")
            this.$store.commit('SET_ASSESS_RESULT', {
                level: "",
                score: "",
            })
            this.$store.commit('SET_DANGER_DATA', {})
        }
        next()
    },
    mounted() {
        const formData = this.$store.state.danger;  //这里的数据是从”implement.vue“页面传递过来的
        console.log(formData)
        if (!formData.riskContent) {  //不存在巡查地点返回
            this.$router.go(-1)
        }
        if (formData) {
            this.form = formData
        }

        const { level, score } = this.$store.state.assessModel.result
        if (level) {
            this.form.assessmentLevel = level;
            this.form.score = score
        }
    },
    methods: {
        //风险等级
        seletGrade(name) {
            this.saveFormData()
            this.$router.push('/assess/model/step1')
        },
        saveFormData() {
            this.$store.commit('SET_DANGER_DATA', this.form)
        },
        jsonToFormData(config) {
            const formData = new FormData();
            //循环传入的值转换formData
            Object.keys(config).forEach((key) => {
                if (key == 'abnormalPics') {
                    config[key].forEach(val => {
                        formData.append('file[]', val.file)
                    })
                } else {
                    formData.append(key, config[key]);
                }
            });
            return formData;
        },
        onSubmit() {
            var formDataJson = this.jsonToFormData(this.form);
            postFun('/risk/currentRiskAbnormal/save', formDataJson).then(res => {
                this.cancel()
            })
        },
        cancel() {
            this.$router.go(-1)
        }
    },
};
</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>
