<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="fileType">
        <el-select v-model="queryParams.fileType" placeholder="请选择文件类型" clearable>
          <el-option
            v-for="dict in dict.type.iit_file_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="ID" align="center" prop="id" width="60" :show-overflow-tooltip="true"/>
      <el-table-column label="名称" align="left" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="文件名称" align="left" prop="fileName" :show-overflow-tooltip="true" width="350"/>
      <el-table-column label="文件类型" align="center" prop="fileType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iit_file_type" :value="scope.row.fileType"/>
        </template>
      </el-table-column>
      <el-table-column label="文件大小" align="center" prop="fileSize" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ formatFileSize(scope.row.fileSize) }}
        </template>
      </el-table-column>
      <el-table-column label="推送次数" align="center" prop="pushTimes" width="100"/>
      <el-table-column label="文件备注" align="left" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="上传用户" align="left" prop="userName" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="上传时间" align="left" prop="createTime" width="160"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleFileInfo(scope.row)"
                     v-hasPermi="['iit:file:query']">文件详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改消息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="70px">
        <el-row>
          <el-col :span="14">
            <el-form-item label="文件名称" prop="fileName">
              <el-input v-model="form.fileName" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="文件类型" prop="fileType">
              <el-select v-model="form.fileType" :disabled="true">
                <el-option
                  v-for="dict in dict.type.iit_file_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="上传用户" prop="userName">
              <el-input v-model="form.userName" style="width: 180px" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上传时间" prop="createTime">
              <el-input v-model="form.createTime" style="width: 180px" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="文件大小" prop="fileSize">
              <el-input v-model="form.fileSize" style="width: 180px" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="文件备注" prop="remark">
          <el-input type="textarea" :rows="4" v-model="form.remark" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="downloadFile">文件下载</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMyselfInfo} from "@/api/iit/file";

export default {
  name: "Myself",
  dicts: ['iit_file_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件管理表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        fileName: null,
        fileType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    downloadFile() {
      window.open(process.env.VUE_APP_BASE_API + this.form.fileUrl);
    },
    handleFileInfo(row) {
      console.log(row);
      let fileType = row.fileType
      this.form = row;
      // this.form.fileSize = this.form.fileSize < 1024 ? (this.form.fileSize.toFixed(0) + ' KB') : (this.form.fileSize / 1024).toFixed(2) + ' MB\';
      this.form.fileType = fileType.toString();
      this.open = true;
      this.title = "文件详情";
    },
    formatFileSize(fileSize) {
      if (fileSize < 1024) {
        return fileSize.toFixed(0) + ' KB';
      } else {
        return (fileSize / 1024).toFixed(2) + ' MB';
      }
    },
    /** 查询文件管理列表 */
    getList() {
      this.loading = true;
      listMyselfInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        fileName: null,
        fileType: null,
        fileSize: null,
        filePath: null,
        fileUrl: null,
        remark: null,
        createTime: null,
        userId: null,
        userName: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
};
</script>
<style scoped>
/* 自定义左右面板宽度 */
::v-deep .el-transfer-panel {
  width: 300px; /* 单个面板宽度 */
  height: 500px;
}

::v-deep .el-transfer-panel__body {
  height: 100%;
}

::v-deep .el-transfer-panel__list {
  height: 100%;
}

/* 自定义按钮区域样式 */
::v-deep .el-transfer__buttons {
  width: 210px; /* 按钮区域宽度 */
  padding: 0 10px;
}
</style>
