<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device === 'mobile' && sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <sidebar class="sidebar-container" />
    <div :class="{ hasTagsView: needTagsView }" class="main-container">
      <div :class="{ 'fixed-header': fixedHeader }">
        <navbar />
        <tags-view v-if="needTagsView" />
      </div>
      <app-main />
    </div>
    <el-dialog title="文件下发通知" width="700px" :visible.sync="dialogTableVisible">
      <el-table :data="ListDownload" size="mini">
        <el-table-column property="title" label="标题"></el-table-column>
        <el-table-column property="createTime" label="下发时间" width="120"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">确认接受并且下载</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { Navbar, Sidebar, AppMain } from './components'
import TagsView from './components/TagsView/index'
import ResizeMixin from './mixin/ResizeHandler'
import { comListDownloadGet } from '@/api/common'
import { exportFile } from '@/api/universal/universal_list'
import { downloadFiles } from '@/utils/util'
export default {
  name: 'Layout',
  components: {
    Navbar,
    Sidebar,
    AppMain,
    TagsView
  },
  mixins: [ResizeMixin],
  computed: {
    sidebar() {
      return this.$store.state.app.sidebar
    },
    device() {
      return this.$store.state.app.device
    },
    fixedHeader() {
      return this.$store.state.settings.fixedHeader
    },
    needTagsView() {
      return this.$store.state.settings.tagsView
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      ListDownload: []
    }
  },
  mounted() {
    if (window.localStorage.getItem('dakaitishichuangkou')) {
      window.localStorage.removeItem('dakaitishichuangkou')
      // 打开提示窗口
      comListDownloadGet({ pageNo: 1, pageSize: -1 }).then(res => {
        this.ListDownload = res.data.records
        if (this.ListDownload.length) {
          this.dialogTableVisible = true
        }
      })
    }
  },
  methods: {
    handleClick(item) {
      if (item.isPwd) {
        this.$prompt('请输入下载密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValidator: value => {
            if (!value) {
              return '密码不能为空！'
            }
          }
        })
          .then(({ value }) => {
            downloadFiles('com/list/download', {
              id: item.id,
              downloadPwd: value
            })
          })
          .catch(() => {})
      } else {
        downloadFiles('com/list/download', {
          id: item.id
        })
      }
    },
    handleClickOutside() {
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/mixin.scss';
@import '~@/styles/variables.scss';

.app-wrapper {
  @include clearfix;
  position: relative;
  height: 100%;
  width: 100%;
  &.mobile.openSidebar {
    position: fixed;
    top: 0;
  }
}
.drawer-bg {
  background: #000;
  opacity: 0.3;
  width: 100%;
  top: 0;
  height: 100%;
  position: absolute;
  z-index: 999;
}

.fixed-header {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 999;
  width: calc(100% - #{$sideBarWidth});
  transition: width 0.28s;
}

.hideSidebar .fixed-header {
  width: calc(100% - 54px);
}

.mobile .fixed-header {
  width: 100%;
}
</style>
