<template>
    <div>
        <div class="tag right" @click="showDetailBtn()">
            <Tooltip
            width="50"
            trigger="hover"
            >
                <Badge :count="taskMessageCount">
                <i
                    class="iconfont iconbj_message badge"
                />
                </Badge>
                <div slot="content">
                    {{$t('tips.message')}}
                </div>
            </Tooltip>
        </div>
      <!-- <Button @click="showDetailBtn()" style='margin-top: -6px;' size='small' type="fcdefault">详情111</Button> -->
      <Drawer title="" :closable="false" v-model="value1" v-if="showDetail"> 
        <!-- v-if="showDetail" -->
        <messagePanel :panel="messagePanel"  @closeOrderDetail="closeOrderDetail" @nextPage="nextPage" @jumpTask="jumpTask"></messagePanel>   
        <!-- params.ID -->
      </Drawer>
    </div>
  </template>
  <script>
    import { DispatchEvent, R3_IMPORT } from '../../assets/js/__utils__/dispatchEvent';
    const R3 = window.R3;
    const { network, urlSearchParams, store } = R3;
    import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
    import messagePanel from './messagePanel.vue'; 
    export default {
        data () {
            return {
                showDetail: false,
                value1: false,
                showAudioPower: false,
                type: 'list',
                // taskMessageCount: 3,

                messagePanel: {
                    start: 0,
                    list: [],
                    show: false,
                }
            }
        },
        components: {
            messagePanel,
        },
        created() {
            console.log(window.R3.store.state.global.taskMessageCount)
            // this.taskMessageCount = window.VueStore.state.global.taskMessageCount
        },
        computed: {
            ...mapState('global', {
                taskMessageCount: (state) => state.taskMessageCount,
            }),
        },
        watch: {
            taskMessageCount(newValue, oldValue) {
                // 当message变化时，会执行这个函数
                console.log(`message changed from ${oldValue} to ${newValue}`);
                const asyncTaskScheme = () => (window.ProjectConfig && window.ProjectConfig.asyncTaskScheme ? window.ProjectConfig.asyncTaskScheme : '');  // 异步导出设计方案
                if(asyncTaskScheme() === 'skq') {
                    const params = {
                        detail: {
                            type: 'notice'
                        }
                    }
                    DispatchEvent(R3_IMPORT, params)
                    this.$R3loading.hide(this.loadingName);
                    this.closeDialog()
                    return
                }
            }
        },
        methods: {
            closeDialog() { // 关闭弹窗
                this.$emit('closeActionDialog', true);
            },
            closeOrderDetail() {
                this.value1 = false;
                // this.showDetail = false;
            },
            showDetailBtn() {
                this.value1 = true;
                this.showDetail = true;
                this.getMessages(0)
                this.messagePanel.show = true;
            },
            jumpTask() {
                // 跳转我的任务列表界面
                this.messagePanel.show = false
                this.value1 = false;
                // this.showDetail = false;
                // 跳转
                window.parent.R3.store.commit('global/tabOpen', {
                    type: 'S',
                    url: '/SYSTEM/TABLE/U_NOTE/963'
                })
            },
            nextPage() {
                if (this.messagePanel.start < this.messagePanel.total) {
                    this.getMessages()
                }
                // console.log(111,this.messagePanel)
            },
          
            getMessages(start) {
                // 请求我的任务数据
                const self = this
                if (start !== undefined) {
                    self.messagePanel.start = start
                    self.messagePanel.list = []
                }
                // self.messagePanel.start = start
                // self.messagePanel.list = []
                const searchdata = {
                    table: 'U_NOTE',
                    column_include_uicontroller: true,
                    fixedcolumns: {
                    OPERATOR_ID: [JSON.parse(localStorage.getItem('userInfo')).id],
                    READ_STATE: ['=0'],
                    TASK_STATE: ['=2', '=3']
                    },
                    multiple: [],
                    startindex: self.messagePanel.start,
                    range: 20,
                    orderby: [
                        {
                            column: 'U_NOTE.ID',
                            asc: false,
                        },
                    ],
                }
                network.post('/p/cs/QueryList', urlSearchParams({ searchdata })).then((res) => {
                    const result = res.data
                    if (!result.datas) {
                        result.datas = result.data
                    }
                    if (result.code === 0) {
                        result.datas.row.forEach(item => {
                            if(this.getBeforeDate(30)<item.MODIFIEDDATE.val&&item.MODIFIEDDATE.val<this.getBeforeDate(7)){
                                item.MODIFIEDDATE.val = '一周前'
                            }else if(this.getBeforeDate(30)>item.MODIFIEDDATE.val) {
                                item.MODIFIEDDATE.val = '一个月前'
                            }
                            item.disabled = false;
                        })
                        self.messagePanel.list = self.messagePanel.list.concat(
                            result.datas.row
                        )
                        self.messagePanel.start = result.datas.start + result.datas.rowCount
                        self.messagePanel.total = result.datas.totalRowCount
                    }
                    console.log(self.messagePanel)
                })
            },

            //日期时间计算
            getBeforeDate(n) {
                var n = n;
                var d = new Date();
                var year = d.getFullYear();
                var mon = d.getMonth() + 1;
                var day = d.getDate();
                if(day <= n) {
                    if(mon > 1) {
                        mon = mon - 1;
                    } else {
                        year = year - 1;
                        mon = 12;
                    }
                }
                d.setDate(d.getDate() - n);
                year = d.getFullYear();
                mon = d.getMonth() + 1;
                day = d.getDate();
                let s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
                return s;
            },
        }
    }
  </script>
  