<template>
    <div>
        <van-sticky>
            <LHeader :text="text"></LHeader>
        </van-sticky>
        <van-sticky offset-top="1.5rem">
            <van-search
                v-model="searchValue"
                show-action
                placeholder="请输入搜索内容"
                @search="onSearch"
            >
                <template #action>
                    <div @click="onSearch">搜索</div>
                </template>
            </van-search>
        </van-sticky>
        <!-- tab标签 -->
        <van-tabs
            v-model="active"
            @change="
                postList();
                showIndex = null;
            "
            color="#2980f7"
            animated
            :sticky="true"
            offset-top="3rem"
        >
            <van-tab v-for="(item, key) in tabs" :key="key" :title="item.title">
                <!-- 内容列表 -->
                <div class="con-list" @touchmove="showIndex = null">
                    <van-cell-group
                        inset
                        v-for="(item, index) in messageList"
                        :key="index"
                        @click="touchstart(index, 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.hdDiscoveryTime }}</van-col>
                        </van-row> -->
                        <van-row gutter="">
                            <van-col span="7">适用范围：</van-col>
                            <van-col span="17">{{ item.hdRange }}</van-col>
                        </van-row>
                        <van-row gutter="">
                            <van-col span="7">隐患类型：</van-col>
                            <van-col span="17">{{ item.hdType }}</van-col>
                        </van-row>
                        <van-row gutter="">
                            <van-col span="7">超期标识：</van-col>
                            <van-col span="17">{{
                                item.dueDate | formatTime
                            }}</van-col>
                        </van-row>
                        <van-row gutter="" v-if="item.taskName">
                          <van-col span="7">工单状态：</van-col>
                          <van-col span="17">{{ item.taskName }}</van-col>
                        </van-row>


                        <!-- 长按显示遮罩层 -->
                        <van-overlay :show="showIndex == index">
                            <div class="wrapper" @click.stop="showIndex = null">
                                <van-button
                                    round
                                    type="primary"
                                    @click="goDetail(item)"
                                    >详情</van-button
                                >
                                <van-button
                                    round
                                    type="info"
                                    @click="goConfirm(item)"
                                    v-if="active !== 2"
                                    >隐患整改</van-button
                                >
                                <van-button
                                    round
                                    type="info"
                                    @click="goDelay(item)"
                                    v-if="active !== 2"
                                    >申请延期</van-button
                                >
                                <!-- <van-button round type="warning " @click="goClose(item)"
                                  >关闭</van-button
                                > -->
                            </div>
                        </van-overlay>
                    </van-cell-group>
                </div>
                <!-- 暂无数据 -->
                <div
                    style="
                        width: 100%;
                        text-align: center;
                        font-size: 0.48rem;
                        position: fixed;
                        top: 30%;
                    "
                    v-if="messageList.length == 0"
                >
                    暂无数据
                </div>
            </van-tab>
        </van-tabs>
    </div>
</template>

<script>
import LHeader from "@/components/header.vue";
import { getFun, postFun } from "@/service/table.js";
// import { dangerRect, list1, list2 } from "@/service/danger";
import { Dialog } from "vant";
export default {
    components: {
        LHeader,
    },
    data() {
        return {
            text: "隐患整改",
            searchValue: "",
            messageList: [],
            showIndex: null, // 是否显示遮罩层
            active: 0,
            tabs: [
                {
                    title: "隐患整改",
                    api: "/rectification/list1",
                },
                {
                    title: "已退回",
                    api: "/rectification/list2",
                },
                {
                    title: "已整改",
                    api: "/rectification/finishList",
                },
            ],
        };
    },
    created() {
        this.postList();
    },
    methods: {
        // 关闭
        goClose(val) {
            // 点击了关闭
            Dialog.confirm({
                title: "提示",
                message: "确定要关闭此条隐患吗？",
            })
                .then(() => {
                    this.$toast.loading({
                        message: "删除中...",
                        forbidClick: true,
                        loadingType: "spinner",
                        duration: 0,
                    });
                    postFun(`/rectification/delete/${val.taskId}`)
                        .then((res) => {
                            // console.log(res,'怎么样')
                            this.$toast.clear();
                            this.$toast.success({
                                message: "关闭成功",
                                duration: 2000,
                            });
                            this.onClickTab(0, this.searchValue);
                        })
                        .catch(() => {
                            this.$toast.clear();
                            this.$toast.fail("关闭失败,请稍后再试");
                        });
                    this.showIndex = null;
                })
                .catch(() => {
                    this.$toast.clear();
                    this.showIndex = null;
                });
            // console.log(val)
        },
        onSearch(val) {
            this.postList(this.searchValue);
        },
        postList(select = "") {
            this.$toast.loading({
                message: "加载中...",
                forbidClick: true,
                loadingType: "spinner",
                duration: 0,
            });
            let formdata = new FormData();
            formdata.append("select", select);
            postFun(this.tabs[this.active]["api"], formdata)
                .then((res) => {
                    console.log(res);
                    this.$toast.clear();
                    this.messageList = res.rows || res.data;
                })
                .catch((err) => {
                    console.log(err);
                    this.$toast.clear();
                    this.$toast.fail("加载失败,请稍后再试");
                });
        },
        // onClickTab(val, select = "") {
        //     this.showIndex = null;
        //     this.$toast.loading({
        //         message: "加载中...",
        //         forbidClick: true,
        //         loadingType: "spinner",
        //         duration: 0,
        //     });

        //     // 这里根据tab切换调用接口
        //     let formdata = new FormData();
        //     formdata.append("select", select);
        //     [list1, list2]
        //         [val](formdata)
        //         .then((res) => {
        //             console.log(res.rows);
        //             this.$toast.clear();
        //             this.messageList = res.rows;
        //             // 判断有无数据返回
        //             // if (this.messageList.length == 0) {
        //             //   this.isHaveNews = true;
        //             // }
        //         })
        //         .catch(() => {
        //             this.$toast.clear();
        //             this.$toast.fail("加载失败,请稍后再试");
        //         });
        // },

        // 此接口暂时废弃
        // postList() {
        //   this.$toast.loading({
        //     message: "加载中...",
        //     forbidClick: true,
        //     loadingType: "spinner",
        //     duration: 0,
        //   });
        //   dangerRect("/rectification/list")
        //     .then((res) => {
        //       this.$toast.clear();
        //       this.messageList = res.rows;
        //       // 判断有无数据返回
        //       if (this.messageList.length == 0) {
        //         this.isHaveNews = true;
        //       }
        //     })
        //     .catch(() => {
        //       this.$toast.clear();
        //       this.$toast.fail("加载失败,请稍后再试");
        //     });
        // },

        touchstart(index, item) {
            if (this.showIndex != null) {
                this.showIndex = null;
                return;
            }
            this.showIndex = index;
        },

        // 详情
        goDetail(data) {
            this.$router.push({
                name: "normal-detail",
                params: {
                    id: data.taskId||data.id,
                },
            });
            this.showIndex = null;
        },
        // 确认
        goConfirm(data) {
            this.$router.push({
                name: "change-info",
                params: {
                    data: data,
                },
            });
            this.showIndex = null;
        },
        // 延期
        goDelay(data) {
            this.$router.push({
                name: "delay-info",
                params: {
                    taskId: data.taskId,
                    taskName: data.taskName,
                },
            });
            this.showIndex = null;
        },
    },
    filters: {
        formatTime: function (val) {
            if (new Date(val).getTime() <= new Date().getTime()) {
                return "超期";
            } else if (
                new Date(val).getTime() >= new Date().getTime() &&
                new Date(val).getTime() <= new Date().getTime() + 259200000
            ) {
                return "临期";
            } else {
                return "正常";
            }
        },
    },
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
#app {
    font-family: "";
    color: #2c3e50;
}
.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%;
            }
        }
    }
}
/deep/.van-tab__pane-wrapper {
    min-height: 8rem;
}
.van-tabs__track {
    min-height: 3rem;
}
</style>
