<template>
  <popu>
    <div>
      <div class="menu-list">
        <div>
          <div class="main-title">
            <div class="title">常用菜单</div>
            <div class="el-icon-setting" @click="dialogVisible = !dialogVisible" />
          </div>
          <div class="child">
            <el-row :gutter="20">
              <el-col
                v-for="(it, idx) in normsMenuList"
                :key="idx"
                class="item"
                :span="4"
                style="margin-top: 16px; padding: 3px 20px"
              >
                <el-link class="link" :underline="false" @click="clickMenu(it.fullUrl)">{{ it.moduleName }}</el-link>
              </el-col>
            </el-row>
          </div>
        </div>
        <div v-for="(item, index) in menuList" :key="index" class="group">
          <div v-if="item.moduleName != '首页'">
            <div class="main-title">
              <div class="title">{{ item.moduleName }}</div>
            </div>
            <div v-if="item.children && item.children.length" class="child">
              <el-row :gutter="20">
                <el-col v-for="(it, idx) in item.children" :key="idx" class="item" :span="4">
                  <el-link class="link" :underline="false" @click="clickMenu(it.fullUrl)">{{ it.moduleName }}</el-link>
                </el-col>
              </el-row>
            </div>
            <div v-else class="child">
              <div class="item">
                <el-link class="link" :underline="false" @click="clickMenu(item.fullUrl)">{{
                  item.moduleName
                }}</el-link>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 弹窗 -->
      <el-dialog title="菜单配置" append-to-body :visible.sync="dialogVisible" width="70%">
        <el-checkbox-group v-model="checkList" :max="10">
          <div v-for="(item, index) in menuList" :key="index" class="group">
            <div v-if="item.moduleName != '首页'">
              <div v-if="item.children && item.children.length" class="dia-child">
                <el-checkbox v-for="(it, idx) in item.children" :key="idx" :label="it.moduleId" class="dia-item">{{
                  it.moduleName
                }}</el-checkbox>
              </div>
              <div v-else class="child">
                <el-checkbox class="dia-item" :label="item.moduleId">{{ item.moduleName }}</el-checkbox>
              </div>
            </div>
          </div>
        </el-checkbox-group>
        <div class="dia-tip">（ {{ checkList.length }} / 10 ）注：最多选10个导航菜单</div>
        <span slot="footer" class="dialog-footer">
          <el-button
            @click="
              dialogVisible = false
              checkList = []
            "
            >取 消</el-button
          >
          <el-button type="primary" @click="handleSaveMenu">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </popu>
</template>

<script>
import {
  getNoticeList,
  getComListList,
  getComListDownload,
  getNoticeDetail,
  confirmCheck,
  getRebateOrderList,
  getRouter,
  getMenuList,
  setMenuList,
  getListInvoiceOrder
} from '@/api/dashboard'
import { getArrivalNotice } from '@/api/stock'

import { getListOrderTrack } from '@/api/supply/pickup'
import { getFileUrl } from '@/api/common'
import { mapGetters } from 'vuex'
import popu from '@/components/template/popu.vue'
export default {
  name: 'Dashboard',
  components: {
    popu
  },
  created() {
    getRouter({
      flag: 'index',
      adminUserId: this.userid
    }).then(res => {
      this.menuList = res.data
      console.log(res.data)
    })
    this.getMenuList()
  },
  computed: {
    ...mapGetters(['showMessages', 'userid', 'isCustomer'])
  },
  data() {
    return {
      imageURL: this.$imageUrl,
      type: '1',
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [],
      listLoading: false, // 列表加载loading
      fileList: [],
      inDialog: false,
      detailData: [],
      rebateList: [],
      // isCustomer: JSON.parse(localStorage.getItem('supply_user')).isCustomer,
      menuList: [],
      dialogVisible: false,
      checkList: [],
      normsMenuList: [],
      invoiceOrderList: [],
      bol: true,
      visible: false,
      orderId: '',
      logisticsDetail: [],
      arrivalNoticeList: [],
      specification: ''
    }
  },

  methods: {
    // 获取常用菜单列表
    getMenuList() {
      getMenuList().then(res => {
        this.normsMenuList = res.data
        if (this.normsMenuList.length) {
          this.normsMenuList.forEach(k => {
            this.checkList.push(k.moduleId)
          })
        }
      })
    },
    // 保存常用菜单
    handleSaveMenu() {
      setMenuList({ adminModuleIds: this.checkList.join(',') }).then(res => {
        this.$successMsg('设置成功')
        this.checkList = []
        this.getMenuList()
        this.dialogVisible = false
      })
    },
    // 打开链接
    openLink(url) {
      getFileUrl({ key: url }).then(res => {
        window.open(res.data)
      })
    },
    // 复核
    secondFn(id) {
      this.$router.push({
        path: '/finance/rebate_list',
        query: {
          id
        }
      })

      this.$store.commit('user/showMessage', 'no')
    },
    // 确认查收
    confirmCheck(id) {
      confirmCheck({ noticeIds: id }).then(res => {
        this.$successMsg('查收成功')
        this.inDialog = false
        this.getDataList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          readFlag: false
        })
      })
    },
    // 检查文件类型
    checkFileType(url) {
      if (!url) return ''
      const fileSuffix = url.substring(url.lastIndexOf('.') + 1)

      if (['jpg', 'jpeg', 'png'].includes(fileSuffix)) {
        return 'image'
      } else if (['doc', 'docx', 'dot', 'wps', 'wpt'].includes(fileSuffix)) {
        return 'word'
      } else if (['xls', 'xlsx', 'xlt', 'et', 'ett'].includes(fileSuffix)) {
        return 'excel'
      } else if (['ppt', 'pptx', 'dps', 'dpt', 'pot', 'pps'].includes(fileSuffix)) {
        return 'ppt'
      } else if (['pdf'].includes(fileSuffix)) {
        return 'pdf'
      } else {
        return ''
      }
    },
    // 查看
    async lookInDialog(id) {
      const res = await getNoticeDetail({ id })
      // console.log(res, 7898);
      this.detailData = res.data
      this.inDialog = true
    },
    // 下载
    async downloadFn(id) {
      const res = await getComListDownload({ id })
      if (res.data) {
        window.open(this.imageURL + res.data)
      }
    },
    // 获取文件列表数据
    async getFileList(data) {
      const res = await getComListList(data)
      this.fileList = res.data.records
      this.listTotal = res.data.total
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      if (this.type == 1) {
        this.getDataList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          readFlag: false
        })
      }
      if (this.type == 2) {
        this.getFileList({
          needDown: true,
          pageSize: this.pageSize,
          pageNo: this.currentPage
        })
      }
      if (this.type == 3) {
        this.getRebateList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          isSecondExamine: false
        })
      }
      if (this.type == 4) {
        this.getListInvoiceOrder({
          pageSize: this.pageSize,
          pageNum: this.currentPage
        })
      }
      if (this.type == 5) {
        this.getArrivalNotice({
          pageSize: this.pageSize,
          pageNum: this.currentPage
        })
      }
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      if (this.type == 1) {
        this.getDataList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          readFlag: false
        })
      }
      if (this.type == 2) {
        this.getFileList({
          needDown: true,
          pageSize: this.pageSize,
          pageNo: this.currentPage
        })
      }
      if (this.type == 3) {
        this.getRebateList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          isSecondExamine: false
        })
      }
      if (this.type == 4) {
        this.getListInvoiceOrder({
          pageSize: this.pageSize,
          pageNum: this.currentPage
        })
      }
      if (this.type == 5) {
        this.getArrivalNotice({
          pageSize: this.pageSize,
          pageNum: this.currentPage
        })
      }
    },
    // 获取返利确认单数据
    async getRebateList(data) {
      const res = await getRebateOrderList(data)
      this.rebateList = res.data.records
      this.listTotal = res.data.total
    },
    // 获取列表数据
    async getDataList(data) {
      const res = await getNoticeList(data)
      console.log(res)
      this.dataList = res.data.records
      this.listTotal = res.data.total
    },
    // 获取物流列表
    async getListInvoiceOrder(data) {
      const res = await getListInvoiceOrder(data)
      this.invoiceOrderList = res.data.records
      this.listTotal = res.data.total
    },
    async getArrivalNotice(data) {
      data = {
        ...data,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        specification: this.specification
      }
      const res = await getArrivalNotice(data)
      this.arrivalNoticeList = res.data.records
      this.listTotal = res.data.total
    },
    // 选择
    checkFn(v) {
      this.$refs.table.doLayout()
      this.type = v
      if (v == 1) {
        this.getDataList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          readFlag: false
        })
      }
      if (v == 2) {
        this.getFileList({
          needDown: true,
          pageSize: this.pageSize,
          pageNo: this.currentPage
        })
      }
      if (v == 3) {
        this.getRebateList({
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          isSecondExamine: false
        })
      }
      if (v == 4) {
        this.getListInvoiceOrder({
          pageSize: this.pageSize,
          pageNum: this.currentPage
        })
      }
      if (this.type == 5) {
        this.getArrivalNotice({
          pageSize: this.pageSize,
          pageNum: this.currentPage
        })
      }
    },
    // 关闭
    closeFn() {
      this.$store.commit('user/showMessage', 'no')
    },

    // 点击菜单
    clickMenu(path) {
      const curlArr = ['/notice', '/issue']
      // if (curlArr.includes(path)) {
      //   path += '/index'
      // }
      this.$router.push({
        path
      })
    },
    handLogistics(row) {
      getListOrderTrack({ orderId: row.id }).then(res => {
        this.logisticsDetail = res.data
      })
      this.orderId = row.id
      this.visible = true
    },
    onClose() {
      this.logisticsDetail = []
      this.orderId = ''
      this.visible = false
    },
    handleJump(id) {
      this.$router.push({ path: `/supply/pickup/sum_list?id=${id}` })
      this.$store.commit('user/showMessage', 'no')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input__inner {
  /* width: 80px; */
  font-size: 16px;
  color: #333333;
  /* border: 0 !important; */
}
.dashboard {
  &-container {
    background: #f5f5f5;
    padding: 30px;
    min-height: calc(100vh - 86px);
    .top-container {
      .today-content {
        font-size: 14px;
      }
    }
  }
}
.cql {
  ::v-deep.el-table__header-wrapper {
    background-color: #f5f7fa;
  }
}
.dia-menu-title {
  padding: 10px 0;
  color: #000;
  font-weight: bold;
}
.dia-tip {
  padding: 10px 0;
  color: #000;
  font-weight: bold;
}
.dia-child {
  display: flex;
  flex-wrap: wrap;
}
.dia-item {
  flex: 0 1 220px;
  padding: 3px 0;
}
.menu-list {
  .main-title {
    margin: 10px 0 0;
    .el-icon-setting {
      cursor: pointer;
      font-size: 22px;
    }
  }
  .group {
    margin-bottom: 20px;
    .child {
      display: flex;
      flex-wrap: wrap;
      padding-left: 10px;
      .el-row {
        width: 100%;
      }
      .item {
        margin-top: 16px;
      }
    }
  }
  ::v-deep .el-link--inner {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.logistics-title {
  padding-bottom: 10px;
}
.tip {
  height: 200px;
  text-align: center;
  line-height: 200px;
}
</style>
