<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <Collapse :screen-form="screenForm">
        <template #right_btn>
          <el-button size="mini" @click="resetScreenForm">清空</el-button>
          <el-button size="mini" type="primary" @click="submitScreenForm">搜索</el-button>
        </template>
        <template #search>
          <el-form ref="screenForm" :model="screenForm" label-width="60px" size="mini" label-position="left">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="18" :lg="18">
                <el-form-item label="标题" prop="title">
                  <el-input v-model="screenForm.title" placeholder="请输入标题"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </Collapse>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fl">
          <el-button
            class="fl"
            size="small"
            type="primary"
            icon="el-icon-plus"
            @click="clickImport()"
            v-if="$checkBtnRole('add', $route.meta.roles)"
            >新增</el-button
          >
          <div class="tips fl">导入模板第一行必须为空白行、第一列必须为经销商编号</div>
        </div>
      </div>
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column align="center" label="操作" fixed="right" width="160">
            <template slot-scope="scope">
              <el-button type="text" @click="openDetail(scope.row.id)" v-if="checkBtnRole('edit')">详情</el-button>
              <el-button type="text" @click="handleExport(scope.row)" v-if="checkBtnRole('edit')">下载</el-button>
              <el-popconfirm
                style="margin-left: 10px"
                title="确定删除吗？"
                @onConfirm="deleteData(scope.row.id)"
                v-if="$checkBtnRole('del', $route.meta.roles)"
              >
                <el-button slot="reference" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="标题"
            prop="title"
            min-width="140"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="center" label="操作人" prop="operatorUserName" min-width="140" show-overflow-tooltip>
            <template slot-scope="scope"> {{ scope.row.operatorNickName }}({{ scope.row.operatorUserName }}) </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="remark"
            label="备注"
            min-width="140"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
          >
          </el-pagination>
        </div>
      </div>
    </div>

    <!-- 新增 -->
    <el-dialog
      title="新增"
      :visible.sync="importFormDialog"
      :show-close="false"
      width="40%"
      :close-on-click-modal="false"
    >
      <el-form ref="importForm" label-position="left" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="importForm.title" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="上传文件" prop="fileUrl">
          <FileUpload :fileList="fileList" :fileType="['excel']" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="importForm.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelImportForm">取 消</el-button>
        <el-button type="primary" @click="submitImportForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog title="详情" :visible.sync="detailDialog" :show-close="false" width="70%" :close-on-click-modal="false">
      <div class="table" style="margin: 10px 0 20px">
        <el-table
          v-loading="detailTable_listLoading"
          :data="detailTable_dataList"
          element-loading-text="Loading"
          tooltip-effect="dark"
          style="width: 100%"
          max-height="270"
        >
          <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column
            align="center"
            prop="title"
            label="标题"
            min-width="140"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            prop="adminNickName"
            label="下载人"
            min-width="140"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            prop="remark"
            label="备注"
            min-width="140"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="center" prop="createTime" label="下载时间" min-width="160"></el-table-column>
        </el-table>
      </div>
      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @current-change="detailTableCurrentChange"
            :current-page="detailTable_currentPage"
            :page-size="detailTable_pageSize"
            background
            layout="prev, pager, next"
            :total="detailTable_listTotal"
          >
          </el-pagination>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="detailDialog = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { getList, getDownloadList, deleteData, exportFile, handleImport } from '@/api/issue'
import FileUpload from '@/components/Common/file-upload.vue'

export default {
  components: {
    FileUpload
  },
  data() {
    return {
      imageURL: this.$imageUrl,
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      screenForm: {
        // 筛选表单数据
        title: ''
      },
      importForm: {
        title: '',
        remark: ''
      },
      importFormDialog: false,
      fileList: [],
      isCollapse: true,
      detailDialog: false, // 详情 - 弹窗
      detailTable_dataList: null, // 详情 - 列表数据
      detailTable_listLoading: true, // 详情 - 列表加载loading
      detailTable_currentPage: 1, // 详情 - 当前页码
      detailTable_pageSize: 10, // 详情 - 每页数量
      detailTable_listTotal: 0 // 详情 - 列表总数
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    // 查询按钮权限
    checkBtnRole(value) {
      return true
      let btnRole = this.$route.meta.roles
      if (!btnRole) {
        return true
      }
      let index = btnRole.indexOf(value)
      return index >= 0 ? true : false
    },

    // 获取列表
    getList() {
      this.listLoading = true
      let params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        title: this.screenForm.title
      }
      getList(params).then(res => {
        this.listLoading = false
        this.dataList = res.data ? res.data.records : []
        this.listTotal = res.data ? res.data.total : 0
      })
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getList()
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1
      this.getList()
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields()
      this.currentPage = 1
      this.getList()
    },

    // 操作 - 删除
    deleteData(id) {
      deleteData({ id }).then(res => {
        this.getList()
        this.$successMsg()
      })
    },

    // 详情 - 获取列表
    getDownloadList() {
      getDownloadList({
        pageNo: this.detailTable_currentPage,
        pageSize: this.detailTable_pageSize,
        id: this.detailId
      }).then(res => {
        this.detailTable_dataList = res.data.records
        this.detailTable_listTotal = res.data.total
        this.detailTable_listLoading = false
      })
    },

    // 详情 - 打开弹窗
    openDetail(id) {
      this.detailId = id
      this.detailDialog = true
      this.detailTable_currentPage = 1
      this.getDownloadList()
    },

    // 详情 - 更改列表当前页
    detailTableCurrentChange(val) {
      this.detailTable_currentPage = val
      this.getDownloadList()
    },

    // 点击导入
    clickImport() {
      this.importFormDialog = true
    },

    // 取消 导入
    cancelImportForm() {
      this.importFormDialog = false
      this.fileList = []
      this.importForm.title = ''
      this.importForm.remark = ''
    },

    // 导入
    async submitImportForm() {
      if (!this.importForm.title) {
        return this.$errorMsg('请填写标题')
      }
      if (this.fileList.length <= 0) {
        return this.$errorMsg('请上传文件')
      }
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const formData = {
        file: this.fileList[0].url,
        title: this.importForm.title,
        remark: this.importForm.remark
      }
      handleImport(formData)
        .then(res => {
          this.fileList = []
          this.getList()
          this.importFormDialog = false
        })
        .catch(res => {
          this.$errorMsg(res.message)
        })
        .finally(res => {
          loading.close()
        })
    },

    // 下载
    handleExport(item) {
      exportFile({ id: item.id }).then(res => {
        if (res.data) {
          window.open(this.imageURL + res.data)
        }
      })
    }
  }
}
</script>

<style scoped>
.tips {
  font-size: 14px;
  color: red;
  line-height: 32px;
  margin-left: 10px;
}
</style>
