<template>
  <div class="wrap-box" v-loading="viewLoding">
    <!-- 头部搜索组件 -->
    <el-card class="box-card">
      <searchView @searchSuses="getSearchData"></searchView>
    </el-card>
    <br />
    <el-card class="box-card">
      <!-- 表格组件 -->
      <tableView
        :getTableData="tabledata"
        @pageTruning="changeTrunig"
      ></tableView>
    </el-card>
  </div>
</template>
<script>
import searchView from "./searchView/index.vue";
import tableView from "./tableView/index.vue";
import {
  getAccountSumHosp,
  getAccountSumSup,
  getAccountSumSupYbj,
} from "@/api/summary/index";
import { supData, hosData } from "./const";
const pagaData = { current: 1, size: 10 };
export default {
  name: "wrapView",
  components: {
    searchView,
    tableView,
  },
  data() {
    return {
      curetPage: 1, //表格当前在第几页
      size: 10, //表格一页有几条数据
      tabledata: {}, //接口请求到的表格数据
      viewLoding: false, //数据加载中loding
      userName: "",
    };
  },
  created() {},
  mounted() {
    //刚进入页面请求数据
    this.getTableData(pagaData);
  },
  methods: {
    //点击搜索按钮触发
    getSearchData(data) {
      Object.keys(data).forEach((item) => {
        this.userName = data[item];
      });
      this.$nextTick(function () {
        this.getTableData({ ...data, ...pagaData });
      });
    },
    //获取表格数据接口
    getTableData(data, search) {
      let { user } = this.$store.state;
      if (supData[user.orgType]) {
        this.getSumHospTableData({ ...data, supplierId: user.userID });
      } else if (hosData[user.orgType]) {
        this.getSumSupTableData(data);
      }
      if (user.orgType == "YBJ") {
        this.getSumHospYbjTableData(data);
      }
    },
    //医保局表格数据
    getSumHospYbjTableData(data) {
      let { initCode, hospId } = this.$route.query;
      getAccountSumSupYbj({
        ...data,
        hospName: this.userName,
        initCode,
        hospId,
      })
        .then((res) => {
          this.viewLoding = false;
          if (res.success == true) {
            this.tabledata = res.data;
          }
        })
        .catch((error) => {
          this.viewLoding = false;
        });
    },
    //配送商表格数据
    getSumHospTableData(data) {
      let { initCode } = this.$route.query;
      // 接口请求
      getAccountSumHosp({
        ...data,
        hospName: this.userName,
        initCode,
      })
        .then((res) => {
          this.viewLoding = false;
          if (res.success == true) {
            this.tabledata = res.data;
          }
        })
        .catch((error) => {
          this.viewLoding = false;
        });
    },
    // 医疗机构用户表格数据
    getSumSupTableData(data) {
      let { userID } = this.$store.state.user;
      let { initCode } = this.$route.query;
      // 接口请求
      getAccountSumSup({
        ...data,
        supplierName: this.userName,
        hospId: userID,
        initCode,
      })
        .then((res) => {
          this.viewLoding = false;
          if (res.success == true) {
            this.tabledata = res.data;
          }
        })
        .catch((error) => {
          this.viewLoding = false;
        });
    },
    //分页器改变触发
    changeTrunig(pageTrunig) {
      this.getTableData({
        current: pageTrunig.current,
        size: pageTrunig.size,
      });
    },
  },
};
</script>
<style scoped>
.wrap-box {
  padding: 5px;
}
</style>
