<template>
  <div>
    <el-menu class="navbar" mode="horizontal" style="background: rgb(87,177,253)!important;">
      <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>
      <breadcrumb />
      <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <img :src="require('@/assets/img/user.jpg')" class="user-avatar">
          <i class="el-icon-caret-bottom"/>
        </div>
        <el-dropdown-menu slot="dropdown" class="user-dropdown">
          <router-link class="inlineBlock" to="/">
            <el-dropdown-item>
              主页
            </el-dropdown-item>
          </router-link>
          <el-dropdown-item divided @click.native="logout">
            <span style="display:block;">退出</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-menu>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import { searchTable1 } from '@/api/PurchasingCenter/PurchaseContractApproval'
import { getMaterial, getMaterial_NOTES, setChakan } from '@/api/PurchasingCenter/MaterialManagement'
import { initApproval, initApprovalOffline } from '@/api/PurchasingCenter/FundApproval'
import { searchTable1 as searchTableInvoice } from '@/api/PurchasingCenter/InvoiceSettlementApprove'
import { searchRequestFund } from '@/api/PurchasingCenter/PurchaseContractRequestFund'
import { getPickingList, setPickingList } from '@/api/Inventory/MaterialOut'
export default {
  components: {
    Breadcrumb,
    Hamburger
  },
  data() {
    return {
      timer: '',
      noticeContract1: null,
      noticeContract2: null,
      noticeContract3: null,
      noticeContract4: null,
      noticeContract5: null,
      noticeContract6: null,
      noticeFund: null,
      noticeInvoice: null,
      noticeContractKG: null
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id',
      'token',
      'token'
    ])
  },
  mounted() {
    if (this.id.toString() === '3') {
      this.checkApproval()
      this.timer = setInterval(this.checkApproval, 1000 * 60 * 5)
    } else if (this.token.toString() === '11' || this.token.toString() === '12') {
      this.comfirmMaterial()
      this.timer = setInterval(this.comfirmMaterial, 1000 * 60 * 5)
    } else if (this.token.toString() === '2' || this.token.toString() === '3') {
      this.comfirmMaterial_CG()
      this.timer = setInterval(this.comfirmMaterial_CG, 1000 * 60 * 5)
    } else if (this.token.toString() === '15') {
      this.comfirmMaterial_KG()
      this.timer = setInterval(this.comfirmMaterial_KG, 1000 * 60 * 5)
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    comfirmMaterial_KG() {
      getPickingList().then(res => {
        if (res.data.length) {
          this.noticeContractKG = null
          this.noticeContractKG = this.$notify.success({
            title: '提示',
            message: '您有新的领料单需要备料确认，请及时处理，点击查看',
            onClick: () => {
              setPickingList().then(res => {
                this.$router.push({ path: '/Inventory/MaterialOut' })
                setTimeout(this.noticeContractKG.close(), 1000)
              })
            },
            duration: 1000 * 60
          })
        }
      })
    },
    comfirmMaterial_CG() {
      getMaterial_NOTES().then(res => {
        if (res.data.length) {
          this.noticeContract1 = null
          this.noticeContract1 = this.$notify.success({
            title: '提示',
            message: '有新采购物料审批结果，点击查看',
            onClick: () => {
              setChakan().then(res => {
                this.$router.push({ path: '/PurchasingCenter/MaterialMProcure' })
                setTimeout(this.noticeContract1.close(), 1000)
              })
            },
            duration: 1000 * 60
          })
        }
      })
      if (this.token.toString() === '2') {
        searchRequestFund(1, 9999, 0, 0, 0, 0, 0).then(res => {
          if (res.data.length) {
            this.noticeContract2 = null
            this.noticeContract2 = this.$notify.success({
              title: '提示',
              message: '有合同请款申请审批结果，点击查看',
              onClick: () => {
                setChakan().then(res => {
                  this.$router.push({ path: '/PurchasingCenter/PurchaseContractRequestFund' })
                  setTimeout(this.noticeContract2.close(), 1000)
                })
              },
              duration: 1000 * 60
            })
          }
        })
        initApproval(1, 999, 1).then(res => {
          if (res.data.rows.length) {
            this.noticeContract3 = null
            this.noticeContract3 = this.$notify.success({
              title: '提示',
              message: '有合同请款申请待审批，点击查看',
              onClick: () => {
                setChakan().then(res => {
                  this.$router.push({ path: '/PurchasingCenter/FundApproval1' })
                  setTimeout(this.noticeContract3.close(), 1000)
                })
              },
              duration: 1000 * 60
            })
          }
        })
      } else if (this.token.toString() === '3') {
        searchRequestFund(1, 99999, 0, 0, 0, 0, 0).then(res => {
          if (res.data.length) {
            this.noticeContract4 = null
            this.noticeContract4 = this.$notify.success({
              title: '提示',
              message: '有合同请款申请审批结果，点击查看',
              onClick: () => {
                setChakan().then(res => {
                  this.$router.push({ path: '/PurchasingCenter/PurchaseContractRequestFund' })
                  setTimeout(this.noticeContract4.close(), 1000)
                })
              },
              duration: 1000 * 60
            })
          }
        })
      }
    },
    comfirmMaterial() {
      getMaterial().then(res => {
        if (res.data.length) {
          this.noticeContract5 = null
          this.noticeContract5 = this.$notify.success({
            title: '提示',
            message: '您有采购物料申请待审批，请及时处理，点击查看',
            onClick: () => {
              this.$router.push({ path: '/PurchasingCenter/MaterialManagement' })
              setTimeout(this.noticeContract5.close(), 1000)
            },
            duration: 1000 * 60
          })
        }
      })
    },
    checkApproval() {
      searchTable1(1, 999, 0, null, null, 0, 0, 0, 0, 0, 0).then(res => {
        if (res.data.total) {
          this.noticeContract6 = null
          this.noticeContract6 = this.$notify.success({
            title: '提示',
            message: '您有采购合同待审批，请及时处理，点击查看',
            onClick: () => {
              this.$router.push({ path: '/PurchasingCenter/PurchaseContractApproval' })
              setTimeout(this.noticeContract6.close(), 1000)
            },
            duration: 1000 * 60
          })
        }
      })
      const p1 = new Promise((resolve, reject) => {
        initApproval(1, 999, 1).then(res => {
          resolve(res)
        })
      })
      const p2 = new Promise((resolve, reject) => {
        initApprovalOffline(1, 999, 1).then(res => {
          resolve(res)
        })
      })
      Promise.race([p1, p2]).then(res => {
        if (res.data.total) {
          this.noticeFund = this.$notify.success({
            title: '提示',
            message: '您有合同请款申请待审批，请及时处理，点击查看',
            onClick: () => {
              this.$router.push({ path: '/PurchasingCenter/FundApproval2' })
              setTimeout(this.noticeFund.close(), 1000)
            },
            duration: 1000 * 60 * 5
          })
        }
      })
      searchTableInvoice(0, null, null, 1, 999, this.token).then(res => {
        if (res.data.total) {
          this.noticeInvoice = null
          this.noticeInvoice = this.$notify.success({
            title: '提示',
            message: '您有发票结算申请待审批，请及时处理，点击查看',
            onClick: () => {
              this.$router.push({ path: '/PurchasingCenter/InvoiceSettlementApprove' })
              setTimeout(this.noticeInvoice.close(), 1000)
            },
            duration: 1000 * 60 * 5
          })
        }
      })
    },
    toggleSideBar() {
      this.$store.dispatch('ToggleSideBar')
    },
    logout() {
      this.$store.dispatch('FedLogOut').then(() => {
        location.reload() // 为了重新实例化vue-router对象 避免bug
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.navbar {
  height: 50px;
  line-height: 50px;
  border-radius: 0px !important;
  .hamburger-container {
    line-height: 58px;
    height: 50px;
    float: left;
    padding: 0 10px;
  }
  .screenfull {
    position: absolute;
    right: 90px;
    top: 16px;
    color: red;
  }
  .avatar-container {
    height: 50px;
    display: inline-block;
    position: absolute;
    right: 35px;
    .avatar-wrapper {
      cursor: pointer;
      margin-top: 5px;
      position: relative;
      .user-avatar {
        width: 40px;
        height: 40px;
        border-radius: 10px;
      }
      .el-icon-caret-bottom {
        position: absolute;
        right: -20px;
        top: 25px;
        font-size: 12px;
      }
    }
  }
}
</style>
