<template>
    <div>
        <LHeader :text="text"></LHeader>
        <van-cell-group
            inset
            v-for="(item, index) in messageList"
            :key="index"
            @click="goDetail(item.taskId)"
        >
            <!-- {{item}} -->
            <van-row gutter="">
                <van-col span="7">隐患单编号：</van-col>
                <van-col span="17">{{ item.businessId||item.id}}</van-col>
            </van-row>

            <van-row gutter="">
                <van-col span="7">隐患项目名称：</van-col>
                <van-col span="17">{{ item.proId }}</van-col>
            </van-row>
            <van-row gutter="">
                <van-col span="7">隐患级别：</van-col>
                <van-col span="17">{{ item.hdLev | dangerText }}</van-col>
            </van-row>
            <!-- <van-row gutter="">
          <van-col span="7">发现时间：</van-col>
          <van-col span="17">{{ item.startDate }}</van-col>
        </van-row> -->
            <!-- 详情 -->
            <div class="detail">详情</div>
        </van-cell-group>
        <!-- {{messageList[0]}} -->
        <van-form
            @submit="onSubmit"
            :show-error="false"
            :scroll-to-error="true"
            validate-trigger="onSubmit"
        >
            <van-field name="isResult" label="审批结果">
                <template #input>
                    <van-radio-group
                        v-model="radio"
                        direction="horizontal"
                        @change="selectResult"
                    >
                        <van-radio name="1" style="margin-bottom: 0.3rem">{{
                            name == "项目经理二次认定" ? "退回到发起人" : "同意"
                        }}</van-radio>
                        <van-radio name="0" style="margin-bottom: 0.3rem">{{
                            name == "项目经理二次认定"
                                ? "重新发起重大隐患审批"
                                : "退回"
                        }}</van-radio>
                        <!-- 退回到发起人  -->
                        <!-- <van-radio name="1" style="margin-bottom:0.3rem">{{name=='项目经理二次认定'?'退回到发起人':'同意'}}</van-radio>
                        <van-radio name="0" style="margin-bottom:0.3rem">{{name=='项目经理二次认定'?'重新发起重大隐患审批':'退回'}}</van-radio> -->
                    </van-radio-group>
                </template>
            </van-field>

            <div class="disagree">
                <van-field
                    readonly
                    v-if="
                        radio == 1
                            ? name == '项目经理二次认定'
                                ? true
                                : false
                            : name == '项目经理二次认定'
                            ? false
                            : true
                    "
                    clickable
                    name="thinkHdLev"
                    :value="thinkLevel"
                    label="认定隐患级别"
                    placeholder="请选择认定隐患级别"
                    @click="showThinkLevel = true"
                />
                <van-popup v-model="showThinkLevel" position="bottom">
                    <van-picker
                        show-toolbar
                        value-key="projectName"
                        :default-index="0"
                        :columns="columnsThinkLevel"
                        @confirm="onConThinkLevel"
                        @cancel="showThinkLevel = false"
                    />
                </van-popup>

                <van-field
                    v-model="disagreeOpinion"
                    label="审批意见"
                    name="details"
                    rows="3"
                    type="textarea"
                    placeholder="请输入"
                    :required="true"
                    :rules="[{ required: true, message: '审批意见不能为空' }]"
                />
            </div>

            <div style="margin: 16px">
                <van-button round block type="info" native-type="submit"
                    >保存</van-button
                >
            </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";
import { majorAdd } from "@/service/danger";
export default {
    name: "major-survey",
    components: {
        LHeader,
    },
    data() {
        return {
            name: "",
            taskId: "",
            messageList: [],
            radio: "1",
            text: "重大隐患审批",
            agreeOpinion: "",
            disagreeOpinion: "",
            thinkLevel: "", //因为隐患级别
            thinkHdLev: "",
            showThinkLevel: false,
            columnsThinkLevel: [
                "建议为严重隐患",
                "建议为较大隐患",
                "建议为危险隐患",
            ],
        };
    },
    mounted() {
        this.messageList = [];
        let paramsData =
            this.$route.params.data ||
            JSON.parse(localStorage.getItem("majorDanderData"));
        this.messageList.push(paramsData);
        this.taskId = paramsData.taskId;
        this.radio = "1";
        this.agreeOpinion = "";
        this.disagreeOpinion = "";
        this.thinkLevel = "";
        this.getName();
    },
    methods: {
        selectResult(val) {
            this.radio = val;
            this.disagreeOpinion = "";
        },
        getName() {
            getFun("/majorapprove/add", {
                taskId: this.taskId,
            })
                .then((data) => {
                    this.name = data.msg;
                })
                .catch((err) => {
                    this.$toast.fail("提交失败,请稍后再试");
                });
        },
        onSubmit(values) {
            this.$toast.loading({
                message: "提交中...",
                forbidClick: true,
                loadingType: "spinner",
                duration: 0,
            });
            let formdata = new FormData();
            formdata.append("isResult", values.isResult);
            formdata.append("details", values.details);
            formdata.append(
                "thinkHdLev",
                values.isResult == 1 ? "" : values.thinkHdLev
            );
            formdata.append("taskId", this.taskId);
            majorAdd(`/majorapprove/add`, formdata)
                .then((res) => {
                    this.$toast.clear();
                    this.$toast.success({
                        message: "提交成功",
                        duration: 2000,
                    });
                    history.go(-1);
                })
                .catch(() => {
                    this.$toast.clear();
                    this.$toast.fail("提交失败,请稍后再试");
                });
        },

        // 所属项目
        onConThinkLevel(value) {
            this.thinkLevel = value;
            this.showProjectName = false;
            this.showThinkLevel = false;
        },
        // 详情
        goDetail(taskId) {
            this.$router.push({
                name: "normal-detail",
                params: {
                    id: taskId,
                },
            });
        },

        cancel() {
            this.$router.go(-1);
        },
    },
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
.van-cell-group--inset {
    margin: 0;
    margin-bottom: 10px;
    padding: 10px;
    font-size: 13px;
    position: relative;
    .van-row {
        margin-bottom: 0.133333rem;
        line-height: 0.64rem;
    }
    .van-overlay {
        position: absolute;
        .wrapper {
            display: flex;
            align-items: center;
            justify-content: space-evenly;
            height: 100%;
        }
    }
}
.detail {
    position: absolute;
    bottom: 0.32rem;
    right: 15px;
    color: #2a80f7;
    font-weight: none;
    z-index: 99;
}
</style>
