<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="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入材料名称"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交人姓名" prop="name" label-width="120px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入提交人姓名"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交时间范围" label-width="150px" prop="supervisorName">
        <el-date-picker
          v-model="timeRange"
          type="daterange"
          align="right"
          :editable="false"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:pro:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column label="材料名称" align="center" prop="fileName" />
      <el-table-column label="材料大小" align="center" prop="fileSize" />
      <el-table-column label="材料类型" align="center" prop="fileType" />
      <el-table-column label="提交人" align="center" prop="name" />
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePreview(scope.row)"
            v-hasPermi="['system:post:view']"
          >预览</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDownload(scope.row)"
            v-hasPermi="['system:post:download']"
          >下载</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="文件预览" :visible.sync="open" width="90%" append-to-body>
      <div style="width: 100%;height:720px">
        <bns-kkFile-preview :items="filePreview"></bns-kkFile-preview>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getProProfile } from "@/api/pro/outsourcingPro";
import bnsKkFilePreview from '@/components/pro-tabs/bns-kkFile-preview.vue';
import { downloadFile } from '@/api/tool/select'
import { indexContinuous } from '@/utils/bonus'
export default {
  name: "Post",
  components: {
    'bns-kkFile-preview': bnsKkFilePreview,
  },
  data() {
    return {
      proId: undefined,
      filePreview: {
        filePreviewUrl: '',
        fileName: '',
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件表格数据
      fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      timeRange: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: undefined,
        name: undefined,
        startDate: undefined,
        endDate: undefined,
      },
      //时间范围选择器
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.proId = this.$route.params && this.$route.params.proId
    this.getList();
  },
  methods: {
    indexContinuous,
    /** 更改时间*/
    changeTime(){
      this.queryParams.startDate = this.timeRange[0]
      this.queryParams.endDate = this.timeRange[1]
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.queryParams.proId = this.proId;
      getProProfile(this.queryParams).then(response => {
        this.fileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fileId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.changeTime()
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.startDate = undefined
      this.queryParams.endDate = undefined
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.derive('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    },
    /** 预览文件 */
    handlePreview(row) {
      this.open = true;
      this.filePreview = {
        filePreviewUrl: row.filePath,
        fileName: row.fileName,
      }
    },
    handleDownload(row){
      downloadFile(row.filePath, row.fileName)
    }
  }
};
</script>
