<template>
  <div class="bg-box">
    <div class="bg-card">
      <SearchBoxVue :data="searchData" @search="handleSearch" @reset="handleReset" />
      <div style="margin-top: -22px;">
        <el-divider></el-divider>
        <TableBoxVue :data="tableData" @sort="handleSort" @export="handleExport" />
        <Pagination :page="paginationData.page" :limit="paginationData.page_size" :total="paginationData.total" @pagination="handlePagination" />
      </div>
    </div>
  </div>
</template>

<script>
import SearchBoxVue from './components/doctorBl/SearchBox.vue'
import TableBoxVue from './components/doctorBl/TableBox.vue'
import Pagination from '@/components/Pagination'
import { bmyDoctorRankingBlExport } from '@/api/excel'

export default {
  components: {
    SearchBoxVue,
    TableBoxVue,
    Pagination
  },
  data() {
    return {
      searchData: {
        time: [],
        level: '',
        doctor_name: [],
        AAC11N: '',
        sort: []
      },
      tableData: [
        {
          "AAA28": "00045458",
          "ZYH": "805905",
          "AAC01": "2023-06-28 08:36:00",
          "AAC11N": "康复医学科",
          "KZRXM": "王德强",
          "ZHFZRYSXM": "王德强",
          "ZZYSXM": "孙芳菲",
          "ZYYSXM": "赵威东",
          "ZZYISXM": "",
          "home_bmy_score": 100,
          "bl_level": "甲级"
        },
        {
          "AAA28": "00047338",
          "ZYH": "805903",
          "AAC01": "2023-06-28 08:35:00",
          "AAC11N": "康复医学科",
          "KZRXM": "王德强",
          "ZHFZRYSXM": "王德强",
          "ZZYSXM": "孙芳菲",
          "ZYYSXM": "赵威东",
          "ZZYISXM": "",
          "home_bmy_score": 100,
          "bl_level": "甲级"
        }
      ],
      paginationData: {
        page: 1,
        page_size: 10,
        total: 0
      }
    }
  },
  created() {
    const { start_time, end_time } = this.$route.query
    this.$set(this.searchData, 'AAC01', [start_time, end_time])
    this.getList()
  },
  methods: {
    getList() {
      const {
        page,
        page_size
      } = this.paginationData
      const { time } = this.searchData
      const params = {
        is_export: 0,
        page,
        page_size,
        ...this.searchData
      }
      if (time && time.length) {
        params.start_time = time[0]
        params.end_time = time[1]
      }
      this.$axios.post('/bmy/doctorRankingBlList', params).then(res => {
        this.tableData = res.data.list
        this.paginationData.total = res.data.count
      });
    },
    handleSearch() {
      this.paginationData.page = 1
      this.getList()
    },
    handleReset() {
      this.searchData = {
        time: [],
        level: '',
        doctor_name: [],
        AAC11N: '',
        sort: []
      }
    },
    handlePagination(val) {
      const { page, limit } = val
      this.paginationData.page = page
      this.paginationData.page_size = limit
      this.getList()
    },
    handleSort(val) {
      this.searchData.sort = val
      this.getList()
    },
    handleExport() {
      const { time } = this.searchData
      const params = {
        is_export: 1,
        ...this.searchData
      }
      if (time && time.length) {
        params.start_time = time[0]
        params.end_time = time[1]
      }
      bmyDoctorRankingBlExport(params).then(res => {
        const content = res.data; // 后台返回二进制数据
        const blob = new Blob([content]);
        const fileName = `首页质控(编码员)-医生病历总数.csv`;
        if ('download' in document.createElement('a')) {
          // 非IE下载
          const elink = document.createElement('a');
          elink.download = fileName;
          elink.style.display = 'none';
          elink.href = URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href); // 释放URL 对象
          document.body.removeChild(elink);
        } else {
          // IE10+下载
          navigator.msSaveBlob(blob, fileName);
        }
      });
    }
  }
}
</script>

<style>

</style>