<template>
    <div>
        <lj-win-index-common :title="title" 
            api-name="CommonDynamicSelect" 
            :data-reset-callback="handleDataReset"
            :data-loaded-callback="handleDataLoaded" 
            :queryHardParms="queryHardParms"
            ref="baseWin" 
            :addable="false"
        >
            <div slot="body_list">
                <mu-list class="lj-group">
                    <template v-for="item in allData">
                        <push-msg-item :item="item" @click.native="handleCellClick(item)"></push-msg-item>
                    </template>
                </mu-list>
            </div>
        </lj-win-index-common>
    </div>
</template>

<script>
    import LjWinIndexCommon from "components/lj-win-index-common";
    import PushMsgItem from './push-msg-item';

    export default {
        components: {
            LjWinIndexCommon,
            PushMsgItem
        },
        data() {
            return {
                winMode: "",
                allData: [],
                queryHardParms: {
                    dsname: "app_phone_push_msg_list",
                    queryparams: {
                        empid: $lj.getStorage('user').empid
                    }
                },
                queryFields: []
            }
        },
        created() {
            
        },
        methods: {
            handleDataReset(response) {
                this.allData.length = 0;
                this.addList(response.datatable);
            },
            handleDataLoaded(response) {
                let moreItems = response.datatable;
                if (moreItems.length > 0) {
                    this.addList(moreItems);
                    return true;
                }
                return false;
            },
            addList(list) {
                this.allData = this.allData.concat(list);
            },
            handleCellClick(mainObject) {
                switch (mainObject.reltype) {
                    case "待采购审":
                        $lj.openWin("cpcheck-buyer");
                        break;
                    case "待品管审":
                        $lj.openWin("cpcheck-special");
                        break;
                    case "待采购收货":
                        $lj.openWin("inware-buy-sptcheck");
                        break;
                    case "设备报修":
                        $lj.openWin("equipment-repair-info", {mainObject: {repairtid: mainObject.relid}});
                        break;
                    case "代办任务":
                        $lj.openWin("equipment-repair-info");
                    default:
                        break;
                }
                let list = []
                list.push(mainObject.pushid);

                this.handleSetReadMessage(list);
            },
            handleClearAllMessage() {
                let list = [];
                for (let item of allData) {
                    list.push(item.pushid);
                }

                this.handleSetReadMessage(list);
            },
            handleSetReadMessage(list) {
                // 标记已读
                let requestBody = {
                    token: $lj.getStorage("token"),
                    CList: [{
                        uoName: "PushHelper",
                        functionName: "UpdatePushMessageRead",
                        SParms: {
                            pushids: list
                        },
                        ifcommit: true
                    }]
                };
                $lj.postLJRequest('CommonBill', requestBody, (ret) => {
                    if (ret) {
                        this.$refs.baseWin.refresh();
                    }
                })
            }
        },
        computed: {
            title() {
                return "推送消息"
            }
        }
    }
</script>