<template>
  <div>
    <el-card class="el-card">

      <el-form :model="filterQuery" label-width="auto" v-if="showSearch" size="mini">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="患者编号:" class="query-form-item">
              <el-input v-model="filterQuery.code" placeholder="请输入患者编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="患者姓名:" class="query-form-item">
              <el-input v-model="filterQuery.name" placeholder="请输入患者姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="住院号:" class="query-form-item">
              <el-input v-model="filterQuery.adNum" placeholder="请输入住院号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="外部系统:">
              <el-select v-model="filterQuery.thirdSys" style="width: 90%" placeholder="请选择第三方系统">
                <el-option
                  v-for="item in thirdSys"
                  :key="item.value"
                  :label="item.thirdName"
                  :value="item.thirdId">
                  <span style="float: left">{{ item.thirdName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <el-button-group class="top-right-btn">
        <el-button type="primary" icon="el-icon-refresh" @click="showSearch = !showSearch">显示/隐藏搜索栏</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-download" @click="downloadErp">选中下载</el-button>
        <el-button type="primary" icon="el-icon-download" @click="downloadAllErp">结果全部下载
        </el-button>

      </el-button-group>
      <el-divider></el-divider>


      <el-table v-loading="loading" :data="list"
                @selection-change="handleSelectionChange"
                border highlight-current-row
                style="width: 100%">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" type="index" width="80"></el-table-column>
        <el-table-column label="患者编号" prop="code" width="160"></el-table-column>
        <el-table-column label="患者姓名" prop="name" width="160"></el-table-column>
        <el-table-column label="性别" prop="createByName" width="160"></el-table-column>
        <el-table-column label="住院号" prop="adNum" width="160"></el-table-column>
        <el-table-column label="职业" prop="gender" width="160"></el-table-column>
        <el-table-column label="最后更新时间" prop="createTime" width="180"></el-table-column>
      </el-table>

    </el-card>
  </div>
</template>

<script>

import {dlSickerOnline, downloadSick} from "@/api/basic/sicker/skPersonApi";
import {getBasicThirdSys} from "@/api/thrsys/basicThirdSys";

let query = {
  name: "",
  code: "",
  adNum: "",
  thirdSys: "thirdId",
  selectSickers: null,
  page: 1,
  limit: 10
}
export default {
  name: "skPersonDownload",
  props: {
    selectType: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      filterQuery: {...query},
      showSearch: true,
      multipleSelection: [],
      loading: false,
      list: [],
      thirdSys: [],
    }

  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    onReset() {
      this.filterQuery = {...query}
      this.getList()
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList()
    },

    getList() {
      this.loading = true;
      dlSickerOnline(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;
        });
    },


    downloadErp() {
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message.warning("请选择需要下载患者信息！");
        return
      }
      let selectData = this.multipleSelection;
      this.filterQuery.selectSickers = []
      selectData.forEach((obj) => {
        this.filterQuery.selectSickers.push(obj);
      });

      downloadSick(this.filterQuery).then(res => {
        this.dlSickLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        } else {
          this.$emit("cancelDialog", true);
          this.$message.success("后台正在下载更新，请稍后刷新重试！");
        }
      }).catch(() => {
        this.dlSickLoading = false
        this.$message.error("数据加载失败")
      })
    },

    downloadAllErp() {
      this.dlSickLoading = true;
      downloadSick({thidSys: "thirdId"}).then(res => {
        this.dlSickLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        } else {
          this.$emit("cancelDialog", true);
          this.$message.success("后台正在下载更新，请稍后刷新重试！");
        }
      }).catch(() => {
        this.dlSickLoading = false
        this.$message.error("数据加载失败")
      })
    },


    getBasicThirdSys() {
      let query = {
        enabled: true,
      };
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || [];
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },
  },
  created() {
    this.getBasicThirdSys();
  },
}
</script>

<style scoped>

</style>
