<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          >导入人力成本分摊</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      ref="proTable"
      :height="tableHeight"
      border
      v-loading="loading"
      :data="postList"
    >
      <el-table-column label="人力成本分摊文件名称" min-width="180" align="center" prop="fileName" />
      <el-table-column label="分摊月份" min-width="180" align="center" prop="configMonth" />
      <el-table-column label="操作人" min-width="180" align="center" prop="createdBy" />
      <el-table-column label="导入时间" min-width="180" align="center" prop="createdTime" />
      <el-table-column
        label="操作"
        width="120"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="dowaLoadBill(scope.row)"
            >下载</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"
      class="fixed-pagination"
    />

    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
      
    >
    <el-form ref="spuRef" :model="form2" label-width="60px">
        <el-form-item
          label="月份:"
          :rules="[{ required: true, message: '请选择月份', trigger: 'change' }]"
        >
          <el-date-picker
            v-model="form2.month"
            type="month"
            value-format="yyyyMM"
            :clearable="false"
            style="width: 225px"
          ></el-date-picker>
          
        </el-form-item>
    </el-form>

      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        class="warehouse-dialog"
        :data='{
          "configDate ": form2.month,
        }'  
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  hrUploadList,
} from '@/api/saleConfig/index';
import { getToken } from "@/utils/auth";

export default {
  name: "hr",
  data() {
    return {
      // 遮罩层
      loading: true,
      showSearch: true,
      submitLoading: false,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 是否显示弹出层
      open: false,
      tableHeight: 'auto',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form2:{
        month: ''
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近三天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
              picker.$emit("pick", [end, start]);
            },
          },
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [end, start]);
            },
          },
          {
            text: "最近半个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
              picker.$emit("pick", [end, start]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [end, start]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [end, start]);
            },
          },
        ],
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: true,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API_NEW + "/hr/upload",
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    handleImport(){
      const currentMonthFirstDay = new Date();
      currentMonthFirstDay.setTime(currentMonthFirstDay.getTime() + 3600 * 1000 * 24 * 30);
      const year = currentMonthFirstDay.getFullYear();
      const month =
        currentMonthFirstDay.getMonth() + 1 < 10
          ? "0" + (currentMonthFirstDay.getMonth() + 1)
          : currentMonthFirstDay.getMonth() + 1;
      this.form2.month = year  + month;
      this.upload.title = "导入";
      this.upload.open = true;
    },
    /** 查询岗位列表 */
    getList(type) {
      this.loading = true;
      hrUploadList(this.queryParams).then((response) => {
        this.postList = response.data.hrListResDTOS;
        this.total = response.data.total;
        this.loading = false;

        this.$nextTick(() => {
          const height = window.innerHeight - 210;
          this.tableHeight = height + "px";
        });
      });
      
    },
    dowaLoadBill(row){
      window.open('https://newfirst.oss-cn-shanghai.aliyuncs.com/'+row.filePath);
    },
    // 提交上传文件
    submitFileForm() {
      this.submitLoading = true;
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.submitLoading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    /** 下载模板操作 */
    importTemplate() {
      // this.newDownload(
      //   "spu/importTemplate",
      //   {},
      //   `spu_template_${new Date().getTime()}.xlsx`
      // );
      window.location.href = 'https://newfirst.oss-cn-shanghai.aliyuncs.com/hrConfig/template/%E6%B8%A0%E9%81%93_%E5%BA%97%E9%93%BA%E8%BF%90%E8%90%A5%E4%BA%BA%E5%8A%9B%E6%88%90%E6%9C%AC%E6%A8%A1%E7%89%88.xlsx';
    },
  },
};
</script>
<style scoped>
.charts-box {
  height: 350px;
  margin-top: 30px;
}
.erpPrice > span {
  color: #409eff;
}
.iconEdit {
  cursor: pointer;
}
.app-container {
  padding-bottom: 50px;
}
.input-box{
  text-align: left;
}
.disabled-text{
  em{
    color: #999;
  }
  
}
</style>
<style lang="scss">
  .warehouse-dialog {
    .el-upload{
      width: 100%;
    }
    .el-upload-dragger{
      width: 100%;
    }
  }
</style>
