<template>

    <div class="main-contain">
    <div class="position">
        <bread-crumb :crumbs='crumbs'></bread-crumb>
    </div>
    <div class="app-container sign-and-weigh-box">
        <el-row :gutter="24">
            <el-col :span="8">
                <div class="dataTitle">患者列表</div>
                <div style="margin-bottom: 10px;">
                    <el-input v-model.trim="queryParams.keywords" @keyup.enter.native='changeSearch' placeholder="姓名/透析号" style="width: 150px;" @change="changeSearch"></el-input>&nbsp;
                    <el-button type="primary" @change="changeSearch" icon="el-icon-search">搜索</el-button>&nbsp;&nbsp;&nbsp;&nbsp;
                    <el-select v-model="queryParams.schedule_type" style="width: 100px;" @change="changeSearch">
                    <el-option v-for="item in shiftOptions" :label="item.label" :value="item.value" :key="item.value"></el-option>
                    </el-select>
                </div>

                <el-table  :data="patients" :class="signAndWeighBoxPatients" style="width: 100%" border highlight-current-row :header-cell-style="{
                  backgroundColor: 'rgb(245, 247, 250)',
                  color: '#606266'
                }"
                :row-style="{ color: '#303133' }" max-height="300" @current-change="handleCurrentChange">
                    <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                    <el-table-column prop="name" label="姓名" min-width="20" align="center">
                       <template slot-scope="scope">
                            {{scope.row.name}}({{scope.row.dialysis_no}})
                        </template>
                    </el-table-column>

                    <el-table-column prop="state" label="状态" min-width="30" align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.dialysis_order && scope.row.dialysis_order.stage==2">已下机</span>
                            <span v-else-if="scope.row.dialysis_order && scope.row.dialysis_order.id>0 ">已上机</span>
                            <span v-else-if="scope.row.predialysisevaluation.id ==0">未签到</span>
                            <span v-else>已签到</span>

                        </template>
                    </el-table-column>
                </el-table>

                <el-table  :data="schedules" :class="signAndWeighBoxPatients" style="width: 100%; margin:15px 0 0 0;" border highlight-current-row :header-cell-style="{
                  backgroundColor: 'rgb(245, 247, 250)',
                  color: '#606266'
                }"
                :row-style="{ color: '#303133' }">
                    <el-table-column prop="shift" label="班次" min-width="30" align="center"></el-table-column>
                    <el-table-column prop="arrange" label="排班" min-width="30" align="center" ></el-table-column>
                    <el-table-column prop="sign" label="签到" min-width="30" align="center" ></el-table-column>
                    <el-table-column prop="weight" label="称重(透前/透后)" min-width="50" align="center">
                        <template slot-scope="scope">
                           {{scope.row.before}} / {{scope.row.after}}
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>

            <el-col :span="16">
                <div class="dataTitle">患者信息</div>
                <el-form class="information" label-position="left">
                  <div class="inforTitle">
                  <el-button  @click="change()" style="float:right;">修改</el-button>
                   <span class="name">姓名:{{weigh_form.name}} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;透析号:{{weigh_form.dialysis_no}} &nbsp;&nbsp;</span>
                   </div>
                    <div class="border"></div>
                    <div style="margin-bottom:20px;color:rgb(96,96,96);font-size:14px">
                    透前称重(kg):&nbsp;<el-input v-model="weigh_list.weight_before" :disabled="disa" style="width:200px"></el-input> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    干体重(kg):&nbsp;<el-input v-model="weigh_list.dry_weight" :disabled="true" style="width:200px"></el-input><br>
                    </div>
                    <div style="margin-bottom:12px;color:rgb(96,96,96);font-size:14px">
                     体温(℃):&nbsp;<el-input v-model="weigh_list.temperature" :disabled="disa" style="width:70px"></el-input>&nbsp;&nbsp;&nbsp;
                     <!-- 呼吸(次/分):&nbsp;<el-input v-model="weigh_list.breathing_rate" :disabled="disa" style="width:70px"></el-input>&nbsp;&nbsp;&nbsp; -->
                     血压(mmHg):&nbsp;<el-input v-model="weigh_list.systolic_blood_pressure" style="width: 70px" :disabled="disa"></el-input><span> / </span> <el-input v-model="weigh_list.diastolic_blood_pressure" style="width: 70px" :disabled="disa"></el-input>&nbsp;&nbsp;&nbsp;
                     脉率(次/分):&nbsp;<el-input v-model="weigh_list.pulse_frequency" :disabled="disa" style="width:70px"></el-input>
                    </div>
                    <div class="border"></div>
                    <div style="margin-bottom:20px;color:rgb(96,96,96);font-size:14px">
                     透后称重(kg):&nbsp;<el-input v-model="weigh_infor.weight_after" style="width:200px" :disabled="disa"></el-input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                     干体重(kg):&nbsp;<el-input v-model="weigh_list.dry_weight" :disabled="true" style="width:200px"></el-input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </div>
                    <div style="margin-bottom:12px;color:rgb(96,96,96);font-size:14px">
                      体温(℃):&nbsp;<el-input v-model="weigh_infor.temperature" :disabled="disa" style="width:70px"></el-input>&nbsp;&nbsp;&nbsp;
                      <!-- 呼吸(次/分):&nbsp;<el-input v-model="weigh_infor.breathing_rate" :disabled="disa" style="width:70px"></el-input>&nbsp;&nbsp;&nbsp; -->
                      血压(mmHg):&nbsp;<el-input v-model="weigh_infor.systolic_blood_pressure" style="width: 70px" :disabled="disa"></el-input> <span> / </span><el-input v-model="weigh_infor.diastolic_blood_pressure" style="width: 70px" :disabled="disa"></el-input>&nbsp;&nbsp;&nbsp;
                      脉率(次/分):&nbsp;<el-input v-model="weigh_infor.pulse_frequency" :disabled="disa" style="width:70px"></el-input>
                    </div>

                    <div class="border"></div>
                    <div v-if="show">
                    <el-row :gutter="20">
                        <el-col :span="23" align="right" class="button">
                            <el-button  @click="hide()">取消</el-button>
                            <el-button @click="updateSignweight();sighdata()" type="primary">保存</el-button>
                        </el-col>
                    </el-row>
                   </div>
                </el-form>
            </el-col>
        </el-row>
    </div>
   </div>
</template>

<script>
import { fetchSignPatients, getPatientSign, getDialysisInforInfomation, getDialysisAfterInfomation, updateSignweight, sighdata } from '@/api/signandweigh'
import BreadCrumb from '../components/bread-crumb'
export default {
  name: 'sign',
  components: { BreadCrumb },
  data() {
    return {
      crumbs: [
        { path: '/sign/index', name: '签到称重' }
      ],
      patientlist: [],
      signAndWeighBoxPatients: 'sign-and-weigh-box-patients',
      queryParams: {
        keywords: '',
        schedule_type: '',
        need_schedule_type: 0
      },
      querySignParams: {
        patient_id: 0,
        date_time: ''
      },
      weigh_form: {
        choose: false,
        name: '',
        dry_weight: '',
        clothes_weight: '',
        // 透前
        weigh_before: '',
        dehydrated_weight: '',
        dehydrated_percent: '',
        weight_before: '',
        temperature_before: '',
        pulse_rate_before: '', // P 脉率
        respiratory_rate_before: '', // R 呼吸频率
        DBP_before: '', // 舒张压
        SBP_before: '', // 收缩压
        // 透后
        weigh_after: '',
        weight_reduce_after: '',
        weight_after: '',
        temperature_after: '',
        pulse_rate_after: '', // P 脉率
        respiratory_rate_after: '', // R 呼吸频率
        DBP_after: '', // 舒张压
        SBP_after: '', // 收缩压
        dialysis_no: ''

      },
      weigh_list: {
        id: '',
        weight_before: '', // 透前体重
        temperature: '', // 体温
        pulse_frequency: '',
        breathing_rate: '', // 呼吸频率
        dry_weight: '', // 干体重
        systolic_blood_pressure: '', // 收缩压
        diastolic_blood_pressure: ''// 舒张压
      },
      weigh_infor: {
        weight_after: '', // 透后体重
        temperature: '',
        pulse_frequency: '',
        breathing_rate: '',
        dry_weight: '',
        systolic_blood_pressure: '',
        diastolic_blood_pressure: ''
      },
      shiftOptions: [
        { value: 0, label: '全部' },
        { value: 1, label: '上午' },
        { value: 2, label: '下午' },
        { value: 3, label: '晚上' }
      ],
      schedules: [{
        type: 1,
        shift: '上午',
        arrange: 0,
        sign: 0,
        before: 0,
        after: 0
      }, {
        type: 2,
        shift: '下午',
        arrange: 0,
        sign: 0,
        before: 0,
        after: 0
      }, {
        type: 3,
        shift: '晚上',
        arrange: 0,
        sign: 0,
        before: 0,
        after: 0
      }],
      patients: [],
      dialysis_stege: 0,
      show: false,
      disa: true
    }
  },
  methods: {
    fetchSignPatients() {
      fetchSignPatients(this.queryParams).then(response => {
        this.patients = []
        if (response.data.state === 1) {
          this.patients = response.data.data.patients
          // 将没有排班的信息排除
          for (let i = this.patients.length - 1; i >= 0; i--) {
            if (this.patients[i].schedule.id === 0) {
              this.patients.splice(i, 1)
            }
          }
          this.queryParams.schedule_type = response.data.data.schedule_type
          this.querySignParams.date_time = response.data.data.today
          if (this.queryParams.need_schedule_type === 1) {
            var sl = this.schedules.length
            var panel = response.data.data.panel
            for (let index = 0; index < sl; index++) {
              if (this.schedules[index].type in response.data.data.panel) {
                this.schedules[index].arrange = response.data.data.panel[this.schedules[index].type].schedule
                this.schedules[index].sign = response.data.data.panel[this.schedules[index].type].sign
                this.schedules[index].before = response.data.data.panel[this.schedules[index].type].before
                this.schedules[index].after = response.data.data.panel[this.schedules[index].type].after
              }
            }
          }
        }
      })
    },
    getPatientSign() {
      getPatientSign(this.querySignParams).then(response => {
        if (response.data.state === 1) {
          if (response.data.data.sign != null) {
            var sign = response.data.data.sign
            this.weigh_form.dry_weight = sign.dry_weight
            this.weigh_form.clothes_weight = sign.clothes_weight
            // 透前
            this.weigh_form.weigh_before = sign.weigh_before
            this.weigh_form.dehydrated_weight = sign.dehydrated_weight
            this.weigh_form.dehydrated_percent = sign.dehydrated_percent
            this.weigh_form.weight_before = sign.weight_before
            this.weigh_form.temperature_before = sign.temperature_before
            this.weigh_form.pulse_rate_before = sign.pulse_rate_before // P 脉率
            this.weigh_form.respiratory_rate_before = sign.respiratory_rate_before // R 呼吸频率
            this.weigh_form.DBP_before = sign.DBP_before // 舒张压
            this.weigh_form.SBP_before = sign.SBP_before // 收缩压
            // 透后
            this.weigh_form.weigh_after = sign.weigh_after
            this.weigh_form.weight_reduce_after = sign.weight_reduce_after
            this.weigh_form.weight_after = sign.weight_after
            this.weigh_form.temperature_after = sign.temperature_after
            this.weigh_form.pulse_rate_after = sign.pulse_rate_after // P 脉率
            this.weigh_form.respiratory_rate_after = sign.respiratory_rate_after // R 呼吸频率
            this.weigh_form.DBP_after = sign.DBP_after // 舒张压
            this.weigh_form.SBP_after = sign.SBP_after // 收缩压
            this.weigh_form.id = sign.id
          }
        }
      })
    },
    changeSearch() {
      this.queryParams.need_schedule_type = 0
      this.fetchSignPatients()
    },
    handleCurrentChange(row, old) {
      this.disa = true
      this.show = false
      this.weigh_form.choose = true
      this.weigh_form.name = row.name
      this.weigh_form.dialysis_no = row.dialysis_no
      this.weigh_form.patient_id = row.id
      this.querySignParams.patient_id = row.id
      this.dialysis_stege = row.dialysis_order.stage
      this.getDialysisInforInfomation(row.id)
      this.getDialysisAfterInfomation(row.id)
      this.weigh_list.id = row.signin.id
      // console.log("是否有数据",this.weigh_list.id)
      // this.getsignweigh(row.id)
    },
    getDialysisInforInfomation(id) {
      getDialysisInforInfomation(id).then(response => {
        if (response.data.data.patientlist != null) {
          var patientlist = response.data.data.patientlist
          this.weigh_list.id = patientlist.id
          this.weigh_list.dry_weight = patientlist.dry_weight
          if (this.weigh_list.dry_weight === 0) {
            this.weigh_list.dry_weight = ''
          }
          this.weigh_list.weight_before = patientlist.weight_before
          if (this.weigh_list.weight_before === 0) {
            this.weigh_list.weight_before = ''
          }
          this.weigh_list.temperature = patientlist.temperature
          if (this.weigh_list.temperature === 0) {
            this.weigh_list.temperature = ''
          }
          this.weigh_list.pulse_frequency = patientlist.pulse_frequency
          if (this.weigh_list.pulse_frequency === 0) {
            this.weigh_list.pulse_frequency = ''
          }
          this.weigh_list.breathing_rate = patientlist.breathing_rate
          if (this.weigh_list.breathing_rate === 0) {
            this.weigh_list.breathing_rate = ''
          }
          this.weigh_list.systolic_blood_pressure = patientlist.systolic_blood_pressure
          if (this.weigh_list.systolic_blood_pressure === 0) {
            this.weigh_list.systolic_blood_pressure = ''
          }
          this.weigh_list.diastolic_blood_pressure = patientlist.diastolic_blood_pressure
          if (this.weigh_list.diastolic_blood_pressure === 0) {
            this.weigh_list.diastolic_blood_pressure = ''
          }
        }
      })
    },

    getDialysisAfterInfomation(id) {
      getDialysisAfterInfomation(id).then(response => {
        if (response.data.data.patientinfor != null) {
          var patientinfor = response.data.data.patientinfor

          this.weigh_infor.dry_weight = patientinfor.dry_weight
          if (this.weigh_infor.dry_weight === 0) {
            this.weigh_infor.dry_weight = ''
          }

          this.weigh_infor.weight_after = patientinfor.weight_after
          if (this.weigh_infor.weight_after === 0) {
            this.weigh_infor.weight_after = ''
          }

          this.weigh_infor.temperature = patientinfor.temperature
          if (this.weigh_infor.temperature === 0) {
            this.weigh_infor.temperature = ''
          }
          this.weigh_infor.pulse_frequency = patientinfor.pulse_frequency
          if (this.weigh_infor.pulse_frequency === 0) {
            this.weigh_infor.pulse_frequency = ''
          }
          this.weigh_infor.breathing_rate = patientinfor.breathing_rate
          if (this.weigh_infor.breathing_rate === 0) {
            this.weigh_infor.breathing_rate = ''
          }
          this.weigh_infor.systolic_blood_pressure = patientinfor.systolic_blood_pressure
          if (this.weigh_infor.systolic_blood_pressure === 0) {
            this.weigh_infor.systolic_blood_pressure = ''
          }

          this.weigh_infor.diastolic_blood_pressure = patientinfor.diastolic_blood_pressure
          if (this.weigh_infor.diastolic_blood_pressure === 0) {
            this.weigh_infor.diastolic_blood_pressure = ''
          }
        }
      })
    },
    change() {
      this.show = true
      this.disa = false
    },

    updateSignweight() {
      // eslint-disable-next-line no-new-object
      var params = new Object()
      var data_time = this.querySignParams.date_time
      params.date_time = data_time
      params.patient_id = this.weigh_form.patient_id
      params.dry_weight = this.weigh_list.dry_weight
      params.weight_before = this.weigh_list.weight_before
      params.temperature = this.weigh_list.temperature
      params.pulse_frequency = this.weigh_list.pulse_frequency
      params.systolic_blood_pressure = this.weigh_list.systolic_blood_pressure
      params.diastolic_blood_pressure = this.weigh_list.diastolic_blood_pressure

      params.patient_id = this.weigh_form.patient_id
      params.weight_after = this.weigh_infor.weight_after
      params.temperatureafter = this.weigh_infor.temperature
      params.pulse_frequencyafter = this.weigh_infor.pulse_frequency
      params.systolic_blood_pressureafter = this.weigh_infor.systolic_blood_pressure
      params.diastolic_blood_pressureafter = this.weigh_infor.diastolic_blood_pressure
      console.log("params",params)
      updateSignweight(params).then(response => {
        if (response.data.state === 1) {
          var signs = response.data.data.signs
          console.log('signs是什么', signs)
          this.$message.success('成功')
          // this.$message({
          //   type: 'success',
          //   message: '成功!'
          // })
        }
      })
    },
    hide() {
      this.show = false
    },

    sighdata() {
      // 透前数据
      var params = new Object()
      var data_time = this.querySignParams.date_time
      params.date_time = data_time
      params.patient_id = this.weigh_form.patient_id
      params.dry_weight = this.weigh_list.dry_weight
      params.weight_before = this.weigh_list.weight_before
      params.temperature = this.weigh_list.temperature
      params.pulse_frequency = this.weigh_list.pulse_frequency
      params.breathing_rate = this.weigh_list.breathing_rate
      params.systolic_blood_pressure = this.weigh_list.systolic_blood_pressure
      params.diastolic_blood_pressure = this.weigh_list.diastolic_blood_pressure

      // 透后数据
      params.weight_after = this.weigh_infor.weight_after
      params.temperatureafter = this.weigh_infor.temperature
      params.pulse_frequencyafter = this.weigh_infor.pulse_frequency
      params.systolic_blood_pressureafter = this.weigh_infor.systolic_blood_pressure
      params.diastolic_blood_pressureafter = this.weigh_infor.diastolic_blood_pressure
      sighdata(params).then(response => {
        var signs = response.data.data.signs
        if (response.data.state === 1) {
          // this.$message({
          //   type: 'success',
          //   message: '成功!'
          // })
          var tlen = this.patients.length

          for (let index = 0; index < tlen; index++) {
            if (this.patients[index].id === params.patient_id) {
              var signin = this.patients[index].signin
    
              var sings = response.data.data.signs
         

              this.patients[index].signin = response.data.data.signs

              if (this.patients[index].schedule.id > 0 && (this.patients[index].schedule.schedule_type >= 1 || this.patients[index].schedule.schedule_type <= 3)) {
                var sl = this.schedules.length
                for (let j = 0; j < sl; j++) {
                  if (this.schedules[j].type === this.patients[index].schedule.schedule_type) {
                    if ((signin.weigh_before_time === 0 && response.data.data.signs.weigh_before_time > 0) || (signin.weigh_time === 0 && response.data.data.signs.weigh_time > 0)) {
                      this.schedules[j].sign++
                    }
                    if (signin.weigh_before_time === 0 && response.data.data.signs.weigh_before_time > 0) {
                      this.schedules[j].before++
                    }
                    if (signin.weigh_time === 0 && response.data.data.signs.weigh_time > 0) {
                      this.schedules[j].after++
                    }
                  }
                }
              }
              break
            }
          }
          this.weigh_list.id = response.data.data.signs.id
        } else {
          this.$message.error(response.data.msg)
          return false
        }
      })
    }
  },
  watch: {
    'weigh_form.weigh_before': function() {
      this.weigh_form.weight_before = this.weigh_form.weigh_before - this.weigh_form.clothes_weight
      this.weigh_form.dehydrated_weight = this.weigh_form.weigh_before - this.weigh_form.dry_weight - this.weigh_form.clothes_weight
      this.weigh_form.weight_before += ''
      this.weigh_form.dehydrated_weight += ''

      if (this.dialysis_stege === 2) {
        this.weigh_form.weight_reduce_after = this.weigh_form.weigh_before - this.weigh_form.weigh_after
        this.weigh_form.weight_reduce_after += ''
      }
    },
    'weigh_form.weigh_after': function() {
      if (this.dialysis_stege === 2) {
        this.weigh_form.weight_reduce_after = this.weigh_form.weigh_before - this.weigh_form.weigh_after
        this.weigh_form.weight_reduce_after += ''

        this.weigh_form.weight_after = this.weigh_form.weigh_after - this.weigh_form.clothes_weight
        this.weigh_form.weight_after += ''
      }
    },
    'weigh_form.clothes_weight': function() {
      this.weigh_form.weight_before = this.weigh_form.weigh_before - this.weigh_form.clothes_weight
      this.weigh_form.dehydrated_weight = this.weigh_form.weigh_before - this.weigh_form.dry_weight - this.weigh_form.clothes_weight
      this.weigh_form.weight_before += ''
      this.weigh_form.dehydrated_weight += ''

      if (this.dialysis_stege === 2) {
        this.weigh_form.weight_after = this.weigh_form.weigh_after - this.weigh_form.clothes_weight
        this.weigh_form.weight_after += ''
      }
    },
    'weigh_form.dry_weight': function() {
      this.weigh_form.dehydrated_weight = this.weigh_form.weigh_before - this.weigh_form.dry_weight - this.weigh_form.clothes_weight

      this.weigh_form.dehydrated_weight += ''
    },
    'weigh_form.dehydrated_weight': function() {
      if (this.weigh_form.dry_weight === 0) {
        this.weigh_form.dehydrated_percent = ''
      } else {
        var dehydrated_percent = ((this.weigh_form.dehydrated_weight / this.weigh_form.dry_weight) * 100).toFixed(2)
        if (isNaN(dehydrated_percent)) {
          this.weigh_form.dehydrated_percent = ''
        } else {
          this.weigh_form.dehydrated_percent = dehydrated_percent + '%'
        }
      }
    }

  },
  created() {
    this.queryParams.schedule_type = -1
    this.queryParams.need_schedule_type = 1
    this.fetchSignPatients()
  }
}
</script>

<style rel="stylesheet/css" lang="scss" scoped>
.information {
    border: 1px #EBEEF5 solid;
    padding: 30px 20px 30px 20px;

    .border {
        border-bottom: 1px #dcdfe6 solid;
        margin: 0px 0 20px 0;
    }
}
.title {
    background:#409eff;
    height: 44px;
    line-height: 44px;
    padding: 0 0 0 10px;
    color: #fff;
    margin: 0 0 10px 0;
    // border-radius: 4px 4px 0 0;
}

.edit_separater {
    border-top: 1px solid rgb(233, 233, 233);
    margin-top: 15px;
    margin-bottom: 15px;
}

</style>

<style>
.sign-and-weigh-box .sign-and-weigh-box-patients .cell{
    font-size: 14px;
}

.sign-and-weigh-box .sign-and-weigh-box-patients .current-row>td{
    background: #6fb5fa;
}

  .button{
      float: right;
      margin-bottom:0px;
  }

  .inforTitle{
    line-height: 40px;
    height: 50px;
  }

  .el-table th .cell, .el-table td .cell{
    padding: 0!important;
  }
  .el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group {
  border-color: #d0d3da;
}
.el-table--border::after,
.el-table--group::after,
.el-table::before {
  background-color: #d0d3da;
}
</style>
