<template>
  <div>
    <div class="Dialog">
      <div class="DialogTit">
        <span class="iconfont" @click="close()">&#xe6e9;</span>
        <h1 class="name">接诊评估</h1>
        <span class="success" @click="commitInfo" v-if="hasPermission">保存</span>
        <span class="success" style="visibility: hidden;" v-if="!hasPermission">保存</span>
      </div>
      <div class="warnTxt" v-if="showTxt != ''">
        {{ showTxt }}
      </div>
      <div class="DialogContent choose">
        <el-form
          class="acceptsForm"
          :model="receiveTreatmentAsses"
          label-width="130px"
        >
          <el-form-item label="入室方式: " v-if="isShow('入室方式')">
            <el-radio v-for="(item,index) in way_arr" :key="index"
                      :label="item.id" v-model="receiveTreatmentAsses.way">{{item.name}}
            </el-radio>

          </el-form-item>
          <el-form-item label="病人意识: " v-if="isShow('病人意识')">

            <el-radio v-for="(item,index) in consciousness_arr" :key="index"
                      :label="item.id" v-model="receiveTreatmentAsses.consciousness">{{item.name}}
            </el-radio>

          </el-form-item>
          <el-form-item label="病人食欲: " v-if="isShow('病人食欲')">

            <el-radio v-for="(item,index) in appetite_arr" :key="index"
                      :label="item.id" v-model="receiveTreatmentAsses.appetite">{{item.name}}
            </el-radio>

          </el-form-item>
          <el-form-item label="病人情况: " v-if="isShow('病人情况')">
            <el-radio v-model="receiveTreatmentAsses.condition" label="1"
              >住院</el-radio
            >
            <el-radio v-model="receiveTreatmentAsses.condition" label="2"
              >门诊</el-radio
            >
            <el-radio v-model="receiveTreatmentAsses.condition" label="3"
              >手术期</el-radio
            >
            <el-radio v-model="receiveTreatmentAsses.condition" label="4"
              >急诊</el-radio
            >
          </el-form-item>

          <el-form-item
            label-width="110dp"
            label="住院号: "
            v-if="receiveTreatmentAsses.condition == '1'"
          >
            <el-input
              class="illnessInput"
              v-model="receiveTreatmentAsses.admission_number"
            ></el-input>
          </el-form-item>

          <el-form-item
            label-width="110dp"
            label="诊断"
            v-if="receiveTreatmentAsses.condition == '1'"
          >
            <el-input
              class="illnessInput"
              v-model="receiveTreatmentAsses.diacrisis"
            ></el-input>
          </el-form-item>

          <el-form-item label="体位: " v-if="isShow('体位')">

            <el-radio v-for="(item,index) in posture_arr" :key="index"
                      :label="item.id" v-model="receiveTreatmentAsses.posture">{{item.name}}
            </el-radio>
          </el-form-item>
          <el-form-item label="病情: " v-if="isShow('病情')">

            <el-radio v-for="(item,index) in sick_condition_arr" :key="index"
                      :label="item.id" v-model="receiveTreatmentAsses.sick_condition">{{item.name}}
            </el-radio>
          </el-form-item>
          <el-form-item
            label-width="110dp"
            label="其他病情 : "
            v-if="isShow('其他病情')"
          >
            <el-input
              class="illnessInput"
              v-model="receiveTreatmentAsses.sick_condition_other"
            ></el-input>
          </el-form-item>
          <el-form-item
            label-width="110dp"
            label="跌倒风险评估评分 : "
            v-if="isShow('跌倒风险评估评分')"
          >
            <el-input
              class="illnessInput"
              v-model="receiveTreatmentAsses.score"
              style="width: 100px"
            ></el-input>
          </el-form-item>

          <el-form-item label="风险程度: " v-if="isShow('风险程度')">
            <el-radio v-model="receiveTreatmentAsses.danger_level" label="1"
              >无</el-radio
            >
            <el-radio v-model="receiveTreatmentAsses.danger_level" label="2"
              >低风险</el-radio
            >
            <el-radio v-model="receiveTreatmentAsses.danger_level" label="3"
              >中度风险</el-radio
            >
            <el-radio v-model="receiveTreatmentAsses.danger_level" label="4"
              >高风险</el-radio
            >
          </el-form-item>

          <el-form-item
            label-width="130px"
            label="跌倒风险预防措施: "
            v-if="isShow('跌倒风险预防措施')"
          >
            <el-checkbox-group v-model="precautionTwo">
              <el-checkbox
                v-for="item in precaution_arr"
                :label="item.name"
                :key="item.id"
              >{{ item.name }}</el-checkbox
              >
            </el-checkbox-group>
          </el-form-item>
          <el-form-item
            label-width="110dp"
            label="其他跌倒风险预防措施 : "
            v-if="isShow('其他跌倒风险预防措施')"
          >
            <el-input
              class="illnessInput"
              v-model="receiveTreatmentAsses.precaution_other"
            ></el-input>
          </el-form-item>

          <el-form-item label="摄入量: " v-if="isShow('摄入量')">
            <el-radio v-for="(item,index) in intake_arr" :key="index"
                      :label="item.id" v-model="receiveTreatmentAsses.intake">{{item.name}}
            </el-radio>
          </el-form-item>
          <el-form-item label="营养状况: " v-if="isShow('营养状况')">
            <el-radio v-for="(item,index) in nutrition_arr" :key="index"
                      :label="item.id" v-model="receiveTreatmentAsses.nutrition">{{item.name}}
            </el-radio>

          </el-form-item>
          <el-form-item label="心理评估: " v-if="isShow('心理评估')">
            <el-radio
              v-model="receiveTreatmentAsses.psychological_assessment"
              label="1"
              >正常</el-radio
            >
            <el-radio
              v-model="receiveTreatmentAsses.psychological_assessment"
              label="2"
              >异常</el-radio
            >
          </el-form-item>
          <el-form-item
            label-width="110dp"
            label="心理评估异常情况 : "
            v-if="
              isShow('心理评估异常情况') &&
                receiveTreatmentAsses.psychological_assessment == 2
            "
          >
            <el-input
              v-model="receiveTreatmentAsses.psychological_assessment_other"
            ></el-input>
          </el-form-item>

          <el-form-item
            label-width="110dp"
            label="其他心理评估 : "
            v-if="isShow('其他心理评估')"
          >
            <el-input
              class="illnessInput"
              v-model="receiveTreatmentAsses.psychological_other"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { commitAcceptsAssessment, getLastAccepts } from '@/api/dialysis'
import { Toast } from 'vant'
import { getDataConfig } from '@/utils/data'

import request from '@/utils/request'

export default {
  name: 'AcceptsDialog',
  data () {
    return {
      showTxt: '',
      hasPermission: true,
      way_arr: [],
      consciousness_arr: [],
      appetite_arr: [],
      posture_arr: [],
      sick_condition_arr: [],
      precaution_arr: [],
      intake_arr: [],
      nutrition_arr: [],

      receiveTreatmentAsses: {
        way: '1',
        consciousness: '1',
        appetite: '1',
        condition: '1',
        posture: '1',
        sick_condition: '1',
        danger_level: '1',
        intake: '1',
        nutrition: '1',
        psychological_assessment: '1',
        psychological_assessment_other: '',
        sick_condition_other: '',
        score: '',
        psychological_other: '',
        precaution: '',
        precaution_other: '',
        admission_number: '',
        diacrisis: ''
      },
      patient: {
        id: 0
      },
      record_date: '',
      precautionTwo: []
    }
  },
  props: {
    accepts: {
      type: Object
    },
    patient_prop: {
      type: Object
    }
  },
  created () {
    this.getPermission()
    this.way_arr = getDataConfig('hemodialysis', 'way')
    this.consciousness_arr = getDataConfig('hemodialysis', 'consciousness')
    this.appetite_arr = getDataConfig('hemodialysis', 'appetite')
    this.posture_arr = getDataConfig('hemodialysis', 'posture')
    this.sick_condition_arr = getDataConfig('hemodialysis', 'sick_condition')
    this.precaution_arr = getDataConfig('hemodialysis', 'precaution')
    this.intake_arr = getDataConfig('hemodialysis', 'intake')
    this.nutrition_arr = getDataConfig('hemodialysis', 'nutrition')
    var arr = []

    if (this.accepts.precaution) {
      var precaution = this.accepts.precaution.split(',')
      // console.log("precaution", precaution);
      // console.log("hhh", this.precaution_arr);
      for (let i = 0; i < precaution.length; i++) {
        for (let j = 0; j < this.precaution_arr.length; j++) {
          if (parseInt(precaution[i]) == this.precaution_arr[j].id) {
            // console.log("aaaa");
            arr.push(this.precaution_arr[j].name)
          }
        }
      }
      this.precautionTwo = arr
      // console.log("arr", arr);
    }

    if (this.accepts == null || this.accepts.id == '') {
      let ParamsQuery = {}
      ParamsQuery['patient'] = this.$route.query.patient_id
      getLastAccepts(ParamsQuery)
        .then(response => {
          if (response.data.state == 0) {
            return false
          } else {
            for (const key in response.data.data.receiveTreatmentAsses) {
              this.accepts[key] = response.data.data.receiveTreatmentAsses[key]
              this.accepts['id'] = 0
              this.accepts['creater'] = 0
              this.receiveTreatmentAsses.way = this.accepts.way
              this.receiveTreatmentAsses.consciousness = this.accepts.consciousness
              this.receiveTreatmentAsses.appetite = this.accepts.appetite
              this.receiveTreatmentAsses.condition = this.accepts.condition + ''
              this.receiveTreatmentAsses.posture = this.accepts.posture
              this.receiveTreatmentAsses.sick_condition = this.accepts.sick_condition
              this.receiveTreatmentAsses.danger_level = this.accepts.danger_level + ''
              this.receiveTreatmentAsses.intake = this.accepts.intake
              this.receiveTreatmentAsses.nutrition = this.accepts.nutrition
              this.receiveTreatmentAsses.psychological_assessment = this.accepts.psychological_assessment + ''
              this.receiveTreatmentAsses.psychological_assessment_other = this.accepts.psychological_assessment_other
              this.receiveTreatmentAsses.score = this.accepts.score
              this.receiveTreatmentAsses.sick_condition_other = this.accepts.sick_condition_other
              this.receiveTreatmentAsses.psychological_other = this.accepts.psychological_other
              this.receiveTreatmentAsses.precaution = this.accepts.precaution
              this.receiveTreatmentAsses.precaution_other = this.accepts.precaution_other
              this.receiveTreatmentAsses.admission_number = this.accepts.admission_number
              this.receiveTreatmentAsses.diacrisis = this.accepts.diacrisis

            }
          }
        })
        .catch(error => {
          Toast.fail('请求失败')
        })
    } else {
      if (
        this.accepts.way == 0 &&
        this.accepts.consciousness == 0 &&
        this.accepts.appetite == 0 &&
        this.accepts.condition == 0 &&
        this.accepts.posture == 0 &&
        this.accepts.sick_condition == 0 &&
        this.accepts.danger_level == 0 &&
        this.accepts.intake == 0 &&
        this.accepts.nutrition == 0 &&
        this.accepts.psychological_assessment == 0 &&
        this.accepts.psychological_assessment_other == '' &&
        this.accepts.sick_condition_other == '' &&
        this.accepts.psychological_other == '' &&
        this.accepts.precaution == 0 &&
        this.accepts.precaution_other == '' &&
        this.accepts.score == '' &&
        this.accepts.admission_number == ''
      ) {
        let ParamsQuery = {}
        ParamsQuery['patient'] = this.$route.query.patient_id
        getLastAccepts(ParamsQuery).then(response => {
          if (response.data.state == 0) {
            return false
          } else {
            for (const key in response.data.data.receiveTreatmentAsses) {
              this.accepts[key] = response.data.data.receiveTreatmentAsses[key]
              this.accepts['id'] = 0
              this.accepts['creater'] = 0

              this.receiveTreatmentAsses.way = this.accepts.way
              this.receiveTreatmentAsses.consciousness = this.accepts.consciousness
              this.receiveTreatmentAsses.appetite = this.accepts.appetite
              this.receiveTreatmentAsses.condition =
                this.accepts.condition + ''
              this.receiveTreatmentAsses.posture = this.accepts.posture
              this.receiveTreatmentAsses.sick_condition = this.accepts.sick_condition
              this.receiveTreatmentAsses.danger_level =
                this.accepts.danger_level + ''
              this.receiveTreatmentAsses.intake = this.accepts.intake
              this.receiveTreatmentAsses.nutrition = this.accepts.nutrition
              this.receiveTreatmentAsses.psychological_assessment =
                this.accepts.psychological_assessment + ''
              this.receiveTreatmentAsses.psychological_assessment_other =
                this.accepts.psychological_assessment_other + ''
              this.receiveTreatmentAsses.score = this.accepts.score
              this.receiveTreatmentAsses.sick_condition_other = this.accepts.sick_condition_other

              this.receiveTreatmentAsses.precaution = this.accepts.precaution
              this.receiveTreatmentAsses.precaution_other = this.accepts.precaution_other
              this.receiveTreatmentAsses.psychological_other = this.accepts.psychological_other
              this.receiveTreatmentAsses.admission_number = this.accepts.admission_number
              this.receiveTreatmentAsses.diacrisis = this.accepts.diacrisis
            }
          }
        })
      } else {
        this.receiveTreatmentAsses.way = this.accepts.way
        this.receiveTreatmentAsses.consciousness = this.accepts.consciousness
        this.receiveTreatmentAsses.appetite = this.accepts.appetite
        this.receiveTreatmentAsses.condition = this.accepts.condition + ''
        this.receiveTreatmentAsses.posture = this.accepts.posture
        this.receiveTreatmentAsses.sick_condition = this.accepts.sick_condition
        this.receiveTreatmentAsses.danger_level =
          this.accepts.danger_level + ''
        this.receiveTreatmentAsses.intake = this.accepts.intake
        this.receiveTreatmentAsses.nutrition = this.accepts.nutrition
        this.receiveTreatmentAsses.psychological_assessment =
          this.accepts.psychological_assessment + ''
        this.receiveTreatmentAsses.psychological_assessment_other =
          this.accepts.psychological_assessment_other + ''
        this.receiveTreatmentAsses.score = this.accepts.score
        this.receiveTreatmentAsses.sick_condition_other = this.accepts.sick_condition_other

        this.receiveTreatmentAsses.precaution = this.accepts.precaution
        this.receiveTreatmentAsses.precaution_other = this.accepts.precaution_other
        this.receiveTreatmentAsses.psychological_other = this.accepts.psychological_other
        this.receiveTreatmentAsses.admission_number = this.accepts.admission_number
        this.receiveTreatmentAsses.diacrisis = this.accepts.diacrisis
      }
    }

    var date = this.$route.query && this.$route.query.date
    date *= 1000
    var newDate = new Date(date)

    var y = newDate.getFullYear()
    var m = newDate.getMonth() + 1
    var d = newDate.getDate()
    if (isNaN(y) || isNaN(m) || isNaN(d)) {
      newDate = new Date()
      y = newDate.getFullYear()
      m = newDate.getMonth() + 1
      d = newDate.getDate()
    }
    this.record_date =
      y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d)
    this.patient.id = this.patient_prop.id
  },
  methods: {
    isShow (name) {
      var filedList = this.$store.getters.user.fileds
      for (let i = 0; i < filedList.length; i++) {
        if (
          filedList[i].module == 2 &&
          filedList[i].filed_name_cn == name &&
          filedList[i].is_show == 1
        ) {
          return true
        }
      }

      return false
    },
    commitInfo: function () {
      var arr = []
      for (let i = 0; i < this.precaution_arr.length; i++) {
        for (let j = 0; j < this.precautionTwo.length; j++) {
          if (this.precaution_arr[i].name == this.precautionTwo[j]) {
            arr.push(this.precaution_arr[i].id)
          }
        }
      }
      console.log('arr', arr)
      var obj = arr.join(',')
      this.receiveTreatmentAsses.precaution = obj
      let mode = 1
      Toast.loading({ forbidClick: true, duration: 0 })

      let ParamsQuery = this.receiveTreatmentAsses
      console.log('ParamsQuery', ParamsQuery)
      // ParamsQuery["patient"] =  this.patient.id
      ParamsQuery['patient'] = this.$route.query.patient_id
      ParamsQuery['record_date'] = this.record_date
      if (this.accepts.id > 0) {
        mode = 2
        if (this.accepts.creater != this.$store.getters.user.admin.id) {
          mode = 3
        }
      }
      ParamsQuery['mode'] = mode
      commitAcceptsAssessment(ParamsQuery)
        .then(response => {
          if (response.data.state == 0) {
            Toast.fail(response.data.msg)
            return false
          } else {
            Toast.success('提交成功')
            for (const key in response.data.data.receiveTreatmentAsses) {
              this.accepts[key] = response.data.data.receiveTreatmentAsses[key]
            }
            this.finish()
          }
        })
        .catch(error => {
          Toast.fail('请求失败')
        })
    },
    finish: function () {
      this.$emit('finish')
    },
    close: function () {
      this.$emit('close')
    },
    getPermission () {
      request
        .get('/m/api/permission/get', {
          params: {
            create_url: '/m/api/dialysis/acceptsAssessment?mode=1',
            modify_url: '/m/api/dialysis/acceptsAssessment?mode=2',
            modify_other_url: '/m/api/dialysis/acceptsAssessment?mode=3',
            module: 2
          }
        })
        .then(res => {
          console.log(res)
          console.log('接诊', this.accepts)

          if (res.data.state == 0) {
            this.hasPermission = false
          } else if (res.data.state == 1) {
            if (this.accepts.id != '' && this.accepts.creater != 0) {
              // 有数据
              if (this.accepts.creater == this.$store.getters.user.admin.id) {
                // 创建人是自己
                if (res.data.data.is_has_modify == false) {
                  this.hasPermission = false
                  this.showTxt = '你没有修改接诊评估权限'
                }
              } else {
                // 创建人不是自己
                if (res.data.data.is_has_modify_other == false) {
                  this.hasPermission = false
                  this.showTxt = '你没有修改他人接诊评估权限'
                }
              }
            } else if (this.accepts.id == '' || this.accepts.creater == 0) {
              if (res.data.data.is_has_create == false) {
                this.hasPermission = false
                this.showTxt = '你没有新增接诊评估权限'
              }
            }
          }
        })
    }
  }
}
</script>

<style style="stylesheet/scss" lang="scss" scoped>
.choose {
  .el-form-item {
    margin-bottom: 0;
    border-bottom: 1px #e5e5e5 solid;
    padding: 0.15rem 0.36rem;
    .el-radio {
      font-size: 18px;
    }
  }
  .button {
    text-align: center;
    margin-top: 1rem;
    .submitButton {
      width: 3rem;
      height: 0.8rem;
      line-height: 0.8rem;
      background: $main-color;
      color: #fff;
      font-size: 0.3rem;
      text-align: center;
      border-radius: 6px;
    }
  }
}
.warnTxt {
  font-size: 0.5rem;
  height: 1rem;
  line-height: 1rem;
  text-align: center;
  background: #faa331;
  color: #fff;
}
</style>
<style lang="scss">
.acceptsForm {
  .el-radio {
    margin-right: 14px;
    width: 2.2rem;
    @media only screen and (min-width: 768px) {
      margin-bottom: 0.34rem;
    }
  }
  .el-radio__inner {
    width: 16px !important;
    height: 16px !important;
    @media only screen and (min-width: 768px) {
      width: 26px !important;
      height: 26px !important;
      margin-bottom: 4px !important;
    }
  }
}

.illnessInput {
  .el-input__inner {
    font-size: 0.45rem;
    @media only screen and (min-width: 768px) {
      height: 60px;
      line-height: 60px;
      font-size: 0.45rem;
    }
  }
}
</style>
