<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-if="showSearch">
        <el-row style=" display:flex;  flex-wrap: wrap; ">
          <el-col :span="8">
            <el-form-item class="query-form-item" label="项目收费日期:">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="onReset"
          >重置
          </el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit"
          >查询
          </el-button
          >
          <el-button type="primary" icon="el-icon-document" @click="submitDownload" :loading="searchExportLoading">
            结果下载
          </el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row
                :row-style="{ height: '32px' }"
      >
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column prop="chargTime" width="140" label="收费时间"></el-table-column>
        <el-table-column prop="sfxm" width="140" label="收费项目"></el-table-column>
        <el-table-column prop="sfmc" width="140" label="项目名称"></el-table-column>
        <el-table-column prop="je" width="140" label="收费金额"></el-table-column>
        <el-table-column prop="deptCode" width="140" label="部门编码"></el-table-column>
        <el-table-column prop="deptName" width="140" label="部门名称"></el-table-column>

      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>

  </div>
</template>

<script>
import {
  getDeviceBusinessProject, downloadDeviceBusinessProject
} from '@/api/dev/deviceBusinessProjectApi'

export default {
  name: 'downloadDataDialog',
  props: {
    closeDialog: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      showSearch: true,
      list: [],
      busTypeOptions: [],
      filterQuery: {
        startChargTime: null,
        endChargTime: null,
        page: 1,
        limit: 20,
      },

      printMap: {
        0: '未打印',
        1: '已打印',
        null: '未打印'
      },

      fromOptions: [],
      actions: [],
      loading: false,
      selectExportLoading: false,
      searchExportLoading: false,
      downloadDataVisible: false,
      downloadDataLoading: false,
      optinPrintLoading: false,
      selectPrinttLoading: false,
      total: 0,
      tableHeader: [],
      queryList: [],
      fromList: [],
      options: {
        getBusTypeByInv: []
      },
      orderSelection: [],
      showSup: false,
      customerId: this.$store.getters.customerId,
      map: {
        'actDateRange': this.actDateRange,
        'confirmDateRange': this.confirmDateRange
      },
      invList: [],
      invListAllFlagInv: [],
      deptList: [],
      actDateRange: [],
      confirmDateRange: [],
      actionType: null,
      auditDateRange: [],
      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])
            }
          }
        ]
      }
    }
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        startChargTime: null,
        endChargTime: null,
        page: 1,
        limit: 20,
      };
      this.actDateRange = []
    },
    onSubmit() {
      this.loading = true;
      this.filterQuery.page = 1;

      this.getList();
    },
    submitDownload(){
      this.loading = true;
      if (this.actDateRange !== null) {
        this.filterQuery.startChargTime = this.actDateRange[0];
        this.filterQuery.endChargTime= this.actDateRange[1];
      } else {
        return this.$message.error("请选择查询时间范围！");
      }
      downloadDeviceBusinessProject(this.filterQuery)
        .then((response) => {
          if (response.code === 20000) {
            this.$message.success("下载成功");
            this.closeDialog()
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    getList() {
      this.loading = true;
      if (this.actDateRange !== null) {
        this.filterQuery.startChargTime = this.actDateRange[0];
        this.filterQuery.endChargTime= this.actDateRange[1];
      } else {
        return this.$message.error("请选择查询时间范围！");
      }

      getDeviceBusinessProject(this.filterQuery)
        .then((response) => {
          if (response.code === 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
  }
}
</script>

<style scoped>

</style>
