import {downloadSick, page} from "@/api/basic/sicker/skPersonApi";
import {detail, downloadPrescribe, page as prescribePage} from "@/api/basic/sicker/prescribeApi";

import skPrescribeDi from "./skPrescribeDi.vue";
import skPersonDownload from "./skPersonDownload.vue";

let query = {
  name: "",
  code: "",
  adNum: "",
  page: 1,
  limit: 10
}

let prescribeQuery = {
  sickerCode: '',
  page: 1,
  limit: 10
}

export default {
  data() {
    return {
      fullscreenLoading: false,
      dlSickLoading: false,
      updatePrecribeLoading: false,
      showSearch: true,
      filterQuery: {...query},
      editType: "",
      formVisible: false,
      selectFormVisible: false,
      loading: false,
      formName: "",
      total: 0,
      list: [],
      // =============患者=================
      showSickerDialog: false,
      sicker: null,
      sickDiDialog: false,
      sickDlDialog: false,

      // =============患者end================
      // ============处方=============
      sickerInfo: null,
      showPrescribeDetailDialog: false,
      prescribeLoading: false,
      prescribeList: [],
      prescribeTotal: 0,
      prescribeQuery: {...prescribeQuery},
      prescribeDetail: null
      // ============处方end=============
    };
  }
  ,
  mounted() {
  },
  created() {
    this.getList()
  },
  components: {
    skPrescribeDi, skPersonDownload

  },
  methods: {
    onReset() {
      this.filterQuery = {...query}
      this.getList()
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList()
    },
    getList() {
      this.loading = true
      page(this.filterQuery).then(res => {
        this.loading = false
        this.list = res.data.list || []
        this.total = res.data.total || 0
      }).catch(() => {
        this.loading = false
        this.list = []
        this.total = 0
        this.roles = []
      });
    },
    sickerClick(row) {
      this.sickerInfo = {...row}
      this.prescribeQuery = {...prescribeQuery}
      this.prescribeQuery.sickerCode = row.code
      this.prescribeLoading = true
      this.getPrescribeList()
    },

    getPrescribeList() {
      prescribePage(this.prescribeQuery).then(res => {
        this.prescribeLoading = false
        this.prescribeList = res.data.list || []
        this.prescribeTotal = res.data.total || 0
      }).catch(() => {
        this.prescribeLoading = false
        this.prescribeList = []
        this.prescribeTotal = 0
      })
    },


    showSickerInfo(row) {
      this.sicker = {...row}
      this.showSickerDialog = true
    },


    showPrescribeDiInfo(row) {
      this.sicker = {...row}
      this.sickDiDialog = true
    },


    showPrescribeDetail(row) {
      this.fullscreenLoading = true
      detail({prescribeCode: row.code}).then(res => {
        this.fullscreenLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.prescribeDetail = res.data
        this.showPrescribeDetailDialog = true
      }).catch(() => {
        this.fullscreenLoading = false
        this.$message.error("数据加载失败")
      })
    },
    onDownload() {

      this.sickDlDialog = true;

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

    updatePrecribe() {
      this.updatePrecribeLoading = true;
      downloadPrescribe({sickCode: this.sickerInfo.code, adNum: this.sickerInfo.adNum}).then(res => {
        this.updatePrecribeLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        } else {
          this.$message.success(res.data);
        }
      }).catch(() => {
        this.updatePrecribeLoading = false
        this.$message.error("数据加载失败")
      })
    },
  }
}
