<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :optionsEvensGroup="optionsEvensGroup"
    :exportList="exportList"
    :operation="operation()"
    :columnParsing="columnParsing"
  >
    <template #moreSearch>
      <div class="tips fl">导入模板第一行必须为空白行、第一列必须为经销商编号</div>
    </template>
    <!-- 新增 -->
    <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>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import add_callback_mixin from '@/components/template/add_callback_mixin.js'
import Popu from '@/components/template/popu.vue'
import { getToken } from '@/utils/auth'
import { getListV2, getDownloadList, deleteData, exportFile, handleImport } from '@/api/issue'
import FileUpload from '@/components/Common/file-upload.vue'
export default {
  components: { TemplatePage, Popu, FileUpload },
  mixins: [import_mixin, add_callback_mixin],
  data() {
    return {
      visible: false,
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: this.addOn(() => {
                this.clickImport()
              }),
              isRole: this.$checkBtnRole('add', this.$route.meta.roles)
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      }, // 关闭新增弹窗

      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      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 // 详情 - 列表总数
    }
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return getListV2(...p)
    },
    // 列表导出函数
    exportList: () => {},
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            {this.checkBtnRole('edit') ? (
              <el-button
                size="mini"
                type="text"
                onClick={() => {
                  this.openDetail(row.id)
                }}
              >
                详情
              </el-button>
            ) : null}
            {this.checkBtnRole('edit') ? (
              <el-button
                size="mini"
                type="text"
                onClick={() => {
                  this.handleExport(row)
                }}
              >
                下载
              </el-button>
            ) : null}
            {
              <el-popconfirm
                title="确定删除吗？"
                onOnConfirm={() => this.deleteData(row.id)}
              >
                <el-button slot="reference" type="text" size="mini">
                  删除
                </el-button>
              </el-popconfirm>
            }
          </div>
        )
      }
    },
    handleClose() {
      this.addOff(() => {
        this.visible = false
      })()
    },
    // 查询按钮权限
    checkBtnRole(value) {
      return true
      let btnRole = this.$route.meta.roles
      if (!btnRole) {
        return true
      }
      let index = btnRole.indexOf(value)
      return index >= 0 ? true : false
    },
    // 操作 - 删除
    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.addOff(() => {
        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 lang="scss" scoped>
.tips {
  font-size: 14px;
  color: red;
  line-height: 32px;
  margin-left: 10px;
}
</style>
