<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="月份:" prop="exportMonth">
        <el-date-picker
          v-model="queryParams.exportMonth"
          type="month"
          value-format="yyyy-MM"
          :clearable="false"
          placeholder="请选择月份" />
      </el-form-item>
      <el-form-item label="部门:" prop="deptCode">
        <el-select
          v-model="queryParams.deptCode"
          filterable
          :clearable="false"
          placeholder="请选择部门"
        >
          <el-option
            v-for="(dict, index) in deptList"
            :key="index"
            :label="dict.deptName"
            :value="dict.deptCode"
          />
        </el-select>
      </el-form-item>
      
      <!-- <el-row :gutter="10" class="mb8" type="flex" justify="end"> -->
        <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-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            :loading="downLoading"
            >导出月报</el-button
          >
        </el-form-item>
      <!-- </el-row> -->
    </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"
          >导出月报</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="150" align="center" prop="reportType" />
      <el-table-column label="导出参数" min-width="150" align="center" prop="exportParam" />
      <el-table-column label="导出人" min-width="200" align="center" prop="createdBy" />
      <el-table-column label="导出时间" min-width="160" align="center" prop="createdTime" />
    </el-table>

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

  </div>
</template>

<script>
import{
  saleMonthList
} from "@/api/saleConfig/saleTarget";
import{
  gyDeptList
} from "@/api/saleConfig/index";

export default {
  name: "saleExport",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      downLoading: false,
      // 总条数
      total: 0,
      queryParams: {
        "deptCode": "",
        "deptName": "",
        "exportMonth": ""
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
      },
      postList: [],
      tableHeight: 'auto',
      pickerOptions: {
        shortcuts: [
          {
            text: "最近三天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            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 * 15);
              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]);
            },
          },
        ],
      },
      deptList: [],
      totalData: {},
      loading: false
    };
  },
  watch:{
    showSearch(n,_){
      this.$nextTick(() => {
        const height = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 210;
        this.tableHeight = height+'px';
      });
    }
  },
  created() {
    const currentMonthFirstDay = new Date();
    currentMonthFirstDay.setMonth(currentMonthFirstDay.getMonth() - 1);
    const year = currentMonthFirstDay.getFullYear();
    const month =
      currentMonthFirstDay.getMonth()  < 10
        ? "0" + (currentMonthFirstDay.getMonth() +1)
        : currentMonthFirstDay.getMonth()+1 ;
    this.queryParams.exportMonth = year  +'-'+ month;
    
    this.relationPlatformList();
    this.getList();
  },
  mounted() {},
  methods: {
    relationPlatformList(){
      gyDeptList({}).then((res) => {
        this.deptList = res.data;
        this.queryParams.deptCode = this.deptList[0].deptCode;
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
    },
    
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
    
      saleMonthList(this.queryParams2).then((response) => {
        this.totalData = response.data;
        this.postList = response.data.exportSaleMonthLogResDTOS;
        this.total = response.data.total;
        this.loading = false;

        this.$nextTick(() => {
          const height = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 210;
          this.tableHeight = height+'px';
        });
      }).catch(err=>{
        this.postList = [];
        this.total = 0;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.resetForm("queryForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams2.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      saleTargetLogs().then(res=>{
        if(res.status==200){
          this.historyLogTableList = res.data;
          this.open = true;
        }
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = 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();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出按钮操作 */
    handleExport() {
      const data = { ...this.queryParams };

      if(data.deptCode){
        const dept = this.deptList.find(item=>{
          return item.deptCode == data.deptCode;
        })
        data.deptName = dept.deptName;
      }
      this.downLoading = true;
      this.newDownload(
        "/export/sale/month/export",
        data,
        `销售月报_${new Date().getTime()}.xlsx`,
        {},
        true,
        ()=>{
          this.downLoading = false;
        }
      );
    },
  },
};
</script>
<style scoped>
.charts-box {
  height: 350px;
  margin-top: 30px;
}
.erpPrice > span {
  color: #409eff;
}
.iconEdit {
  cursor: pointer;
}
.app-container {
  padding-bottom: 50px;
}
.document-link{
  color: #409eff;
  text-decoration: underline;
}
</style>
