<template>
  <div class="page_doctorAdvice">
    <div class="doctorAdviceTitle">
      <i class="iconfont icon-zuojiantou jiantou" @click="toReturn"></i>
      <div class="patientName" @click="show = true">
        {{ patientName }}
        <van-icon name="arrow-down" />
      </div>
      <p @click="toEditPatient">基本信息</p>
    </div>
    <van-tabs v-model="active" sticky @click="onClick">
      <van-sticky>
        <div class="wapBox" @click="show1 = true">
          <van-icon class="wap" name="wap-nav" />
        </div>
      </van-sticky>
      <van-tab title="医嘱">
        <doctor-management
          :dryId="patient_id"
          :active="active"
        ></doctor-management>
      </van-tab>
      <van-tab title="长期透析处方">
        <long-dialysis :active="active"></long-dialysis>
      </van-tab>
      <van-tab title="透析记录">
        <dialysis-record :active="active"></dialysis-record>
      </van-tab>
      <van-tab title="干体重">
        <dry-weight :active="active" :dryId="patient_id"></dry-weight>
      </van-tab>
      <van-tab title="检验检查">
        <inspection :active="active" v-if="$store.getters.user.template_info.org_id==10580 || $store.getters.user.template_info.org_id==9882 || $store.getters.user.template_info.org_id==10206"></inspection>
        <new_Inspection :active="active" :patientName="patientName" v-if="$store.getters.user.template_info.org_id!=10580 && $store.getters.user.template_info.org_id!=9882 && $store.getters.user.template_info.org_id!=10206"></new_Inspection>
      </van-tab>
      <van-tab title="传染病管理">
        <contagion :active="active"></contagion>
      </van-tab>
      <van-tab title="病程记录">
        <course-management :active="active"></course-management>
      </van-tab>
      <van-tab title="抢救记录">
        <rescue-record :active="active"></rescue-record>
      </van-tab>
      <van-tab title="排班信息">
        <scheduling :active="active" :schedulingId="patient_id"></scheduling>
      </van-tab>
      <van-tab title="宣教信息">
        <education :active="active"></education>
      </van-tab>
      <van-tab title="体征信息">
        <signs-record :active="active"></signs-record>
      </van-tab>
      <van-tab title="康复笔记">
        <recover-notes :active="active"></recover-notes>
      </van-tab>
      <van-tab title="用药管理">
        <medication-plan :active="active"></medication-plan>
      </van-tab>
      <van-tab title="设备管理">
        <device-management :active="active"></device-management>
      </van-tab>
    </van-tabs>
    <!-- 患者弹窗 -->
    <van-popup v-model="show" position="top" :style="{ height: '100%' }">
      <div
        style="padding:10px;font-size:1.25rem;text-align:right"
        @click="show = false"
      >
        <van-icon name="cross" />
      </div>
      <van-search
        class="doctorAdviceSearch"
        v-model="value"
        placeholder="请输入病人名字"
        @cancel="onCancel"
        @search="onSearch"
      />

      <van-index-bar class="indexBar" sticky highlight-color="#fb6463">
        <div v-for="(item, index) in firstNameTwo" :key="index" :index="index">
          <van-index-anchor :index="index">
            <!-- <span class="indexWord">{{ index }}</span> -->
          </van-index-anchor>
          <van-cell
            @click="chooseCity(citem)"
            v-for="(citem, cindex) in item"
            :key="cindex"
            :title="citem"
          />
        </div>
      </van-index-bar>
    </van-popup>

    <van-popup v-model="show1" position="top" :style="{ height: '40%' }">
      <div
        style="padding:10px;font-size:1.25rem;text-align:right"
        @click="show1 = false"
      >
        <van-icon name="cross" />
      </div>
      <div class="titleBox">
        <div
          class="titleBoxOne"
          v-for="(item, index) in title"
          :key="index"
          @click="changeActive(index)"
        >
          {{ item }}
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import '../../styles/resetStyle.scss'
import DoctorManagement from '@/pages/doctorAdvice/components/DoctorManagement'
import DryWeight from '@/pages/doctorAdvice/components/DryWeight'
import CourseManagement from '@/pages/doctorAdvice/components/CourseManagement'
import RescueRecord from '@/pages/doctorAdvice/components/RescueRecord'
import Inspection from '@/pages/doctorAdvice/components/Inspection'
import LongDialysis from '@/pages/doctorAdvice/components/LongDialysis'
import DialysisRecord from '@/pages/doctorAdvice/components/DialysisRecord'
import Scheduling from '@/pages/doctorAdvice/components/Scheduling'
import Education from '@/pages/doctorAdvice/components/Education'
import signsRecord from '@/pages/doctorAdvice/components/signsRecord'
import recoverNotes from '@/pages/doctorAdvice/components/recoverNotes'
import medicationPlan from '@/pages/doctorAdvice/components/medicationPlan'
import deviceManagement from '@/pages/doctorAdvice/components/deviceManagement'
import contagion from '@/pages/doctorAdvice/components/contagion'
import new_Inspection from '@/pages/doctorAdvice/components/new_Inspection'
import { uParseTime } from '@/utils/tools'
import {
  getPatientDetail,
  getBloodDialysisPatientTwo,
  ToSearch,
  getMemberPatient,
  getSlowPatient,
  getBloodPatient
} from '@/api/patient/patient'
import { setRem } from '@/libs/functionRem'
const moment = require('moment')
let pinyin = require('js-pinyin')
pinyin.setOptions({ checkPolyphone: false, charCase: 0 })
export default {
  components: {
    DoctorManagement,
    DryWeight,
    CourseManagement,
    RescueRecord,
    Inspection,
    LongDialysis,
    DialysisRecord,
    Scheduling,
    Education,
    signsRecord,
    recoverNotes,
    medicationPlan,
    deviceManagement,
    contagion,
    restatus: 0,
    new_Inspection
  },
  inject: ['reload'],
  data () {
    return {
      show: false,
      show1: false,
      active: 0,
      title: [
        '医嘱',
        '长期透析处方',
        '透析记录',
        '干体重',
        '检验检查',
        '病程记录',
        '抢救记录',
        '排班信息',
        '宣教信息',
        '体征记录',
        '康复笔记',
        '用药管理',
        '设备管理'
      ],

      searchVal: '',
      startTime: moment(new Date()).format('YYYY-MM-DD'),
      endTime: moment(new Date()).format('YYYY-MM-DD'),
      show1: false,
      show2: false,
      currentDate1: new Date(),
      currentDate2: new Date(),
      show3: false,
      actions: [{ name: '编辑' }, { name: '删除' }],
      time: '2016-10-2',
      patientForm: {
        name: '',
        sex: '',
        age: '',
        dialysis_no: '',
        avatar: ''
      },
      doctorAdvice: [],
      yiShow: false,
      meiShow: false,
      bingShow: false,
      aiShow: false,
      doctor_name: '',
      value: '',
      patientName: '',
      cityNameList: [
        {
          id: 101,
          name: '北京市',
          pid: 1,
          code: 0
        },
        {
          id: 10101,
          name: '天津市',
          pid: 10001,
          code: 0
        },
        {
          id: 20101,
          name: '石家庄市',
          pid: 20001,
          code: 130100
        }
      ],
      FirstPin: [
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'J',
        'K',
        'L',
        'M',
        'N',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'W',
        'X',
        'Y',
        'Z'
      ],
      firstNameTwo: {},
      limit: 10,
      page: 1,
      patient: [],
      patient_id: '',
      patient_info: {
        patient_id: this.patient_id
      },
      id: 0,
      name: 0,
      nowdate: 0
    }
  },
  methods: {

    changeActive (index) {
      this.active = index
      this.show1 = false
    },

    // sadas
    showPopup (index) {
      if (index === 1) {
        this.show1 = true
      } else if (index === 2) {
        this.show2 = true
      }
    },
    chooseStartTime (value) {
      this.startTime = moment(value).format('YYYY-MM-DD')
      var patientid = this.$route.query.patientid
      var orgid = this.$route.query.orgid
      this.GetDoctroAdvice(patientid, orgid, this.startTime)
      this.show1 = false
    },
    chooseEndTime (value) {
      this.endTime = moment(value).format('YYYY-MM-DD')
      var patientid = this.$route.query.patientid
      var orgid = this.$route.query.orgid
      this.GetDoctroAdvice(patientid, orgid, this.endTime)
      this.show2 = false
    },
    // 点击标签页
    onClick (index) {
      this.active = index
      // if (index === 1) {
      //   this.show3 = true
      // } else if (index === 2) {
      //   this.show3 = true
      // }
    },
    RecordTime (time) {
      // return uParseTime(time, '{y}-{m}-{d} {h}:{i}:{s}')
      return uParseTime(time, '{y}-{m}-{d}')
    },
    GetDoctroAdvice (patientid, orgid) {
      GetDoctroAdvice(
        patientid,
        orgid,
        this.startTime,
        this.endTime,
        this.searchVal
      ).then(response => {
        if (response.data.state === 1) {
          var doctoradvice = response.data.data.doctoradvice
          this.doctorAdvice = doctoradvice
          var patients = response.data.data.patients
          this.patientForm.name = patients.name
          this.patientForm.age = patients.age
          this.patientForm.avatar = patients.avatar
          var diseases = response.data.data.diseases
          for (let index = 0; index < diseases.length; index++) {
            if (diseases[index].disease_id === 2) {
              this.yiShow = true
            }
            if (diseases[index].disease_id === 3) {
              this.bingShow = true
            }
            if (diseases[index].disease_id === 4) {
              this.aiShow = true
            }

            if (diseases[index].disease_id === 6) {
              this.meiShow = true
            }
          }
          if (patients.gender === 1) {
            this.patientForm.sex = '男'
          }
          if (patients.gender === 2) {
            this.patientForm.sex = '女'
          }
          this.patientForm.dialysis_no = patients.dialysis_no
          var role = response.data.data.role
          this.doctor_name = role.user_name
        }
      })
    },
    watchNum () {
      var patientid = this.$route.query.patientid
      var orgid = this.$route.query.orgid
      toSeacherDoctorAdvice(patientid, orgid, this.searchVal).then(response => {
        if (response.data.state === 1) {
          var doctorAdvice = response.data.data.doctoradvice
          this.doctorAdvice = doctoradvice
        }
      })
    },
    deleteDoctorAdvice (id, index) {
      deleteDoctorAdvice(id, index).then(response => {
        if (response.data.state === 1) {
          var msg = response.data.data.msg
          this.doctorAdvice.splice(index, 1)
        }
      })
    },
    getPatientDetail (patientid) {
      getPatientDetail(patientid).then(response => {
        if (response.data.state === 1) {
          var patientDetail = response.data.data.patientDetail
          var nowdate = response.data.data.nowdate
          this.nowdate = nowdate
          this.id = patientDetail.blood_id
          this.name = patientDetail.name
          this.patientName = patientDetail.name
        }
      })
    },
    // 全部病人
    getBloodDialysisPatientTwo () {
      getBloodDialysisPatientTwo(this.page, this.limit).then(response => {
        var patient = response.data.data.patient
        this.patient = patient
        let cityNameList = []
        for (let p in patient) {
          cityNameList.push(patient[p].name)
        }
        let firstName = {}
        this.FirstPin.forEach(item => {
          firstName[item] = []
          cityNameList.forEach(el => {
            let first = pinyin.getFullChars(el).substring(0, 1)
            if (first == item) {
              firstName[item].push(el)
            }
          })
          this.firstNameTwo = firstName
          // 判断非空
          let newObj = {}
          Object.keys(this.firstNameTwo).map((item, index) => {
            if (this.firstNameTwo[item].length != 0) {
              newObj[item] = this.firstNameTwo[item]
            }
          })
          this.firstNameTwo = newObj
        })
      })
    },
    // 获取血透病人
    getBloodPatient () {
      getBloodPatient().then(response => {
        if (response.data.state === 1) {
          var patient = response.data.data.patient
          console.log('12121response',response.data.data)
          this.patient = patient
          let cityNameList = []
          for (let p in patient) {
            cityNameList.push(patient[p].name)
          }
          let firstName = {}
          this.FirstPin.forEach(item => {
            firstName[item] = []
            cityNameList.forEach(el => {
              let first = pinyin.getFullChars(el).substring(0, 1)
              if (first == item) {
                firstName[item].push(el)
              }
            })
            this.firstNameTwo = firstName
            // 判断非空
            let newObj = {}
            Object.keys(this.firstNameTwo).map((item, index) => {
              if (this.firstNameTwo[item].length != 0) {
                newObj[item] = this.firstNameTwo[item]
              }
            })
            this.firstNameTwo = newObj
          })
        }
      })
    },
    // 获取慢病病人
    getSlowPatient () {
      getSlowPatient().then(response => {
        if (response.data.state === 1) {
          var patient = response.data.data.patient
          this.patient = patient
          let cityNameList = []
          for (let p in patient) {
            cityNameList.push(patient[p].name)
          }
          let firstName = {}
          this.FirstPin.forEach(item => {
            firstName[item] = []
            cityNameList.forEach(el => {
              let first = pinyin.getFullChars(el).substring(0, 1)
              if (first == item) {
                firstName[item].push(el)
              }
            })
            this.firstNameTwo = firstName
            // 判断非空
            let newObj = {}
            Object.keys(this.firstNameTwo).map((item, index) => {
              if (this.firstNameTwo[item].length != 0) {
                newObj[item] = this.firstNameTwo[item]
              }
            })
            this.firstNameTwo = newObj
          })
        }
      })
    },
    // 
    getMemberPatient () {
      getMemberPatient().then(response => {
        if (response.data.state === 1) {
          var patient = response.data.data.patient
          this.patient = patient
          let cityNameList = []
          console.log('response.data.data12112康桥',response.data.data);
          for (let p in patient) {
            cityNameList.push(patient[p].name)
          }
          let firstName = {}
          this.FirstPin.forEach(item => {
            firstName[item] = []
            cityNameList.forEach(el => {
              let first = pinyin.getFullChars(el).substring(0, 1)
              if (first == item) {
                firstName[item].push(el)
              }
            })
            this.firstNameTwo = firstName
            // 判断非空
            let newObj = {}
            Object.keys(this.firstNameTwo).map((item, index) => {
              if (this.firstNameTwo[item].length != 0) {
                newObj[item] = this.firstNameTwo[item]
              }
            })
            this.firstNameTwo = newObj
          })
        }
      })
    },
    // 弹窗搜索
    onSearch (val) {
      ToSearch(val).then(response => {
        if (response.data.state === 1) {
          var search = response.data.data.search
          let cityNameList = []
          for (let p in search) {
            cityNameList.push(search[p].name)
          }
          let firstName = {}
          this.FirstPin.forEach(item => {
            firstName[item] = []
            cityNameList.forEach(el => {
              let first = pinyin.getFullChars(el).substring(0, 1)
              if (first == item) {
                firstName[item].push(el)
              }
            })
            this.firstNameTwo = firstName
            // 判断非空
            let newObj = {}
            Object.keys(this.firstNameTwo).map((item, index) => {
              if (this.firstNameTwo[item].length != 0) {
                newObj[item] = this.firstNameTwo[item]
              }
            })
            this.firstNameTwo = newObj
          })
        }
      })
    },
    onCancel () {},
    // 弹窗选中患者
    chooseCity (val) {
      this.value = val
      console.log('val',val);
      for (let i = 0; i < this.patient.length; i++) {
        if (this.value === this.patient[i].name) {
          var id = this.patient[i].id
          this.show = false
          this.$router.push(
            '/patientdetail?patientid=' + id + '&seacherActive=' + 1 + '&active='+this.active
          )
          this.patientName = this.value
          this.reload()
        }
      }
    },
    toEditPatient () {
      this.$router.push('/editPatient?patientid=' + this.patient_id)
    },
    toReturn () {
      if (this.restatus == 1) {
        
        this.$router.push(
          '/details?patient_id=' +
            this.id +
            '&date=' +
            this.nowdate +
            '&patient_name=' +
            this.name
        )
      } else {
        this.$router.push('/patients')
      }
    }
  },
  created () {
    setRem()
    if (this.$route.query.active) {
      this.active = parseInt(this.$route.query.active)
    }
    var patientid = this.$route.query.patientid
    this.active = this.$route.query.active*1 || this.active
    console.log('ccccccc',this.active);
    this.patient_id = patientid
    this.getPatientDetail(patientid)
    var seacherActive = this.$route.query.seacherActive
    console.log('seacherActive',seacherActive);
    
    if (seacherActive == 0) {
      this.getBloodDialysisPatientTwo()
    }
    if (seacherActive == 1) {
      this.getBloodPatient()
    }
    if (seacherActive == 2) {
      this.getSlowPatient()
    }
    if (seacherActive == 3) {
      this.getMemberPatient()
    }
    var restatus = this.$route.query.restatus
    this.restatus = restatus
  },
  watch: {
    $route (newVal) {
      this.patient_id = newVal.query.patientid
    }
  }
}
</script>

<style lang="scss" scoped>
.page_doctorAdvice {
  overflow: hidden;
  height: 100%;
  overflow-y: auto;
  .doctorAdviceTitle {
    height: 3.125rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0 1.125rem;
    background: #fff;
    font-size: 1rem;
  }
  .patientName {
    display: flex;
    align-items: center;
    font-size: 1rem;
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 2rem;
  }
  .wapBox {
    position: absolute;
    z-index: 2;
    right: 0;
    top: 0;
    width: 2.625rem;
    height: 2.75rem;
    text-align: center;
    background: rgba(255, 255, 255, 1);
    box-shadow: -2px 0px 12px 0px rgba(91, 91, 91, 0.16);
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  .wap {
    font-size: 1.875rem;
    line-height: 44px;
    color: #5b5b5b;
  }
  .titleBox {
    padding: 0 1.25rem 1.25rem;
    display: flex;
    flex-wrap: wrap;
    .titleBoxOne {
      padding: 0.375rem 0.625rem;
      background: #f6f6f6;
      border-radius: 0.375rem;
      margin: 0.625rem 0.375rem;
      font-size: 0.75rem;
    }
  }
}
::-webkit-scrollbar {
  width: 0;
}
</style>
<style lang="scss">
.page_doctorAdvice {
  .van-tabs__wrap {
    width: 90% !important;
  }
  .van-cell__value {
    display: flex;
    align-items: center;
  }
  .van-tabs__line {
    background: #5b98ff;
  }
  .van-tabs__nav {
    > div:nth-of-type(2) {
      flex-basis: 32% !important;
    }
  }
  .doctorAdviceSearch {
    .van-field__body {
      width: 100%;
    }
  }
  .van-popup {
    transform: none;
  }
  .van-tab--active {
    font-size: 1rem;
    color: #000;
    font-weight: bold;
    flex-basis: 26% !important;
  }
}
</style>
