<template>
  <div>
    <el-dialog title="透析上机" :visible.sync="visible" width="854px" :modal-append-to-body="false">
      <el-form :model="form" label-width="100px">
        <el-form-item label="上机床位">
          <el-select v-model="form.bed_id" placeholder="" :disabled="dialysis_order.id != 0">
            <el-option v-for="(bed, index) in zone_beds" :key="index" :value="bed.id" :label="bed.number"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上机护士">
          <el-select v-model="form.nurse_id" placeholder="" :disabled="dialysis_order.id != 0">
            <el-option v-for="(admin, index) in admins" :key="index" :value="admin.id" :label="admin.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="穿刺护士">
          <el-select v-model="form.puncture_nurse_id" placeholder="" :disabled="dialysis_order.id != 0">
            <el-option v-for="(admin, index) in admins" :key="index" :value="admin.id" :label="admin.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="引血(ml/min)" v-if="template_id == 6" >
          <el-input type="number" v-model="form.lood_drawing" style="width:200px;"></el-input>
        </el-form-item>
    
        <el-form-item label="上机时间 :" style="width:300px">
          <el-date-picker
            :disabled="dialysis_order.id != 0"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="选择时间"
            v-model="form.start_time"
            style="width:100%;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button v-if="dialysis_order.id == 0" @click="submit" type="primary" :loading="loading">执行上机</el-button>
          <el-button v-else type="info" :disabled="true">已上机</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

</template>

<script>
  import { startDialysis } from '@/api/dialysis_record'
  import { parseTime } from '@/utils'

  export default {
    name: 'ComputerDialog',
    data() {
      return {
        visible: false,
        loading: false,
        template_id: 0,
        patient_id: 0,
        schedule_date: 0,
        start_time: 0,
        form: {
          bed_id: '',
          nurse_id: '',
          start_time: '',
          puncture_nurse_id: ''
          lood_drawing:100
        }
      }
    },
    props: {
      dialysis_order: {
        type: Object
      },
      schedule: {
        type: Object
      },
      admins: {
        type: Array
      },
      device_numbers: {
        type: Array
      }
    },
    created() {
<<<<<<< .mine


=======
      
      this.template_id = this.$store.getters.xt_user.template_info.template_id
>>>>>>> .theirs
      this.patient_id = this.$route.query.patient_id
      this.schedule_date = this.$route.query.date

      this.form.bed_id = this.dialysis_order.id == 0 ? this.schedule.bed_id : this.dialysis_order.bed_id
      this.form.nurse_id = this.dialysis_order.id == 0 ? this.$store.getters.xt_user.user.id : this.dialysis_order.start_nurse
      this.form.puncture_nurse_id = this.dialysis_order.id == 0 ? this.$store.getters.xt_user.user.id : this.dialysis_order.puncture_nurse
      this.form.lood_drawing = 0 

      if (this.form.puncture_nurse_id == 0) {
        this.form.puncture_nurse_id = this.$store.getters.xt_user.user.id
      }
    },
    watch: {
      'schedule.id': function() {
        this.form.bed_id = this.dialysis_order.id == 0 ? this.schedule.bed_id : this.dialysis_order.bed_id
      },
      'dialysis_order.id': function() {
        console.log(this.dialysis_order)
        this.form.bed_id = this.dialysis_order.id == 0 ? this.schedule.bed_id : this.dialysis_order.bed_id
        this.form.nurse_id = this.dialysis_order.id == 0 ? this.$store.getters.xt_user.user.id : this.dialysis_order.start_nurse
        this.form.puncture_nurse_id = this.dialysis_order.id == 0 ? this.$store.getters.xt_user.user.id : this.dialysis_order.puncture_nurse

        var nowDate = new Date()
        var nowYear = nowDate.getFullYear()
        var nowMonth = nowDate.getMonth() + 1
        var nowDay = nowDate.getDate()
        var nowHours = nowDate.getHours()
        var nowMinutes = nowDate.getMinutes()
        var nowSeconds = nowDate.getSeconds()
        var time =
          nowYear +
          '-' +
          (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
          '-' +
          (nowDay < 10 ? '0' + nowDay : nowDay) + ' ' + (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)
        this.form.start_time = this.dialysis_order.id == 0 ? time : this.getTime(this.dialysis_order.start_time, '{y}-{m}-{d} {h}:{i}')
      }
    },
    computed: {
      zone_beds: function() {
        var beds = []
        for (let index = 0; index < this.device_numbers.length; index++) {
          const device_number = this.device_numbers[index]
          if (device_number.zone_id == this.schedule.partition_id) {
            beds.push(device_number)
          }
        }
        return beds
      }
    },
    methods: {
      getTime(value, temp) {
        if (value != undefined) {
          return parseTime(value, temp)
        }
        return ''
      },
      show: function() {
        this.visible = true

        var nowDate = new Date()
        var nowYear = nowDate.getFullYear()
        var nowMonth = nowDate.getMonth() + 1
        var nowDay = nowDate.getDate()
        var nowHours = nowDate.getHours()
        var nowMinutes = nowDate.getMinutes()
        var nowSeconds = nowDate.getSeconds()

        if (this.dialysis_order.id != 0) {
          this.form.start_time = this.getTime(this.dialysis_order.start_time, '{y}-{m}-{d} {h}:{i}')
        } else {
          this.form.start_time =
            nowYear +
            '-' +
            (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
            '-' +
            (nowDay < 10 ? '0' + nowDay : nowDay) + ' ' + (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)
        }
      },
      hide: function() {
        this.visible = false
      },
      submit: function() {
        this.loading = true
<<<<<<< .mine
        startDialysis(this.patient_id, parseTime(this.schedule_date, '{y}-{m}-{d}'), this.form.nurse_id, this.form.bed_id, this.form.puncture_nurse_id, this.form.start_time).then(rs => {
=======
        startDialysis(this.patient_id, parseTime(this.schedule_date, '{y}-{m}-{d}'), this.form.nurse_id, this.form.bed_id,this.form.lood_drawing, this.form.puncture_nurse_id,this.form.start_time).then(rs => {
>>>>>>> .theirs
          this.loading = false
          var resp = rs.data
          if (resp.state == 1) {
            var resp_dialysis_order = resp.data.dialysis_order
            var this_order = this.dialysis_order
            for (const key in resp_dialysis_order) {
              this.$set(this_order, key, resp_dialysis_order[key])
            }
          } else {
            this.$message.error(resp.msg)
          }
        })
      }
    }
  }
</script>

<style scoped>
  .txsj {
    text-align: center;
    margin-bottom: 20px;
  }
</style>
