<template>
  <div class="app-container">
    <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.curUrl)">{{ 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.curUrl)">{{ 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.curUrl)">{{ item.moduleName }}</el-link>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 弹窗 -->
    <el-dialog title="菜单配置" :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>

    <el-dialog title="消息" :visible.sync="showMessages" width="50%" :show-close="false" :close-on-click-modal="false">
      <el-radio-group v-model="type" size="mini" @change="checkFn">
        <el-radio-button label="1">系统通知</el-radio-button>
        <el-radio-button label="2">文件下发</el-radio-button>
        <el-radio-button label="3">返利确认单</el-radio-button>
      </el-radio-group>
      <br />
      <br />
      <!-- 系统通知 -->
      <div v-show="type == '1'" class="table">
        <el-table
          v-loading="listLoading"
          class="cql"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column align="center" type="index" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="标题" prop="title" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="来源" prop="source" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="时间" prop="issueTime" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" class="textColor" @click="lookInDialog(scope.row.id)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 文件下发 -->
      <div v-show="type == '2'" class="table">
        <el-table
          v-loading="listLoading"
          :data="fileList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column align="center" type="index" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="标题" prop="title" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="来源" prop="operatorNickName" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="时间" prop="createTime" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="操作" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" class="textColor" @click="downloadFn(scope.row.id)">下载</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 返利确认单 -->
      <div v-show="type == '3'" class="table">
        <el-table
          v-loading="listLoading"
          :data="rebateList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column align="center" type="index" min-width="160" show-overflow-tooltip />
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="确认人" prop="examineBy" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="确认时间" prop="examineTime" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" class="textColor" @click="secondFn(scope.row.rebateOrderId)">{{
                isCustomer ? '确认' : '复核'
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <br />
      <br />
      <!-- 分页 -->
      <div class="fr">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <br /><br />
      <!-- 内层弹窗 -->
      <el-dialog width="40%" title="消息标题" :visible.sync="inDialog" append-to-body>
        <div class="detail-container">
          <div class="title">{{ detailData.title }}</div>
          <div class="text">
            发布人：{{ detailData.issueNickName }}<el-divider direction="vertical" />发布时间：{{
              detailData.issueTime
            }}
          </div>
          <el-divider />
          <div class="content">{{ detailData.content }}</div>
          <el-divider v-if="detailData.files && detailData.files.length > 0" />
          <div v-if="detailData.files && detailData.files.length > 0" class="file">
            <div>附件：</div>
            <div class="file-list">
              <div v-for="(item, index) in detailData.files" :key="index" class="item">
                <el-image
                  v-if="checkFileType(item.url) == 'image'"
                  class="img"
                  :src="imageURL + item.url"
                  :preview-src-list="[imageURL + item.url]"
                />
                <div v-else class="box2" @click="openLink(item.url)">
                  <img v-if="checkFileType(item.url) == 'word'" src="@/assets/common/word.png" />
                  <img v-if="checkFileType(item.url) == 'excel'" src="@/assets/common/excel.png" />
                  <img v-if="checkFileType(item.url) == 'ppt'" src="@/assets/common/ppt.png" />
                  <img v-if="checkFileType(item.url) == 'pdf'" src="@/assets/common/pdf.png" />
                  <div class="name ellipsis-3">{{ item.name }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" type="primary" @click="confirmCheck(detailData.noticeId)">确认接收</el-button>
          <el-button size="mini" @click="inDialog = false">关 闭</el-button>
        </div>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="closeFn">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getNoticeList,
  getComListList,
  getComListDownload,
  getNoticeDetail,
  confirmCheck,
  getRebateOrderList,
  getRouter,
  getMenuList,
  setMenuList
} from '@/api/dashboard'
import { getFileUrl } from '@/api/common'
import { mapGetters } from 'vuex'
import { getInfo, getOrder, getGoodsRank } from '@/api/dashboard'
import { dateFormat } from '@/utils/util'

export default {
  name: 'Dashboard',
  created() {
    this.getDataList({
      pageSize: this.pageSize,
      pageNum: this.currentPage,
      readFlag: false
    })

    getRouter({
      flag: 'index',
      adminUserId: this.userid
    }).then(res => {
      this.menuList = res.data
      console.log(res.data)
    })
    this.getMenuList()
  },
  computed: {
    ...mapGetters(['showMessages', 'userid'])
  },
  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: []
    }
  },

  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/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
        })
      }
    },
    // 更改当前页
    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
        })
      }
    },
    // 获取返利确认单数据
    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
    },
    // 选择
    checkFn(v) {
      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
        })
      }
    },
    // 关闭
    closeFn() {
      this.$store.commit('user/showMessage', 'no')
    },

    // 点击菜单
    clickMenu(path) {
      const curlArr = ['/notice', '/issue']
      if (curlArr.includes(path)) {
        path += '/index'
      }
      this.$router.push({
        path
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.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;
  }
}
</style>
