<template>
  <div class="page_addPatients">
    <van-sticky>
      <div class="addPatientsTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
        <span class="titleName">患者中心</span>
        <p @click="savePatient">保存</p>
        <!-- <p @click="toJiaoBen">脚本</p> -->
      </div>
    </van-sticky>
    <div class="basicTitle">基本信息</div>
    <div class="basicBox">
      <van-field
        v-model="patientForm.name"
        required
        label="姓名"
        placeholder="请输入"
      />
      <van-field
        label="性别"
        v-model="patientForm.sex"
        right-icon="arrow"
        placeholder="请选择"
        required
        readonly
        @click="show = true"
      />
      <van-popup v-model="show" position="bottom" :style="{ height: '40%' }">
        <van-picker
          :columns="columns"
          show-toolbar
          @confirm="onChange"
          @cancel="onCancel5"
        />
      </van-popup>

      <van-field
        v-model="patientForm.idCard"
        required
        label="身份证号"
        @blur="checkIdCardNo"
        placeholder="请输入"
      />
      <van-field
        v-model="patientForm.birthday"
        required
        label="出生日期"
        readonly
        placeholder="请输入"
      />
      <van-field
        v-model="patientForm.phone"
        type="tel"
        required
        label="手机号"
        placeholder="请输入"
        @blur="checkMobile"
      />

       <van-field
        v-model="patientForm.relative_phone"
        type="tel"
        label="家属手机号"
        placeholder="请输入"

      />
      <van-cell>

        <!-- 使用 title 插槽来自定义标题 -->
        <!-- <template slot="title">
          <span class="custom-title">患者类型</span>
          <div style="width:60%;margin-left:2.2rem">
            <van-checkbox-group v-model="result" @change="changeMode">
              <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">CKD患者</van-checkbox>
              <van-checkbox name="6" shape="square">其他患者</van-checkbox>
            </van-checkbox-group>
          </div>
        </template> -->

        <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>
    </div>

    <div v-show="bloodShow">
      <div class="basicTitle">血透信息</div>
      <div class="xtBox">
        <van-field
          v-model="patientForm.dialysis"
          center
          clearable
          required
          label="透析号"
        >
          <van-button
            slot="button"
            size="small"
            type="info"
            @click="generatedialysisno"
            >自动生成</van-button
          >
        </van-field>

        <van-field
          label="患者来源"
          v-model="patientForm.patientsoure"
          right-icon="arrow"
          required
          readonly
          placeholder="请选择"
          @click="show10 = true"
        />
        <van-popup
          v-model="show10"
          position="bottom"
          :style="{ height: '40%' }"
        >
          <van-picker
            :columns="columns3"
            show-toolbar
            @confirm="onChangeOne"
            @cancel="oncancelOne"
          />
        </van-popup>

        <van-field
          label="留治状态"
          v-model="patientForm.lapseto"
          right-icon="arrow"
          required
          readonly
          placeholder="请选择"
          @click="show9 = true"
        />
        <van-popup v-model="show9" position="bottom" :style="{ height: '40%' }">
          <van-picker
            :columns="columns4"
            show-toolbar
            @confirm="onChangeTwo"
            @cancel="onCancelTwo"
          />
        </van-popup>

        <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
          v-model="patientForm.admissionNumber"
          type="text"
          label="住院(门诊)号"
          placeholder="请填写"
        />

        <van-field
          label="首次透析日期"
          v-model="patientForm.fistDate"
          right-icon="arrow"
          placeholder="请选择"
          readonly
          @click="show7 = true"
        />

        <van-popup v-model="show7" position="bottom" :style="{ height: '40%' }">
          <van-datetime-picker
            v-model="currentDate"
            type="date"
            :min-date="minDate"
            :max-date="maxDate"
            @cancel="onCancelOne"
            @confirm="onConfirmOne"
          />
        </van-popup>

        <van-field
          v-model="patientForm.diagnose"
          rows="1"
          autosize
          label="诊断"
          type="textarea"
          maxlength="50"
          placeholder="请输入主要诊断信息"
        />

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

    <div v-show="slowShow">
      <div class="basicTitle">慢病信息</div>
      <div class="xtBox">
        <van-field
          v-model="patientForm.requipmentId"
          label="设备ID"
          placeholder="请输入"
        />

        <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
          label="首次透析日期"
          v-model="patientForm.memberFistDate"
          right-icon="arrow"
          readonly
          placeholder="请选择"
          @click="show2 = true"
        />
        <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
          label="病种"
          v-model="patientForm.patient_type"
          right-icon="arrow"
          readonly
          placeholder="请选择"
          @click="show14 = true"
        />
        <van-popup
          v-model="show14"
          position="bottom"
          :style="{ height: '40%' }"
        >
          <van-picker
            :columns="patientType"
            show-toolbar
            @confirm="onChangeSix"
            @cancel="onCancelSix"
          />
        </van-popup>
        <van-field
          label="治疗方式"
          v-model="patientForm.treatmentMethod"
          right-icon="arrow"
          readonly
          placeholder="请选择"
          @click="show13 = true"
        />
        <van-popup
          v-model="show13"
          position="bottom"
          :style="{ height: '40%' }"
        >
          <van-picker
            :columns="treatmentMethod"
            show-toolbar
            @confirm="onChangeSeven"
            @cancel="onCancelSeven"
          />
        </van-popup>
      </div>
    </div>
    <div class="moreInfo">如需要完善更多病人信息，请在电脑端完善</div>
  </div>
</template>

<script>
import '../../styles/resetStyle.scss'
import {
  GenerateDialysisNo,
  GetIllnessList,
  savePatient,
  GetPatientInfo,
  getBloodPatientInfo,
  getSlowPatientInfo,
  getMemberPatientInfo,
  toGetPatient
} from '@/api/patient/patient'
import { isCardNo, checkMobile, getFileExtension } from '@/utils/tools'
import { getToken } from '@/api/qiniu'
import { setRem, setHeight } from '@/libs/functionRem'
const moment = require('moment')
export default {
  data () {
    return {
      show: false,
      show1: false,
      show2: false,
      show10: false,
      show9: false,
      show8: false,
      show7: false,
      show6: false,
      show11: false,
      show12: false,
      show13: false,
      show14: false,
      name: '',
      columns: ['男', '女'],
      columns1: ['1', '2', '3'],
      sex: '',
      idCard: '',
      birthday: '',
      tel: '',
      radio: '1',
      result: [],
      resultTwo: [],
      resultThree: [],
      resultFour: [],
      dialysisNumber: '',
      source: '',
      hospitalNum: '',
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      time: '',
      message: '',
      bloodShow: true,
      slowShow: false,
      memberShow: false,
      columns3: ['门诊', '住院'],
      columns4: ['留治', '转出', '死亡'],
      contagionList: ['乙肝', '丙肝', '艾滋病', '肺结核', '梅毒'],
      checkDisease: [
        '血透',
        '腹透',
        '移植',
        '高血压',
        '糖尿病',
        '慢性肾病',
        '其他'
      ],
      treatmentMethod: [
        '西医治疗',
        '中医治疗',
        '血液透析',
        '腹膜透析',
        '肾移植'
      ],
      patientType: [],
      patientForm: {
        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: '',
        is_infectious: 1, // 是否患有传染病
        relative_phone: '',
        remark: '',
        treatment_plan: ''
      },
      illnesslist: [],
      showOne: false,
      patient_type:1,
    }
  },
  methods: {
    onChange (picker, value, index) {
      if (value == 0) {
        this.patientForm.sex = '男'
        this.show = false
      }
      if (value == 1) {
        this.patientForm.sex = '女'
        this.show = false
      }
    },
    onCancel5 () {
      this.show = false
    },
    onChangeOne (picker, value, index) {
      if (value == 0) {
        this.patientForm.patientsoure = '门诊'
        this.show10 = false
      }
      if (value == 1) {
        this.patientForm.patientsoure = '住院'
        this.show10 = false
      }
    },
    oncancelOne () {
      this.show10 = false
    },
    onChangeTwo (picker, value, index) {
      if (value == 0) {
        this.patientForm.lapseto = '留治'
        this.show9 = false
      }
      if (value == 1) {
        this.patientForm.lapseto = '转出'
        this.show9 = false
      }

      if (value == 2) {
        this.patientForm.lapseto = '死亡'
        this.show9 = false
      }
    },
    onCancelTwo () {
      this.show9 = false
    },
    onChangeThree (picker, value, index) {
      this.patientForm.contagions = value
      this.show8 = false
    },
    onChangeFour (picker, value, index) {
      this.patientForm.slowcontagions = value
      this.show11 = false
    },
    onChangeFive (picker, value, index) {
      this.patientForm.slowDisease = value
      this.show12 = false
    },
    onChangeSix (picker, value, index) {
      if (value == 0) {
        this.patientForm.patient_type = '尿毒症'
        this.show14 = false
      }
      if (value == 1) {
        this.patientForm.patient_type = '慢性肾衰竭'
        this.show14 = false
      }
      if (value == 2) {
        this.patientForm.patient_type = '糖尿病肾病'
        this.show14 = false
      }
      if (value == 3) {
        this.patientForm.patient_type = '高血压肾病'
        this.show14 = false
      }
      if (value == 4) {
        this.patientForm.patient_type = '肾病综合征'
        this.show14 = false
      }
      if (value == 5) {
        this.patientForm.patient_type = 'IGA肾病'
        this.show14 = false
      }
      if (value == 6) {
        this.patientForm.patient_type = '慢性肾炎'
        this.show14 = false
      }
      if (value == 7) {
        this.patientForm.patient_type = '紫癜性肾炎'
        this.show14 = false
      }
      if (value == 8) {
        this.patientForm.patient_type = '狼疮性肾炎'
        this.show14 = false
      }
      if (value == 9) {
        this.patientForm.patient_type = '小儿肾病'
        this.show14 = false
      }
      if (value == 10) {
        this.patientForm.patient_type = '肾结石'
        this.show14 = false
      }
      if (value == 11) {
        this.patientForm.patient_type = '其他'
        this.show14 = false
      }
    },
    onCancelSix () {
      this.show14 = false
    },
    onChangeSeven (picker, value, index) {
      if (value == 0) {
        this.patientForm.treatmentMethod = '西医治疗'
        this.show13 = false
      }
      if (value == 1) {
        this.patientForm.treatmentMethod = '中医治疗'
        this.show13 = false
      }
      if (value == 2) {
        this.patientForm.treatmentMethod = '血液透析'
        this.show13 = false
      }
      if (value == 3) {
        this.patientForm.treatmentMethod = '腹膜透析'
        this.show13 = false
      }
      if (value == 4) {
        this.patientForm.treatmentMethod = '肾移植'
        this.show13 = false
      }
    },
    onCancelSeven () {
      this.show13 = false
    },
    onChange1 (picker, value, index) {
      this.source = value
      this.show1 = 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.fistDate = `${year}-${month}-${day}`
      this.show7 = false
    },
    onCancelOne () {
      this.show7 = 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.memberFistDate = `${year}-${month}-${day}`
      this.show2 = false
    },
    changeMode (val) {
      // alert(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
          // this.patientForm.dialysis = no;
        }
      })
    },
    GetIllnessList () {
      GetIllnessList().then(response => {
        if (response.data.state === 1) {
          var illnesslist = response.data.data.illnesslist
          this.illnesslist = illnesslist
          var arr = []
          for (let i = 0; i < illnesslist.length; i++) {
            arr.push(illnesslist[i].illness_name)
          }

          this.patientType = arr
        }
      })
    },
    savePatient () {
      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') {
          this.patientForm.sex = 1
          this.patientForm.avatar =
            'https://images.shengws.com/201809182128111.png'
        }
        if (this.patientForm.sex === '女' || this.patientForm.sex == '2') {
          this.patientForm.sex = 2
          this.patientForm.avatar =
            'https://images.shengws.com/201809182128222.png'
        }
      }
      if (this.patientForm.patientsoure === '') {
        this.patientForm.patientsoure = 0
      }
      if (this.patientForm.patientsoure !== '') {
        if (this.patientForm.patientsoure === '门诊' || this.patientForm.patientsoure == '1') {
          this.patientForm.patientsoure = 1
        }
        if (this.patientForm.patientsoure === '住院' || this.patientForm.patientsoure == '2') {
          this.patientForm.patientsoure = 2
        }
      }
      // if (this.patientForm.lapseto !== '') {
      //   if (this.patientForm.lapseto == '留治' || this.patientForm.patientsoure == '1') {
      //     this.patientForm.lapseto = 1
      //   }
      //   if (this.patientForm.lapseto == '转出' || this.patientForm.patientsoure == '2') {
      //     this.patientForm.lapseto = 2
      //   }
      // }
      if (this.patientForm.patient_type == '0') {
        this.patientForm.patient_type = 0
      }
      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
        }
      }
      // console.log('2345555', this.patientForm.lapseto)
      const params = {
        name: this.patientForm.name,
        sex: 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,
        relative_phone: this.patientForm.relative_phone,
        remark: this.patientForm.remark,
        treatment_plan: this.patientForm.treatment_plan,
        patient_type:parseInt(this.patient_type),
      }
    
      
      if(params.lapseto >0){
        params.lapseto = parseInt(params.lapseto)
      }else{
        params.lapseto = parseInt(params.lapseto)
      }
      params.treatmentMethod = 1

      // if(params.treatmentMethod >0){
      //   params.treatmentMethod = parseInt(params.treatmentMethod)?parseInt(params.treatmentMethod):0
      // }else{
      //   params.treatmentMethod = parseInt(params.treatmentMethod)?parseInt(params.treatmentMethod):0
      // }
      console.log('params', params)

      savePatient(params).then(response => {
        if (response.data.state === 1) {
          var msg = response.data.data.msg
          // console.log('msg', msg)
          if (msg == '身份证号码已存在') {
            this.$toast('身份证号码已存在')
            // console.log('this--', this.patientForm.sex)
            if (this.patientForm.patient_type == '0') {
              this.patientForm.patient_type = 0
            }
            if (this.patientForm.treatmentMethod == '0') {
              this.patientForm.treatmentMethod = 0
            }
            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 = '死亡'
            }

            if (this.patientForm.patient_type == 21) {
              this.patientForm.patient_type = '尿毒症'
            }

            if (this.patientForm.patient_type == 22) {
              this.patientForm.patient_type = '慢性肾衰竭'
            }
            if (this.patientForm.patient_type == 23) {
              this.patientForm.patient_type = '糖尿病肾病'
            }
            if (this.patientForm.patient_type == 24) {
              this.patientForm.patient_type = '高血压肾病'
            }
            if (this.patientForm.patient_type == 25) {
              this.patientForm.patient_type = '肾病综合征'
            }
            if (this.patientForm.patient_type == 26) {
              this.patientForm.patient_type = 'IGA肾病'
            }
            if (this.patientForm.patient_type == 27) {
              this.patientForm.patient_type = '慢性肾炎'
            }
            if (this.patientForm.patient_type == 28) {
              this.patientForm.patient_type = '紫癜性肾炎'
            }
            if (this.patientForm.patient_type == 29) {
              this.patientForm.patient_type = '狼疮性肾炎'
            }
            if (this.patientForm.patient_type == 30) {
              this.patientForm.patient_type = '小儿肾病'
            }
            if (this.patientForm.patient_type == 31) {
              this.patientForm.patient_type = '肾结石'
            }
            if (this.patientForm.patient_type == 32) {
              this.patientForm.patient_type = '其他'
            }

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

            return false
          }
          if (msg == '手机号码已存在') {
            this.$toast('手机号码已存在')

            if (this.patientForm.treatmentMethod == '0') {
              this.patientForm.treatmentMethod = 0
            }
            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 = '死亡'
            }
            if (this.patientForm.patient_type == '0') {
              this.patientForm.patient_type = 0
            }

            if (this.patientForm.patient_type == 21) {
              this.patientForm.patient_type = '尿毒症'
            }

            if (this.patientForm.patient_type == 22) {
              this.patientForm.patient_type = '慢性肾衰竭'
            }
            if (this.patientForm.patient_type == 23) {
              this.patientForm.patient_type = '糖尿病肾病'
            }
            if (this.patientForm.patient_type == 24) {
              this.patientForm.patient_type = '高血压肾病'
            }
            if (this.patientForm.patient_type == 25) {
              this.patientForm.patient_type = '肾病综合征'
            }
            if (this.patientForm.patient_type == 26) {
              this.patientForm.patient_type = 'IGA肾病'
            }
            if (this.patientForm.patient_type == 27) {
              this.patientForm.patient_type = '慢性肾炎'
            }
            if (this.patientForm.patient_type == 28) {
              this.patientForm.patient_type = '紫癜性肾炎'
            }
            if (this.patientForm.patient_type == 29) {
              this.patientForm.patient_type = '狼疮性肾炎'
            }
            if (this.patientForm.patient_type == 30) {
              this.patientForm.patient_type = '小儿肾病'
            }
            if (this.patientForm.patient_type == 31) {
              this.patientForm.patient_type = '肾结石'
            }
            if (this.patientForm.patient_type == 32) {
              this.patientForm.patient_type = '其他'
            }

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

            return false
          }
          if (msg == '透析号已存在') {
            this.$toast('透析号已存在')
            if (this.patientForm.treatmentMethod == '0') {
              this.patientForm.treatmentMethod = 0
            }
            if (this.patientForm.sex == 1) {
              this.patientForm.sex = '男'
            }
            if (this.patientForm.sex == 2) {
              this.patientForm.sex = '女'
            }

            if (this.patientForm.patient_type == '0') {
              this.patientForm.patient_type = 0
            }
            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 = '死亡'
            }
            if (this.patientForm.patient_type == 1) {
              this.patientForm.patient_type = '尿毒症'
            }

            if (this.patientForm.patient_type == 2) {
              this.patientForm.patient_type = '慢性肾衰竭'
            }
            if (this.patientForm.patient_type == 3) {
              this.patientForm.patient_type = '糖尿病肾病'
            }
            if (this.patientForm.patient_type == 4) {
              this.patientForm.patient_type = '高血压肾病'
            }
            if (this.patientForm.patient_type == 5) {
              this.patientForm.patient_type = '肾病综合征'
            }
            if (this.patientForm.patient_type == 6) {
              this.patientForm.patient_type = 'IGA肾病'
            }
            if (this.patientForm.patient_type == 7) {
              this.patientForm.patient_type = '慢性肾炎'
            }
            if (this.patientForm.patient_type == 8) {
              this.patientForm.patient_type = '紫癜性肾炎'
            }
            if (this.patientForm.patient_type == 9) {
              this.patientForm.patient_type = '狼疮性肾炎'
            }
            if (this.patientForm.patient_type == 10) {
              this.patientForm.patient_type = '小儿肾病'
            }
            if (this.patientForm.patient_type == 11) {
              this.patientForm.patient_type = '肾结石'
            }
            if (this.patientForm.patient_type == 12) {
              this.patientForm.patient_type = '其他'
            }

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

            return false
          }
          var patient = response.data.data.patient
          // console.log('patient', patient)
          if (msg === undefined) {
            this.$toast('保存成功')
            this.$router.push({ path: '/patients' })
          }
        } else {
          this.$toast('网络错误')
          if (this.patientForm.treatmentMethod == '0') {
            this.patientForm.treatmentMethod = 0
          }
          if (this.patientForm.sex == 1) {
            this.patientForm.sex = '男'
          }
          if (this.patientForm.sex == 2) {
            this.patientForm.sex = '女'
          }

          if (this.patientForm.patient_type == '0') {
            this.patientForm.patient_type = 0
          }
          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 = '死亡'
          }
          if (this.patientForm.patient_type == 1) {
            this.patientForm.patient_type = '尿毒症'
          }

          if (this.patientForm.patient_type == 2) {
            this.patientForm.patient_type = '慢性肾衰竭'
          }
          if (this.patientForm.patient_type == 3) {
            this.patientForm.patient_type = '糖尿病肾病'
          }
          if (this.patientForm.patient_type == 4) {
            this.patientForm.patient_type = '高血压肾病'
          }
          if (this.patientForm.patient_type == 5) {
            this.patientForm.patient_type = '肾病综合征'
          }
          if (this.patientForm.patient_type == 6) {
            this.patientForm.patient_type = 'IGA肾病'
          }
          if (this.patientForm.patient_type == 7) {
            this.patientForm.patient_type = '慢性肾炎'
          }
          if (this.patientForm.patient_type == 8) {
            this.patientForm.patient_type = '紫癜性肾炎'
          }
          if (this.patientForm.patient_type == 9) {
            this.patientForm.patient_type = '狼疮性肾炎'
          }
          if (this.patientForm.patient_type == 10) {
            this.patientForm.patient_type = '小儿肾病'
          }
          if (this.patientForm.patient_type == 11) {
            this.patientForm.patient_type = '肾结石'
          }
          if (this.patientForm.patient_type == 12) {
            this.patientForm.patient_type = '其他'
          }

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

    checkMobile () {
      if (!checkMobile(this.patientForm.phone)) {
        this.$toast('请填写正确的手机号')
        return false
      }
    },
    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)
    },
    changRadio () {
      if (this.radio === '1') {
        this.showOne = false
      }
      if (this.radio === '2') {
        this.showOne = true
      }
    },
    toJiaoBen(){
      toGetPatient().then(response=>{
        if(response.data.state == 1){
          var msg = response.data.data.msg
          this.$toast('成功')
        }
      })
    }
  },
  created () {
    setRem()
    setHeight()
    this.GetIllnessList()
  }
}
</script>

<style lang="scss" scoped>
.page_addPatients {
  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;
    p {
      font-size: 1rem;
    }
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 1.25rem;
  }
  .titleName {
    font-size: 1.125rem;
    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;
      font-size: 1rem;
    }
    .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--small {
    min-width: 60px !important;
    height: 30px;
    padding: 0 8px;
    font-size: 12px !important;
    line-height: 28px;
  }
  .van-button--info {
    border-radius: 0.9375rem;
  }
  .moreInfo {
    text-align: center;
    font-size: 0.75rem;
    color: #338afb;
    margin: 0.625rem 0 1.25rem;
  }
  .van-cell {
    align-items: center;
    // font-size: 1rem;
  }
}
::-webkit-scrollbar {
  width: 0;
}
</style>

<style lang="scss">
.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>
