<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
    </div>
    <div class="app-container">
      <div class="cell clearfix">
        <el-input
            size="small"
            style="width: 150px"
            v-model="keywords"
            class="filter-item"
        />
        <el-button
            size="small"
            style="margin: 0 10px"
            class="filter-item"
            type="primary"
            @click="searchAction"
        >搜索
        </el-button>
        <el-date-picker
            v-model="record_date"
            prefix-icon="el-icon-date"
            @change="handleScheduleDateChange"
            :editable="false"
            :clearable="false"
            style="width: 196px; margin-right: 10px"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期时间"
            align="right"
        ></el-date-picker>
        <el-button
            size="small"
            style="margin: 0 10px"
            class="filter-item"
            type="primary"
            @click="batchPrint(1)"
        >处方筏批量打印
        </el-button
        >
        <el-button
            size="small"
            style="margin: 0 10px"
            class="filter-item"
            type="primary"
            @click="batchPrint(2)"
        >治疗单批量打印
        </el-button
        >
        <el-button
            size="small"
            style="margin: 0 10px"
            class="filter-item"
            type="primary"
            @click="batchPrint(3)"
        >检验单批量打印
        </el-button
        >
      </div>
      <el-table
          :data="tableData"
          border
          ref="table"
          style="width: 100%"
          :row-style="{ color: '#303133' }"
          @selection-change="handleSelectionChange"
          :header-cell-style="{
          backgroundColor: 'rgb(245, 247, 250)',
          color: '#606266',
        }"
          highlight-current-row
      >
        <el-table-column
            align="center"
            type="selection"
            width="55"
        ></el-table-column>
        <el-table-column
            align="center"
            label="序号"
            width="60"
            type="index"
        ></el-table-column>
        <el-table-column align="center" prop="name" label="姓名" width="100">
          <template slot-scope="scope">{{
              scope.row.patients ? scope.row.patients.name : ''
            }}
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            prop="name"
            label="处方日期"
            width="200"
        >
          <template slot-scope="scope">
            {{ getTimes(scope.row.ctime) }}
          </template
          >
        </el-table-column>
        <!--<el-table-column align="center" prop="name" label="患者类型">-->
        <!--<template slot-scope="scope">{{}}</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" prop="name" label="处方号">
          <template slot-scope="scope">{{
              scope.row.prescription_number
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="开立医生">
          <template slot-scope="scope">{{ scope.row.doctor }}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="诊断">
          <template slot-scope="scope">{{
              getDiagnosis(scope.row.diagnosis)
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="状态" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.id > 0">已就诊</div>
            <div v-else>未就诊</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                @click="handerShowDetail(scope.row)"
            >详情
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 50, 100]"
          :page-size="10"
          background
          style="margin-top: 20px; float: right"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
      <inquiries-detail ref="inquiriesDetail"></inquiries-detail>
      <el-dialog
          class="centerDialog"
          width="600px"
          title="打印"
          :visible.sync="innerVisible"
          append-to-body
      >
        <template>
          <el-button
              style="float: right"
              size="small"
              icon="el-icon-printer"
              @click="printThisPage(printData)"
              type="primary"
          >打印
          </el-button>
        </template>
        <div id="printMain">
          <print
              ref="print"
              v-if="printType == 1"
              :printData="printData"
          ></print>
          <treatPrint
              ref="treatPrint"
              v-if="printType == 2"
              :printData="printData"
          ></treatPrint>
          <checkPrint
              ref="checkPrint"
              v-if="printType == 3"
              :printData="printData"
          ></checkPrint>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import inquiriesDetail from './components/inquiriesDetail'
import { getBatchPrint, getHisPrescriptionList, getInitData } from '@/api/his/his'
import { uParseTime } from '@/utils/tools'
import print from './batch_print_template/index'
import treatPrint from './batch_print_treatTemplate/index'
import checkPrint from './batch_print_checkTemplate/index'

export default {
  components: {
    BreadCrumb,
    inquiriesDetail,
    print,
    treatPrint,
    checkPrint
  },
  data() {
    return {
      keywords: '',
      total: 0,
      record_date: '',
      page: 1,
      limit: 10,
      crumbs: [
        { path: false, name: '门诊医生站' },
        { path: false, name: '既往查询' }
      ],
      tableData: [],
      diagnoses: [],
      innerVisible: false,
      selecting_schs: [],
      printData: [],
      printType: 0,
      org_id: ''
    }
  },
  methods: {
    printThisPage(printData) {
      printData.forEach((item) => {
        const print = new Promise((resolve, reject) => {
          if (item.prescriptions.length != 0) {
            resolve(item.prescriptions)
          } else {
            reject(false)
          }
        })
        print
            .then((printData) => {
              if (this.org_id == 10138) {
                const style =
                    '@media print {#prescription-print{font-size:14px;border:1px solid white}.prescription-print:last-child {page-break-after: auto;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 10px;}.infoTitle{display: flex;margin-top:10px;}.infoTitle div{width: 200px;}.infoMain{display: flex;flex-wrap: wrap;margin-top:10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:350px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}@media print {html {zoom: 88%;}}#prescription-print{font-size:14px;border:1px solid white}.prescription-print:last-child {page-break-after: auto;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 10px;}.infoTitle{display: flex;margin-top:10px;}.infoTitle div{width: 200px;}.infoMain{display: flex;flex-wrap: wrap;margin-top:10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:350px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}@media print {html {zoom: 87%;}}}'
                printJS({
                  printable: 'printMain',
                  type: 'html',
                  style: style,
                  scanStyles: false
                })
              }
              if (this.printType == 1) {
                const style =
                    '@media print {html {height: auto;margin: 0px;} #prescription-print{font-size:14px;border:1px solid white;margin-bottom:40px;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 10px;}.infoTitle{display: flex;border-bottom: 2px solid #000;margin-top:10px;line-height: 1px; padding:0 10px;}.infoTitle p{width: 200px;font-size:14px;}.infoMain{display: flex;flex-wrap: wrap;border-bottom: 2px solid #000;padding:0 10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:450px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}@media print {html {zoom: 80%;}}#prescription-print{font-size:14px;border:1px solid white}.prescription-print:last-child {page-break-after: auto;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 20px;}.infoTitle{display: flex;margin-top:10px;line-height: 24px;}.infoTitle div{width: 200px;}.infoMain{display: flex;flex-wrap: wrap;margin-top:10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:450px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}.NoCell{display: flex;justify-content: space-between;margin-bottom: 10px;}.underLine{display: inline-block;border-bottom: 1px solid #000;text-align: center;line-height: 24px;}.basicCell{display: flex;} .basicCell span{line-height: 24px;margin-bottom: 10px;}}'
                printJS({
                  printable: 'printMain',
                  type: 'html',
                  style: style,
                  scanStyles: false
                })
              }
              if (this.printType == 2) {
                const style =
                    '@media print {html {height: auto;margin: 0px;} .printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 10px;}.infoTitle{display: flex;border-bottom: 2px solid #000;margin-top:10px;line-height: 1px; padding:0 10px;}.infoTitle p{width: 200px;font-size:14px;}.infoMain{display: flex;flex-wrap: wrap;border-bottom: 2px solid #000;padding:0 10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:360px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}@media print {html {zoom: 80%;}}#prescription-print{font-size:14px;border:1px solid white}.prescription-print:last-child {page-break-after: auto;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 20px;}.infoTitle{display: flex;margin-top:10px;line-height: 24px;}.infoTitle div{width: 200px;}.infoMain{display: flex;flex-wrap: wrap;margin-top:10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:450px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}.NoCell{display: flex;justify-content: space-between;margin-bottom: 10px;}.underLine{display: inline-block;border-bottom: 1px solid #000;text-align: center;line-height: 24px;}.basicCell{display: flex;} .basicCell span{line-height: 24px;margin-bottom: 10px;}.doctor_style{border: none}.doctor_style tr{page-break-inside:avoid;}.doctor_style tr td{border: 1px solid #000;}}'
                printJS({
                  printable: 'printMain',
                  type: 'html',
                  style: style,
                  scanStyles: false
                })
              }
              if (this.printType == 3) {
                const style =
                    '@media print {html {zoom: 80%;}}#prescription-print{font-size:14px;border:1px solid white}.prescription-print:last-child {page-break-after: auto;}#prescriptionPrint div{box-shadow:none;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 20px;}.infoTitle{display: flex;margin-top:10px;line-height: 24px;}.infoTitle div{width: 200px;}.infoMain{display: flex;flex-wrap: wrap;margin-top:10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:450px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}.NoCell{display: flex;justify-content: space-between;margin-bottom: 10px;}.underLine{display: inline-block;border-bottom: 1px solid #000;text-align: center;line-height: 24px;}.basicCell{display: flex;} .basicCell span{line-height: 24px;margin-bottom: 10px;}}'
                printJS({
                  printable: 'printMain',
                  type: 'html',
                  style: style,
                  scanStyles: false
                })
              } 
              // else {
              //   const style =
              //       '@media print {html {height: auto;margin: 0px;} #prescription-print{font-size:14px;border:1px solid white;margin-bottom:40px;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 10px;}.infoTitle{display: flex;border-bottom: 2px solid #000;margin-top:10px;line-height: 1px; padding:0 10px;}.infoTitle p{width: 200px;font-size:14px;}.infoMain{display: flex;flex-wrap: wrap;border-bottom: 2px solid #000;padding:0 10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:450px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}@media print {html {zoom: 80%;}}#prescription-print{font-size:14px;border:1px solid white}.prescription-print:last-child {page-break-after: auto;}.printTitle{font-size: 22px;text-align: center;font-weight: bold;margin-bottom: 20px;}.infoTitle{display: flex;margin-top:10px;line-height: 24px;}.infoTitle div{width: 200px;}.infoMain{display: flex;flex-wrap: wrap;margin-top:10px;}.infoMain div{width: 50%;line-height: 24px;}.prescriptionBox{min-height:450px;}.Rp{font-size: 18px;font-weight: bold;}.drugsBox{padding-left: 40px;margin-bottom: 10px;}.drugsBox div{line-height: 20px;}.drugsOne{line-height: 24px;}.drugsOne span{margin-right: 20px;}.doctorBox{display: flex;justify-content: space-between;padding:0 10px;line-height: 24px;border-bottom: 2px solid #000;}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar p{width:150px;}.under_line{display: inline-block;border-bottom: 1px solid #000;flex: 1;}.NoCell{display: flex;justify-content: space-between;margin-bottom: 10px;}.underLine{display: inline-block;border-bottom: 1px solid #000;text-align: center;line-height: 24px;}.basicCell{display: flex;} .basicCell span{line-height: 24px;margin-bottom: 10px;}}'
              //   printJS({
              //     printable: 'printMain',
              //     type: 'html',
              //     style: style,
              //     scanStyles: false
              //   })
              // }
            })
            .catch((reject) => {
            })
      })

      this.innerVisible = false
      // this.$nextTick(() => {
      //   this.$refs.print.printThisPage()
      //   this.$refs.treatPrint.printThisPage()
      //   this.$refs.checkPrint.printThisPage()
      // })
    },

    searchAction() {
      this.page = 1
      this.limit = 10
      this.getHisPrescriptionList()
    },
    handleScheduleDateChange() {
      this.page = 1
      this.limit = 10
      this.getHisPrescriptionList()
    },
    handleCurrentChange(page) {
      this.page = page
      this.getHisPrescriptionList()
    },

    handleSizeChange(limit) {
      this.limit = limit
      this.getHisPrescriptionList()
    },
    getTimes(time) {
      return uParseTime(time, '{y}-{m}-{d} {h}:{i}:{s}')
    },
    handerShowDetail(row) {
      if (row.id == 0) {
        this.$message.error('未就诊')
        return
      }
      this.$refs.inquiriesDetail.show(row.id)
    },
    getHisPrescriptionList() {
      let params = {
        record_date: this.record_date,
        page: this.page,
        limit: this.limit,
        keywords: this.keywords
      }
      getHisPrescriptionList(params).then((response) => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg)
          return false
        } else {
          this.tableData = response.data.data.order
          this.total = response.data.data.total
          this.$nextTick(() => {
            this.$refs.table.doLayout() //解决表格错位
          })
        }
      })
    },
    getInitData() {
      getInitData().then((response) => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg)
          return false
        } else {
          this.diagnoses = response.data.data.diagnose
          console.log('诊断列表', this.diagnoses)
        }
      })
    },
    getDiagnosis(id) {
      let ids = id.split(',')
      var name = ''
      for (let i = 0; i < ids.length; i++) {
        for (let b = 0; b < this.diagnoses.length; b++) {
          if (parseInt(ids[i]) == this.diagnoses[b].id) {
            if (name.length == 0) {
              name = this.diagnoses[b].class_name
            } else {
              name = name + ',' + this.diagnoses[b].class_name
            }
          }
        }
      }
      return name
    },
    handleSelectionChange(val) {
      this.selecting_schs = val
    },
    batchPrint(type) {
      this.printType = type
      if (this.selecting_schs.length === 0) {
        this.$message.error('请至少选择一条需要打印的内容')
        return false
      } else {
        var sch_ids = []
        for (let index = 0; index < this.selecting_schs.length; index++) {
          sch_ids.push(this.selecting_schs[index].patient_id)
        }
        let params = {
          record_date: this.record_date,
          patient_ids: sch_ids.join(','),
          type: type
        }
        getBatchPrint(params).then((res) => {
          let Arr = res.data.data.patients


          let new_patients = []
          if (type != 3) {
            Arr.forEach((i) => {
              if (i.prescriptions.length != 0) {
                new_patients.push(i)
              }
            })
          } else {
            Arr.forEach((el) => {
              el.new_prescriptions.forEach((o) => {
                o.project.forEach((i) => {
                  if (i.team.project_team != '') {
                    new_patients.push(el)
                  }
                })
              })
            })
            let projectPrint = []
            new_patients.forEach((el) => {
              el.new_prescriptions.map((item) => {
                if (item.project.length > 0) {
                  item.project.map((it) => {
                    projectPrint.push(it)
                  })
                }
              })
            })
            // 数组去重
            projectPrint = this.split_project(projectPrint)
            // 配对
            let data = []
            projectPrint.map((item) => {
              if (item.team.id == 0) {
                data.push(item)
              } else {
                if (item.team.project_team != '') {
                  new_patients.push(item)
                }
              }
            })
            new_patients = this.split_project_1(new_patients)
          }
          this.printData = new_patients
          console.log(this.printData, 'this.printData')
        })
      }
      this.innerVisible = true
    },
    // 外层去重方
    split_project(Arr) {
      let hash = {}
      let new_Arr = []
      new_Arr = Arr.reduce((preVal, curVal) => {
        hash[curVal.id] ? '' : (hash[curVal.id] = true && preVal.push(curVal))
        return preVal
      }, [])
      Arr = new_Arr.reduce((preVal, curVal) => {
        hash[curVal.team.project_team]
            ? ''
            : (hash[curVal.team.project_team] = true && preVal.push(curVal))
        return preVal
      }, [])
      return Arr
    },

    // 内层去重
    split_project_1(Arr) {
      let hash = {}
      let new_Arr = []
      Arr = Arr.reduce((preVal, curVal) => {
        hash[curVal.id] ? '' : (hash[curVal.id] = true && preVal.push(curVal))
        return preVal
      }, [])

      Arr.forEach(el => {
        if (el.name) {
          new_Arr.push(el)
        }
      })

      console.log(new_Arr, 'Arr')
      return new_Arr
    }
  },
  created() {
    var nowDate = new Date()
    var nowYear = nowDate.getFullYear()
    var nowMonth = nowDate.getMonth() + 1
    var nowDay = nowDate.getDate()
    this.record_date =
        nowYear +
        '-' +
        (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
        '-' +
        (nowDay < 10 ? '0' + nowDay : nowDay)

    this.org_id = this.$store.getters.xt_user.template_info.org_id

    this.getHisPrescriptionList()
    this.getInitData()
  }
}
</script>
