<template>
  <div class="page_editPatients"  v-loading="load"
    element-loading-text="上传中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)">
    <van-sticky>
      <div class="addPatientsTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
        <span class="titleName" @click="showss = true">编辑患者</span>
        <span @click="spread" id="copy">修改</span>
         <!-- <span @click="toJiaoBen" id="copy">脚本</span> -->
      </div>
    </van-sticky>

    <div class="basicTitle">基本信息</div>
    <div class="basicBox">
      <van-field
        :class="readonly ? 'changeColor' : ''"
        v-model="patientForm.name"
        required
        label="姓名"
        :readonly="readonly"
        placeholder="请输入"
      />
      <van-field
        :class="readonly ? 'changeColor' : ''"
        label="性别"
        v-model="patientForm.sex"
        right-icon="arrow"
        placeholder="请选择"
        required
        :readonly="readonly"
        @click="clickSex"
      />
      <van-popup v-model="show" position="bottom" :style="{ height: '40%' }">
        <van-picker :columns="columns" show-toolbar @change="onChange" />
      </van-popup>
      <van-field
        :class="readonly ? 'changeColor' : ''"
        v-model="patientForm.idCard"
        required
        label="身份证号"
        @blur="checkIdCardNo"
        placeholder="请输入"
        :readonly="readonly"
      />
      <van-field
        :class="readonly ? 'changeColor' : ''"
        v-model="patientForm.age"
        required
        label="年龄"
        placeholder="请输入"
        readonly
      />
      <van-field
        :class="readonly ? 'changeColor' : ''"
        v-model="patientForm.birthday"
        required
        label="出生日期"
        :readonly="readonly"
        placeholder="请输入"
      />
      <van-field
        :class="readonly ? 'changeColor' : ''"
        v-model="patientForm.phone"
        type="tel"
        required
        label="手机号"
        placeholder="请输入"
        :readonly="readonly"
      />
       <van-field
        v-model="patientForm.relative_phone"
        type="tel"
        label="家属手机号"
        placeholder="请输入"
      />

      <van-field
          :class="readonly ? 'changeColor' : ''"
          v-model="patientForm.home_address"
          rows="1"
          autosize
          label="家庭地址"
          type="textarea"
          maxlength="50"
          :readonly="readonly"
          placeholder="请输入家庭地址"
        />
      <!-- <van-cell>
     
        <template slot="title">
          <span class="custom-title">患者类型</span>
          <div style="width:60%;margin-left:2rem">
            <van-checkbox-group v-model="result">
              <van-checkbox name="1" shape="square" @click="changeMode"
                >血透患者</van-checkbox
              >
              <van-checkbox name="2" shape="square" @click="changeMode"
                >慢病患者</van-checkbox
              >
              <van-checkbox name="3" shape="square" @click="changeMode"
                >会员患者</van-checkbox
              >
              <van-checkbox name="4" shape="square" @click="changeMode"
                >腹透患者</van-checkbox
              >
              <van-checkbox name="5" shape="square" @click="changeMode"
                >CKD患者</van-checkbox
              >
              <van-checkbox name="6" shape="square" @click="changeMode"
                >其他患者</van-checkbox
              >
            </van-checkbox-group>
          </div>
        </template>
      </van-cell> -->

      <van-cell>
          <template slot="title">
            <span class="custom-title">患者类型</span>
            <div
              style="width:60%;margin-left:1.3rem;display: flex;align-items: center;"
            >
              <van-radio-group v-model="patient_type">
                <van-radio name="1" shape="square">血透患者</van-radio>
                <van-radio name="2" shape="square">慢病患者</van-radio>
                <van-radio name="3" shape="square">会员患者</van-radio>
                <van-radio name="4" shape="square">腹透患者</van-radio>
                <van-radio name="5" shape="square">CKD患者</van-radio>
                <van-radio name="6" shape="square">其他患者</van-radio>
              </van-radio-group>
            </div>
          </template>
        </van-cell>
      <van-cell class="upfileCell">
        <!-- 使用 title 插槽来自定义标题 -->
        <template slot="title">
          <span class="custom-title">头像</span>
          <div class="upfile" v-show="showhead">
            <p class="upfileActive">上传头像</p>
            <span class="upfileBox">
              <img :src="uploadAvatar" alt="">
            </span>
          </div>
          <div class="upfile" v-show="showheadtwo">
            <p>上传头像</p>
            <input ref="image" @change="tirggerFile" type="file" accept="image/*"/>
            <span class="upfileBox">
              <img :src="uploadAvatar" alt="">
            </span>
          </div>
        </template>
      </van-cell>
    </div>
    <div v-show="bloodShow">
      <div class="basicTitle">血透信息</div>
      <div class="xtBox">
        <van-field
          :class="readonly ? 'changeColor' : ''"
          v-model="patientForm.dialysis"
          center
          clearable
          required
          label="透析号"
          :readonly="readonly"
        >
          <van-button
            slot="button"
            size="small"
            type="info"
            @click="generatedialysisno"
            >自动生成</van-button
          >
        </van-field>
        <van-field
          :class="readonly ? 'changeColor' : ''"
          label="患者来源"
          v-model="patientForm.patientsoure"
          right-icon="arrow"
          required
          :readonly="readonly"
          placeholder="请选择"
          @click="clickPatientSoure"
        />
        <van-popup v-model="show3" position="bottom" :style="{ height: '40%' }">
          <van-picker :columns="columns2" show-toolbar @change="onChange1" />
        </van-popup>
        <van-field
          :class="readonly ? 'changeColor' : ''"
          label="留治状态"
          v-model="patientForm.lapseto"
          right-icon="arrow"
          required
          :readonly="readonly"
          placeholder="请选择"
          @click="clickLapseto"
        />
        <van-popup v-model="show4" position="bottom" :style="{ height: '40%' }">
          <van-picker :columns="columns4" show-toolbar @change="onChange2" />
        </van-popup>

       <van-field
          :class="readonly ? 'changeColor' : ''"
          label="转归(死亡)日期"
          v-model="patientForm.death_time"
          right-icon="arrow"
          placeholder="请选择"
          @click="clickDateFive"
          :readonly="readonly"
          v-show="showDeath"
        />
        <van-popup v-model="show20" position="bottom" :style="{ height: '40%' }">
          <van-datetime-picker
            v-model="currentDate"
            type="date"
            :min-date="minDate"
            :max-date="maxDate"
            @cancel="onCancel"
            @confirm="onConfirmTwo"
          />
        </van-popup>

        <van-field
        v-show="showDeath"
        :readonly="readonly"
        :class="readonly ? 'changeColor' : ''"
        v-model="patientForm.out_reason"
        required
        label="转出(死亡)原因"
        placeholder="请输入"
       />

        <van-cell>
          <!-- 使用 title 插槽来自定义标题 -->
          <template slot="title">
            <span class="custom-title">有无传染病</span>
            <div
              style="width:60%;margin-left:1.3rem;display: flex;align-items: center;"
            >
              <van-radio-group v-model="radio" @change="changRadio">
                <van-radio name="1" shape="square">无</van-radio>
                <van-radio name="2" shape="square">有</van-radio>
              </van-radio-group>
            </div>
          </template>
        </van-cell>

        <van-cell v-show="showOne">
          <!-- 使用 title 插槽来自定义标题 -->
          <template slot="title">
            <span class="custom-title">传染病</span>
            <div style="width:60%;margin-left:3rem">
              <van-checkbox-group v-model="resultTwo">
                <van-checkbox name="2" shape="square">乙肝</van-checkbox>
                <van-checkbox name="3" shape="square">丙肝</van-checkbox>
                <van-checkbox name="4" shape="square">艾滋</van-checkbox>
                <van-checkbox name="5" shape="square">肺结核</van-checkbox>
                <van-checkbox name="6" shape="square">梅毒</van-checkbox>
              </van-checkbox-group>
            </div>
          </template>
        </van-cell>

        <van-field
          :class="readonly ? 'changeColor' : ''"
          v-model="patientForm.admissionNumber"
          :readonly="readonly"
          type="text"
          label="住院(门诊)号"
        />
        <van-field
          :class="readonly ? 'changeColor' : ''"
          label="首次透析日期"
          v-model="patientForm.fistDate"
          right-icon="arrow"
          :readonly="readonly"
          placeholder="请选择"
          @click="clickDate"
        />
        <van-popup v-model="show2" position="bottom" :style="{ height: '40%' }">
          <van-datetime-picker
            v-model="currentDate"
            type="date"
            :min-date="minDate"
            :max-date="maxDate"
            @cancel="onCancel"
            @confirm="onConfirm"
          />
        </van-popup>
        <van-field
          :class="readonly ? 'changeColor' : ''"
          v-model="patientForm.diagnose"
          rows="1"
          autosize
          label="诊断"
          type="textarea"
          maxlength="50"
          :readonly="readonly"
          placeholder="请输入主要诊断信息"
        />

        <van-field
          :class="readonly ? 'changeColor' : ''"
          v-model="patientForm.treatment_plan"
          rows="1"
          autosize
          label="诊疗计划"
          type="textarea"
          maxlength="50"
          :readonly="readonly"
          placeholder="请输入诊疗计划"
        />

       

      <van-field
          :class="readonly ? 'changeColor' : ''"
          v-model="patientForm.remark"
          rows="1"
          autosize
          label="备注"
          type="textarea"
          maxlength="50"
          :readonly="readonly"
          placeholder="请输入备注"
        />
      </div>
    </div>
    <div v-show="slowShow">
      <div class="basicTitle">慢病信息</div>
      <div class="xtBox">
        <van-field
          :class="readonly ? 'changeColor' : ''"
          v-model="patientForm.requipmentId"
          label="设备ID"
          placeholder="请输入"
          :readonly="readonly"
        />

        <van-cell>
          <!-- 使用 title 插槽来自定义标题 -->
          <template slot="title">
            <span class="custom-title2">传染病</span>
            <div style="width:60%;margin-left:3rem">
              <van-checkbox-group v-model="resultThree">
                <van-checkbox name="1" shape="square">乙肝</van-checkbox>
                <van-checkbox name="2" shape="square">丙肝</van-checkbox>
                <van-checkbox name="3" shape="square">艾滋</van-checkbox>
                <van-checkbox name="4" shape="square">肺结核</van-checkbox>
                <van-checkbox name="5" shape="square">梅毒</van-checkbox>
              </van-checkbox-group>
            </div>
          </template>
        </van-cell>

        <van-cell>
          <!-- 使用 title 插槽来自定义标题 -->
          <template slot="title">
            <span class="custom-title2">慢性病</span>
            <div style="width:60%;margin-left:3rem">
              <van-checkbox-group v-model="resultFour">
                <van-checkbox name="1" shape="square">血透</van-checkbox>
                <van-checkbox name="2" shape="square">腹透</van-checkbox>
                <van-checkbox name="3" shape="square">移植</van-checkbox>
                <van-checkbox name="4" shape="square">高血压</van-checkbox>
                <van-checkbox name="5" shape="square">糖尿病</van-checkbox>
                <van-checkbox name="6" shape="square">慢性肾病</van-checkbox>
                <van-checkbox name="7" shape="square">其他</van-checkbox>
              </van-checkbox-group>
            </div>
          </template>
        </van-cell>
      </div>
    </div>
    <div v-show="memberShow">
      <div class="basicTitle">会员资料</div>
      <div class="xtBox">
        <van-field
          :class="readonly ? 'changeColor' : ''"
          label="首次透析日期"
          v-model="patientForm.memberFistDate"
          right-icon="arrow"
          :readonly="readonly"
          placeholder="请选择"
          @click="clickDateTwo"
        />
        <van-popup
          v-model="show12"
          position="bottom"
          :style="{ height: '40%' }"
        >
          <van-datetime-picker
            v-model="currentDate"
            type="date"
            :min-date="minDate"
            :max-date="maxDate"
            @cancel="onCancel"
            @confirm="onConfirmOne"
          />
        </van-popup>
        <van-field
          :class="readonly ? 'changeColor' : ''"
          label="病种"
          v-model="patientForm.patient_type"
          right-icon="arrow"
          :readonly="readonly"
          placeholder="请选择"
          @click="clickPatientType"
        />
        <van-popup
          v-model="show14"
          position="bottom"
          :style="{ height: '40%' }"
        >
          <van-picker
            :columns="patientType"
            show-toolbar
            @change="onChangeSix"
            @cancel="cancelSeven"
          />
        </van-popup>
        <van-field
          :class="readonly ? 'changeColor' : ''"
          label="治疗方式"
          v-model="patientForm.treatmentMethod"
          right-icon="arrow"
          :readonly="readonly"
          placeholder="请选择"
          @click="clickTreatMethod"
        />
        <van-popup
          v-model="show13"
          position="bottom"
          :style="{ height: '40%' }"
        >
          <van-picker
            :columns="treatmentMethod"
            show-toolbar
            @change="onChangeSeven"
            @cancel="cancelEight"
          />
        </van-popup>
      </div>
    </div>
    <div class="moreInfo">如需要完善更多病人信息，请在电脑端完善</div>
  </div>
</template>

<script>
import VueCropper from 'vue-cropperjs'
import { Toast } from 'vant'
import {
  GenerateDialysisNo,
  GetIllnessList,
  savePatient,
  GetPatientInfo,
  getBloodPatientInfo,
  getSlowPatientInfo,
  getMemberPatientInfo,
  GetPatientDetail,
  updatedPatient,
  UpdateImage,
  toGetPatient
} from '@/api/patient/patient'
import {
  isCardNo,
  checkMobile,
  getFileExtension,
  uParseTime,
  uParseTimeOne,
  uParseTimeThree
} from '@/utils/tools'
import { getToken } from '@/api/qiniu'
import { setRem, setHeight } from '@/libs/functionRem'
const moment = require('moment')
export default {
  components: {
    VueCropper
  },
  data () {
    var checkMobileRule = (rule, value, callback) => {
      if (!checkMobile(value)) {
        callback(new Error('请填写正确的手机号'))
      } else {
        callback()
      }
    }

    return {
      load: false,
      showhead: true,
      showheadtwo: false,
      show: false,
      show1: false,
      show2: false,
      show3: false,
      show4: false,
      show20: false,
      name: '',
      columns: ['男', '女'],
      columns1: ['1', '2', '3'],
      columns2: ['门诊', '住院'],
      columns4: ['留治', '转出', '死亡'],
      sex: '',
      idCard: '',
      birthday: '',
      tel: '',
      result: [],
      dialysisNumber: '',
      source: '',
      source2: '',
      source3: '',
      show10: false,
      show9: false,
      show8: false,
      show7: false,
      show6: false,
      show11: false,
      show12: false,
      show13: false,
      show14: false,
      hospitalNum: '',
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      time: '',
      message: '',
      patient_id: '',
      resultTwo: [],
      resultThree: [],
      resultFour: [],
      patientForm: {
        id: '',
        name: '',
        sex: '',
        idCard: '',
        birthday: '',
        phone: '',
        dialysis: '',
        patient_type: '',
        admissionNumber: '',
        patientsoure: '',
        lapseto: '',
        contagions: [],
        fistDate: '',
        diagnose: '',
        requipmentId: '',
        slowcontagions: [],
        slowDisease: '',
        memberIllnessTime: '',
        memberlapseto: '',
        TreatmentMethod: '',
        bloodPatient: 0,
        slowPatient: 0,
        memberPatient: 0,
        avatar: '',
        memberFistDate: '',
        blood_patients: '',
        slow_patients: '',
        member_patients: '',
        death_time: '',
        out_reason: '',
        relative_phone: '',
        remark: '',
        treatment_plan: '',
        home_address:"",
        home_telephone:"",
        age:"",
      },
      bloodShow: true,
      slowShow: false,
      memberShow: false,
      patientType: [],
      treatmentMethod: [
        '西医治疗',
        '中医治疗',
        '血液透析',
        '腹膜透析',
        '肾移植'
      ],
      illnesslist: [],
      radio: '1',
      showOne: false,
      readonly: true,

      //
      imgUrl: '',
      uptoken: '',
      cropper: '',
      centerDialogVisible: false,
      loading: false,
      uploadAvatar: '',
      showDeath: false,
      patient_type:"",

    }
  },
  methods: {
    checkIdCardNoOne (id_card_no) {
      if (!isCardNo(id_card_no)) {
        this.$toast('身份证号码信息有误!')
        this.patientForm.birthday = ''
        return false
      }
      var thisLen = id_card_no.length
      var birth = ''
      if (thisLen === 15) {
        birth = '19' + id_card_no.substr(6, 6)
      } else {
        birth = id_card_no.substr(6, 8)
      }
     var birthday =
        birth.substr(0, 4) +
        '-' +
        birth.substr(4, 2) +
        '-' +
        birth.substr(6, 2)

     return birthday
    },
    onChange (picker, value, index) {
      this.patientForm.sex = value
      this.show = false
    },
    onChange1 (picker, value, index) {
      this.patientForm.patientsoure = value
      this.show3 = false
    },
    onChange2 (picker, value, index) {
      if (value == '死亡' || value=='转出') {
        this.showDeath = true
      } else {
        this.showDeath = false
      }
      this.patientForm.lapseto = value
      this.show4 = false
    },
    onChange3 (picker, value, index) {
      this.source3 = value
      this.show3 = false
    },
    onChange4 (picker, value, index) {
      this.source = value
      this.show1 = false
    },
    onCancel () {
      this.show2 = false
    },
    onConfirm (value) {
      let year = value.getFullYear()
      let month = value.getMonth() + 1
      let day = value.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.patientForm.fistDate = `${year}-${month}-${day}`

      this.show2 = false
    },
    onConfirmOne (value) {
      let year = value.getFullYear()
      let month = value.getMonth() + 1
      let day = value.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.patientForm.memberFistDate = `${year}-${month}-${day}`
      this.show12 = false
    },
    onConfirmTwo (value) {
      let year = value.getFullYear()
      let month = value.getMonth() + 1
      let day = value.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.patientForm.death_time = `${year}-${month}-${day}`
      this.show20 = false
    },
    cancelSeven () {
      this.show14 = false
    },
    cancelEight () {
      this.show13 = false
    },
    toReturn () {
      this.$router.push('/patientdetail?patientid=' + this.patient_id)
    },
    changeMode (val) {
      if (val == 1) {
        getBloodPatientInfo(this.patientForm.phone).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
          } else {
            this.$toast('血透患者已存在')
            return false
          }
        })
      }

      if (val == 2) {
        getSlowPatientInfo(this.patientForm.phone).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
          } else {
            this.$toast('慢病患者已存在')
            return false
          }
        })
      }

      if (val == 3) {
        getMemberPatientInfo(this.patientForm.phone).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
          } else {
            this.$toast('会员已存在')
            return false
          }
        })
      }

      if (this.result.indexOf('1') != -1) {
        this.bloodShow = true
        this.slowShow = false
        this.memberShow = false
      }
      if (this.result.indexOf('2') != -1) {
        this.slowShow = true
        this.bloodShow = false
        this.memberShow = false
      }
      if (this.result.indexOf('3') != -1) {
        this.memberShow = true
        this.bloodShow = false
        this.slowShow = false
      }

      if (this.result.indexOf('1') != -1 && this.result.indexOf('2') != -1) {
        this.bloodShow = true
        this.slowShow = true
        this.memberShow = false
      }
      if (
        this.result.indexOf('1') != -1 &&
        this.result.indexOf('2') != -1 &&
        this.result.indexOf('3') != -1
      ) {
        this.bloodShow = true
        this.slowShow = true
        this.memberShow = true
      }

      if (this.result.indexOf('1') != -1 && this.result.indexOf('3') != -1) {
        this.bloodShow = false
        this.slowShow = true
        this.memberShow = true
      }
      if (this.result.indexOf('3') != -1 && this.result.indexOf('1') != -1) {
        this.bloodShow = true
        this.slowShow = false
        this.memberShow = true
      }
      if (this.result.indexOf('2') != -1 && this.result.indexOf('3') != -1) {
        this.bloodShow = false
        this.slowShow = true
        this.memberShow = true
      }

      if (
        this.result.indexOf('2') != -1 &&
        this.result.indexOf('3') != -1 &&
        this.result.indexOf('1') != -1
      ) {
        this.bloodShow = true
        this.slowShow = true
        this.memberShow = true
      }

      if (this.result.length == 0) {
        this.bloodShow = false
        this.slowShow = false
        this.memberShow = false
      }
    },
    generatedialysisno () {
      GenerateDialysisNo().then(response => {
        // eslint-disable-next-line eqeqeq
        if (response.data.state == 1) {
          var no = response.data.data.no + ''
          this.patientForm.dialysis = no
        }
      })
    },
    onChangeSix (picker, value, index) {
      this.patientForm.patient_type = value
      this.show14 = false
    },
    onChangeSeven (picker, value, index) {
      this.patientForm.treatmentMethod = value
      this.show13 = false
    },
    getAge (UUserCard) {
      if (UUserCard != null && UUserCard != '') {
        var myDate = new Date()
        var month = myDate.getMonth() + 1
        var day = myDate.getDate()
        var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1
        if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
          age++
        }
        return age
      }
    },
    GetPatientDetail (patientid) {
      GetPatientDetail(patientid).then(response => {
        if (response.data.state === 1) {
          var patientdetail = response.data.data.patientDetail
          // console.log('patientdetail', patientdetail)
         
          var diseases = response.data.data.diseases
          var slowDisease = response.data.data.slowDiseases
          for (let i = 0; i < diseases.length; i++) {
            this.resultTwo.push(String(diseases[i]))
          }
          for (let i = 0; i < slowDisease.length; i++) {
            this.resultThree.push(String(slowDisease[i].disease_id))
          }
          var cronicdisease = response.data.data.cronicDiseases
          for (let i = 0; i < cronicdisease.length; i++) {
            this.resultFour.push(String(cronicdisease[i].disease_id))
          }
          if (patientdetail.gender === 1) {
            patientdetail.gender = '男'
          }
          if (patientdetail.gender === 2) {
            patientdetail.gender = '女'
          }
          if (patientdetail.source === 1) {
            patientdetail.source = '门诊'
          }
          if (patientdetail.source === 2) {
            patientdetail.source = '住院'
          }
          if (patientdetail.lapseto === 1) {
            patientdetail.lapseto = '留治'
          }
          if (patientdetail.lapseto === 2) {
            patientdetail.lapseto = '转出'
          }

          if (patientdetail.lapseto === 3) {
            patientdetail.lapseto = '死亡'
            this.showDeath = true
          }
          this.patientForm.name = patientdetail.name
          this.patientForm.sex = patientdetail.gender
          this.patientForm.idCard = patientdetail.id_card_no
          this.patientForm.birthday = this.checkIdCardNoOne(patientdetail.id_card_no)

          

          this.patient_type = patientdetail.patient_type.toString()
         
         
          this.patientForm.phone = patientdetail.phone
          this.patientForm.relative_phone = patientdetail.relative_phone


          this.patientForm.age = this.getAge(patientdetail.id_card_no) 
          console.log("hahhah",this.patientForm.relative_phone)
          if(this.patientForm.relative_phone == ""){
            console.log("进啦为33232",patientdetail.home_telephone)
             this.patientForm.relative_phone = patientdetail.home_telephone
          } 
          this.patientForm.home_address = patientdetail.home_address

          if (patientdetail.blood_patients == 1) {
            this.result.push(String(1))
          }
          if (patientdetail.slow_patients == 1) {
            this.result.push(String(2))
          }
          if (patientdetail.member_patients == 1) {
            this.result.push(String(3))
          }
          this.patientForm.id = patientdetail.id
          this.patientForm.dialysis = patientdetail.dialysis_no
          this.patientForm.patientsoure = patientdetail.source
          this.patientForm.lapseto = patientdetail.lapseto
          this.patientForm.admissionNumber = patientdetail.admission_number
          this.patientForm.requipmentId = patientdetail.equitment_id
          this.patientForm.blood_patients = patientdetail.blood_patients
          this.patientForm.member_patients = patientdetail.member_patients
          this.patientForm.slow_patients = patientdetail.slow_patients
          this.uploadAvatar = patientdetail.avatar
          this.patientForm.remark = patientdetail.sch_remark

          this.patientForm.memberFistDate = uParseTimeOne(patientdetail.member_fistdate,'{y}-{m}-{d}')

          console.log('首次透析日期', patientdetail.first_dialysis_date)

          if (patientdetail.first_dialysis_date < 0) {
            this.patientForm.fistDate = ''
          }
          if (patientdetail.first_dialysis_date > 0) {
            this.patientForm.fistDate = uParseTimeOne(patientdetail.first_dialysis_date,'{y}-{m}-{d}')
          }

          this.patientForm.diagnose = patientdetail.diagnose
          if (patientdetail.member_treatement === 1) {
            this.patientForm.treatmentMethod = '西医治疗'
          }
          if (patientdetail.member_treatement === 2) {
            this.patientForm.treatmentMethod = '中医治疗'
          }
          if (patientdetail.member_treatement === 3) {
            this.patientForm.treatmentMethod = '血液透析'
          }
          if (patientdetail.member_treatement === 4) {
            this.patientForm.treatmentMethod = '腹膜透析'
          }
          if (patientdetail.member_treatement === 5) {
            this.patientForm.treatmentMethod = '肾移植'
          }

          for (let i = 0; i < this.illnesslist.length; i++) {
            if (patientdetail.member_patienttype === this.illnesslist[i].id) {
              this.patientForm.patient_type = this.illnesslist[i].illness_name
            }
          }

          if (patientdetail.member_patients === 1) {
            this.bloodShow = false
            this.slowShow = false
            this.memberShow = true
          }
          if (patientdetail.slow_patients === 1) {
            this.bloodShow = false
            this.memberShow = false
            this.slowShow = true
          }
          if (
            patientdetail.blood_patients === 1 &&
            patientdetail.slow_patients === 1
          ) {
            this.bloodShow = true
            this.slowShow = true
            this.memberShow = false
          }
          if (
            patientdetail.blood_patients === 1 &&
            patientdetail.member_patients === 1
          ) {
            this.bloodShow = true
            this.memberShow = true
            this.slowShow = false
          }
          if (
            patientdetail.member_patients === 1 &&
            patientdetail.slow_patients === 1
          ) {
            this.memberShow = true
            this.slowShow = true
            this.bloodShow = false
          }
          if (
            patientdetail.blood_patients === 1 &&
            patientdetail.slow_patients === 1 &&
            patientdetail.member_patients === 1
          ) {
            this.bloodShow = 1
            this.slowShow = 1
            this.memberShow = 1
          }
          if (patientdetail.is_infectious === 1) {
            this.showOne = false
            this.radio = '1'
          }
          if (patientdetail.is_infectious === 2) {
            this.showOne = true
            this.radio = '2'
          }

          this.patientForm.death_time = uParseTimeOne(patientdetail.death_time, '{y}-{m}-{d}')
          this.patientForm.out_reason = patientdetail.out_reason
          this.patientForm.treatment_plan = patientdetail.treatment_plan
         
          
        }
      })
    },
    GetIllnessList () {
      GetIllnessList().then(response => {
        if (response.data.state === 1) {
          var illnesslist = response.data.data.illnesslist
          // console.log('illnesslist', illnesslist)
          this.illnesslist = illnesslist
          var arr = []
          for (let i = 0; i < illnesslist.length; i++) {
            arr.push(illnesslist[i].illness_name)
          }
          this.patientType = arr
        }
      })
    },
    checkIdCardNo () {
      if (!isCardNo(this.patientForm.idCard)) {
        this.$toast('身份证号码信息有误!')
        this.patientForm.birthday = ''
        return false
      }
      var thisLen = this.patientForm.idCard.length
      var birth = ''
      if (thisLen === 15) {
        birth = '19' + this.patientForm.idCard.substr(6, 6)
      } else {
        birth = this.patientForm.idCard.substr(6, 8)
      }
      this.patientForm.birthday =
        birth.substr(0, 4) +
        '-' +
        birth.substr(4, 2) +
        '-' +
        birth.substr(6, 2)
    },
    spread () {
      var spancontent = document.getElementById('copy').innerHTML
      if (spancontent == '修改') {
        this.readonly = false
        document.getElementById('copy').innerHTML = '保存'
        this.showhead = false
        this.showheadtwo = true
      }
      if (spancontent == '保存') {
        this.updatedPatient()
      }
    },
    clickSex () {
      if (this.readonly == true) {
        this.show = false
      }
      if (this.readonly == false) {
        this.show = true
      }
    },
    clickPatientSoure () {
      if (this.readonly == true) {
        this.show3 = false
      }
      if (this.readonly == false) {
        this.show3 = true
      }
    },
    clickLapseto () {
      if (this.readonly == true) {
        this.show4 = false
      }
      if (this.readonly == false) {
        this.show4 = true
      }
    },

    clickDate () {
      if (this.readonly == true) {
        this.show2 = false
      }
      if (this.readonly == false) {
        this.show2 = true
      }
    },
    clickDateTwo () {
      if (this.readonly == true) {
        this.show12 = false
      }
      if (this.readonly == false) {
        this.show12 = true
      }
    },
    clickDateFive () {
      if (this.readonly == true) {
        this.show20 = false
      }
      if (this.readonly == false) {
        this.show20 = true
      }
    },
    clickPatientType () {
      if (this.readonly == true) {
        this.show14 = false
      }
      if (this.readonly == false) {
        this.show14 = true
      }
    },
    clickTreatMethod () {
      if (this.readonly == true) {
        this.show13 = false
      }
      if (this.readonly == false) {
        this.show13 = true
      }
    },
    updatedPatient () {
      if (this.patientForm.name === '') {
        this.$toast('姓名不能为空')
        return false
      }
      if (this.patientForm.sex === '') {
        this.$toast(' 性别不能为空')
        return false
      }
      if (this.patientForm.idCard === '') {
        this.$toast('身份证不能为空')
        return false
      }
      if (this.patientForm.birthday === '') {
        this.$toast('出生日期不能为空')
        return false
      }
      if (this.patientForm.phone === '') {
        this.$toast('手机号码不能为空')
        return false
      }
      if (this.result.length === 0) {
        this.$toast('患者类型不能为空')
        return false
      }
      if (this.patientForm.dialysis === '' && this.result.indexOf('1') != -1) {
        this.$toast('透析号不能为空')
        return false
      }
      if (
        this.patientForm.patientsoure === '' &&
        this.result.indexOf('1') != -1
      ) {
        this.$toast('患者来源不能为空')
        return false
      }

      if (this.patientForm.lapseto === '' && this.result.indexOf('1') != -1) {
        this.$toast('留置状态不能为空')
        return false
      }

      if (this.patientForm.lapseto === '') {
        this.patientForm.lapseto = 0
      }

      if (this.patientForm.patientsoure == '门诊') {
        this.patientForm.patientsoure = 1
      }
      if (this.patientForm.patientsoure == '住院') {
        this.patientForm.patientsoure = 2
      }

      if (this.patientForm.lapseto == '留治') {
        this.patientForm.lapseto = 1
      }

      if (this.patientForm.lapseto == '转出') {
        this.patientForm.lapseto = 2
      }

      if (this.patientForm.lapseto == '死亡') {
        this.patientForm.lapseto = 3
      }

      if (this.resultTwo.length === 0) {
        this.patientForm.is_infectious = 1
      }

      if (this.resultTwo.length > 0) {
        this.patientForm.is_infectious = 2
      }

      if (this.patientForm.treatmentMethod === undefined) {
        this.patientForm.treatmentMethod = 0
      }

      if (this.patientForm.patient_type === '尿毒症') {
        this.patientForm.patient_type = 21
      }
      if (this.patientForm.patient_type === '慢性肾衰竭') {
        this.patientForm.patient_type = 22
      }
      if (this.patientForm.patient_type === '糖尿病肾病') {
        this.patientForm.patient_type = 23
      }

      if (this.patientForm.patient_type === '高血压肾病') {
        this.patientForm.patient_type = 24
      }

      if (this.patientForm.patient_type === '肾病综合征') {
        this.patientForm.patient_type = 25
      }

      if (this.patientForm.patient_type === 'IGA肾病') {
        this.patientForm.patient_type = 26
      }

      if (this.patientForm.patient_type === '慢性肾炎') {
        this.patientForm.patient_type = 27
      }

      if (this.patientForm.patient_type === '紫癜性肾炎') {
        this.patientForm.patient_type = 28
      }

      if (this.patientForm.patient_type === '狼疮性肾炎') {
        this.patientForm.patient_type = 29
      }
      if (this.patientForm.patient_type === '小儿肾病') {
        this.patientForm.patient_type = 30
      }
      if (this.patientForm.patient_type === '肾结石') {
        this.patientForm.patient_type = 31
      }
      if (this.patientForm.patient_type === '其他') {
        this.patientForm.patient_type = 32
      }

      if (this.patientForm.treatmentMethod === '西医治疗') {
        this.patientForm.treatmentMethod = 1
      }
      if (this.patientForm.treatmentMethod === '中医治疗') {
        this.patientForm.treatmentMethod = 2
      }
      if (this.patientForm.treatmentMethod === '血液透析') {
        this.patientForm.treatmentMethod = 3
      }
      if (this.patientForm.treatmentMethod === '腹膜透析') {
        this.patientForm.treatmentMethod = 4
      }
      if (this.patientForm.treatmentMethod === '肾移植') {
        this.patientForm.treatmentMethod = 5
      }

      if (this.patientForm.patient_type === '') {
        this.patientForm.patient_type = 0
      }

      for (let i = 0; i < this.result.length; i++) {
        if (this.result[i] == 1) {
          this.patientForm.bloodPatient = 1
        }
        if (this.result[i] == 2) {
          this.patientForm.slowPatient = 1
        }
        if (this.result[i] == 3) {
          this.patientForm.memberPatient = 1
        }
      }

      if (!checkMobile(this.patientForm.phone)) {
        this.$toast('请填写正确的手机号')
        return false
      }

      if (this.patientForm.sex !== '') {
        if (this.patientForm.sex === '男') {
          this.patientForm.sex = 1
        }
        if (this.patientForm.sex === '女') {
          this.patientForm.sex = 2
        }
      }
      if (this.patientForm.patientsoure === '') {
        this.patientForm.patientsoure = 0
      }
      if (this.patientForm.patientsoure !== '') {
        if (this.patientForm.patientsoure === '门诊') {
          this.patientForm.patientsoure = 1
        }
        if (this.patientForm.patientsoure === '住院') {
          this.patientForm.patientsoure = 2
        }
      }
      if(this.patientForm.patientsoure >2){
         this.patientForm.patientsoure = parseInt(this.patientForm.patientsoure)
      }
      if (this.patientForm.lapseto !== '') {
        if (this.patientForm.lapseto === '留治') {
          this.patientForm.lapseto = 1
        }
        if (this.patientForm.lapseto === '转出') {
          this.patientForm.lapseto = 2
        }

        if (this.patientForm.lapseto === '死亡') {
          this.patientForm.lapseto = 3
        }
      }
      if (this.uploadAvatar.indexOf('?imageView2/2/w/500/h/500/q/90') != -1) {
        this.patientForm.avatar = this.uploadAvatar
      } else {
        this.patientForm.avatar = this.uploadAvatar + '?imageView2/2/w/500/h/500/q/90'
      }

      console.log('头像', this.uploadAvatar)
      for (let i = 0; i < this.illnesslist.length; i++) {
        if (
          this.illnesslist[i].illness_name === this.patientForm.patient_type
        ) {
          this.patientForm.patient_type = this.illnesslist[i].id
        }
      }

      if(this.patientForm.lapseto ==2 || this.patientForm.lapseto == 3){
         if(this.patientForm.death_time == ""){
          this.$toast('请填写正确的转出或死亡日期!,否则可能会清空排班!')
          return false
         }
      }
    

      const params = {
        id: this.patientForm.id,
        name: this.patientForm.name,
        sex: parseInt(this.patientForm.sex),
        idCard: this.patientForm.idCard,
        birthday: this.patientForm.birthday,
        phone: this.patientForm.phone,
        result: this.result,
        dialysis: this.patientForm.dialysis,
        patientsoure: this.patientForm.patientsoure,
        lapseto: this.patientForm.lapseto,
        resultTwo: this.resultTwo,
        admissionNumber: this.patientForm.admissionNumber,
        fistDate: this.patientForm.fistDate,
        diagnose: this.patientForm.diagnose,
        bloodPatient: this.patientForm.bloodPatient,
        slowPatient: this.patientForm.slowPatient,
        memberPatient: this.patientForm.memberPatient,
        avatar: this.patientForm.avatar,
        requipmentId: this.patientForm.requipmentId, // 慢病信息
        resultThree: this.resultThree,
        resultFour: this.resultFour,
        patientType: this.patientForm.patient_type,
        memberFistDate: this.patientForm.memberFistDate,
        treatmentMethod: this.patientForm.treatmentMethod,
        is_infectious: this.patientForm.is_infectious,
        death_time: this.patientForm.death_time,
        out_reason: this.patientForm.out_reason,
        relative_phone: this.patientForm.relative_phone,
        remark: this.patientForm.remark,
        treatment_plan: this.patientForm.treatment_plan,
        patient_type:parseInt(this.patient_type),
      }

      console.log("params-----------",params)
      updatedPatient(params).then(response => {
        if (response.data.state === 1) {
          var msg = response.data.data.msg
          if (msg === '身份证号码已存在') {
            this.$toast('身份证号码已存在')
            if (this.patientForm.sex === 1) {
              this.patientForm.sex = '男'
            }
            if (this.patientForm.sex === 2) {
              this.patientForm.sex = '女'
            }
            if (this.patientForm.patientsoure === 1) {
              this.patientForm.patientsoure = '门诊'
            }
            if (this.patientForm.patientsoure === 2) {
              this.patientForm.patientsoure = '住院'
            }
            if (this.patientForm.lapseto === 1) {
              this.patientForm.lapseto = '留治'
            }
            if (this.patientForm.lapseto === 2) {
              this.patientForm.lapseto = '转出'
            }

            if (this.patientForm.lapseto === 3) {
              this.patientForm.lapseto = '死亡'
            }

            return false
          }
          if (msg == '手机号码已存在') {
            this.$toast('手机号码已存在')
            if (this.patientForm.sex === 1) {
              this.patientForm.sex = '男'
            }
            if (this.patientForm.sex === 2) {
              this.patientForm.sex = '女'
            }
            if (this.patientForm.patientsoure === 1) {
              this.patientForm.patientsoure = '门诊'
            }
            if (this.patientForm.patientsoure === 2) {
              this.patientForm.patientsoure = '住院'
            }
            if (this.patientForm.lapseto === 1) {
              this.patientForm.lapseto = '留治'
            }
            if (this.patientForm.lapseto === 2) {
              this.patientForm.lapseto = '转出'
            }
            if (this.patientForm.lapseto === 3) {
              this.patientForm.lapseto = '死亡'
            }
            return false
          }
          if (msg === '透析号码已存在') {
            this.$toast('透析号码已存在')
            if (this.patientForm.sex === 1) {
              this.patientForm.sex = '男'
            }
            if (this.patientForm.sex === 2) {
              this.patientForm.sex = '女'
            }
            if (this.patientForm.patientsoure === 1) {
              this.patientForm.patientsoure = '门诊'
            }
            if (this.patientForm.patientsoure === 2) {
              this.patientForm.patientsoure = '住院'
            }
            if (this.patientForm.lapseto === 1) {
              this.patientForm.lapseto = '留治'
            }
            if (this.patientForm.lapseto === 2) {
              this.patientForm.lapseto = '转出'
            }

            if (this.patientForm.lapseto === 3) {
              this.patientForm.lapseto = '死亡'
            }
            return false
          }

          if (msg === undefined) {
            this.$toast('修改成功')
            var patients = response.data.data.patients
            this.$router.push('/patientdetail?patientid=' + patients.id)
          }
        }
      })
    },
    changRadio () {
      if (this.radio === '1') {
        this.showOne = false
      }
      if (this.radio === '2') {
        this.showOne = true
      }
    },

    //
    tirggerFile (e) {
      this.load = true
      this.ImageToBase64(e.target.files[0])
      // this.uploadImage()
    },
    cropImage (event) {
      var dataurl = this.$refs.cropper.getCroppedCanvas()
      var rectImage = dataurl.toDataURL('image/png')
      this.uploadImgUrl = rectImage
    },
    getToken () {
      getToken().then(response => {
        if (response.data.state == 1) {
          this.uptoken = response.data.data.uptoken
        }
      })
    },
   
    uploadImage () {
      // var dataurl = this.$refs.cropper.getCroppedCanvas()
      // var rectImage = this.imgUrl.toDataURL('image/png')
      this.uploadImgUrl = this.imgUrl

      if (typeof this.uploadImgUrl === 'undefined') {
        Toast.fail('获取截图资源失败')
        return false
      }
      if (this.uptoken == '') {
        Toast.fail('七牛上传token无效，无法上传')
        return false
      }

      this.loading = true
      var _this = this
      var pic = this.uploadImgUrl.split(',')[1]
      var url = 'https://upload.qiniup.com/putb64/-1'
      var xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
          var returnData = JSON.parse(xhr.responseText)
          const hash = returnData.hash
          _this.uploadAvatar = 'https://images.shengws.com/' + hash + '?imageView2/2/w/500/h/500/q/90'
          // _this.centerDialogVisible = false
          _this.load = false
          Toast.success('成功')
        }
      }
      xhr.open('POST', url, true)
      xhr.setRequestHeader('Content-Type', 'application/octet-stream')
      xhr.setRequestHeader('Authorization', 'UpToken ' + _this.uptoken)
      xhr.send(pic)
    },

    ImageToBase64 (files) {
      // let files = this.$refs.image.files[0];
      var reader = new FileReader()
      reader.readAsDataURL(files)
      reader.onload = () => {
        // console.log('file 转 base64结果：' + reader.result)
        this.imgUrl = reader.result
        this.uploadImage()

      }
      reader.onerror = function (error) {
      }
    },
    toJiaoBen(){
      toGetPatient().then(response=>{
        if(response.data.state == 1){
         var msg = response.data.data.msg
         this.$toast('更新成功')
        }
      })
    }

  },
  created () {
    setRem()
    setHeight()
    this.getToken()
    var patientid = this.$route.query.patientid
    this.patient_id = patientid
    this.GetPatientDetail(patientid)
    this.GetIllnessList()
  }
}
</script>

<style lang="scss" scoped>
.page_editPatients {
  overflow-y: auto;
  height: 100%;
  background: #fafafa;
  .addPatientsTitle {
    height: 3.125rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0 1.125rem;
    background: #fff;
    span {
      font-size: 1rem;
    }
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 1.25rem;
  }
  .titleName {
    font-size: 1.125rem !important;
    font-weight: 600;
  }
  .basicTitle {
    font-size: 0.75rem;
    color: #989898;
    padding: 0 1rem;
    height: 1.875rem;
    line-height: 1.875rem;
  }
  .basicBox {
    .van-cell__value {
      display: flex;
    }
    .van-field__body {
      width: 100%;
    }
  }
  .custom-title::before {
    position: absolute;
    left: 8px;
    color: #ee0a24;
    font-size: 14px;
    content: "*";
  }
  .van-cell__title {
    display: flex;
  }
  .van-checkbox-group {
    display: flex;
    flex-wrap: wrap;
  }
  .van-checkbox {
    width: 48%;
    margin-bottom: 0.625rem;
  }
  .van-radio-group {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
  }
  .van-radio {
    width: 48%;
  }
  .autoGeneration {
    width: 5rem;
    height: 1.875rem;
    line-height: 1.875rem;
    background: #338afb;
    border-radius: 0.9375rem;
    text-align: center;
  }
  .van-button--info {
    border-radius: 0.9375rem;
  }
  .van-button--small {
    min-width: 3.75rem !important;
    height: 1.875rem;
    padding: 0 0.5rem;
    font-size: 0.75rem !important;
    line-height: 1.75rem;
  }
  .moreInfo {
    text-align: center;
    font-size: 0.75rem;
    color: #338afb;
    margin: 0.625rem 0 4.5rem;
  }
  .van-cell {
    align-items: center;
  }
  .turnOut {
    height: 2.8125rem;
    line-height: 2.8125rem;
    text-align: center;
    background: #fff;
    margin-top: 0.75rem;
  }
  .upfile{
    position: relative;
  }
  .upfileActive{
    background: #989898 !important;
  }
  .upfile p{
    display: block;
    text-align: center;
    font-size: 1rem;
    position: absolute;
    top: 0;
    left: 3.25rem;
    width: 5rem;
    height: 2.5rem;
    line-height: 2.5rem;
    background: #0084ff;
    border-radius: .625rem;
    cursor: pointer;
    color: #fff;
  }
  .upfile input{
    position: absolute;
    top: 0;
    left: 3.25rem;
    opacity: 0;
    width: 5rem;
    height: 2.5rem;
  }
  .upfileBox{
    border: 1px dashed #ccc;
    position: absolute;
    left: 10.625rem;
    width: 4.6875rem;
    height: 4.6875rem;
  }
  .upfileBox img{
    width: 100%;
    height: 100%;
  }
  .upfileCell{
      padding: 20px 1rem;
      height: 6.25rem;
      .van-cell__title{
        height: 100%;
      }
  }
}
::-webkit-scrollbar {
  width: 0;
}
</style>

<style lang="scss">
.page_editPatients {
  .changeColor {
    .van-field__control {
      color: #9b9b9b !important;
    }
  }
  .basicBox {
    .van-cell__value {
      font-size: 1rem;
    }
    .van-cell__title {
      font-size: 1rem;
    }
  }
  .xtBox {
    .van-cell__value {
      font-size: 1rem;
    }
    .van-cell__title {
      font-size: 1rem;
    }
  }
}
</style>
