<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="schedual_type" placeholder="请选择班次" @change="changeSchedualType">
            <el-option v-for="(item, index) in schedules_type" :key="index" :value="item.id"
                       :label="item.name"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="上机床位">
          <el-select v-model="form.bed_id" placeholder="请选择上机床位">
            <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="请选择上机护士">
            <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="请选择穿刺护士">
            <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.blood_drawing" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item label="上机时间 :" style="width:300px">
          <el-date-picker
            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-button
            v-if="dialysis_order.id > 0 && (isPremission || this.$store.getters.xt_user.user.id == this.creator)"
            type="primary" @click="editOrder">修改上机
          </el-button>

        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

</template>

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

  export default {
    name: 'ComputerDialog',
    data() {
      return {
        zone_beds: [],
        visible: false,
        loading: false,
        template_id: 0,
        patient_id: 0,
        schedule_date: 0,
        start_time: 0,
        creator: 0,
        form: {
          bed_id: '',
          nurse_id: '',
          start_time: '',
          puncture_nurse_id: '',
          blood_drawing: 100
        },
        schedual_type: 0,
        schedules_type: [
          { id: 1, name: '上午' },
          { id: 2, name: '下午' },
          { id: 3, name: '晚上' }
        ], // 该排班的区里的床位
        isPremission: false
      }
    },
    props: {
      dialysis_order: {
        type: Object
      },
      schedule: {
        type: Object
      },
      admins: {
        type: Array
      },
      device_numbers: {
        type: Array
      },
      special_premission: {
        type: Array
      }
    }, mounted() {

    },
    created() {

      this.template_id = this.$store.getters.xt_user.template_info.template_id
      this.patient_id = this.$route.query.patient_id
      this.schedule_date = this.$route.query.date
      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
      if (this.form.puncture_nurse_id == 0) {
        this.form.puncture_nurse_id = this.$store.getters.xt_user.user.id
      }

    },
    watch: {
      'schedule.id': function() {
      },
      'dialysis_order.id': function() {

        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: {},
    methods: {
      changeSchedualType: function(schedual_type) {
        let ParamsQuery = {}
        ParamsQuery['schedual_type'] = schedual_type
        GetSchedualNumber(ParamsQuery).then(response => {
          if (response.data.state == 0) {
            return false
          } else {
            this.temp_device_numbers = response.data.data.number
            for (let index = 0; index < this.temp_device_numbers.length; index++) {
              const device_number = this.temp_device_numbers[index]
              this.temp_device_numbers[index]['number'] = device_number['zone_name'] + '-' + device_number['number']
            }
            this.zone_beds = this.temp_device_numbers
            this.form.bed_id = this.zone_beds[0].id

          }
        })

      },
      GetSchedualNumber: function() {
        let ParamsQuery = {}
        ParamsQuery['schedual_type'] = this.schedual_type
        GetSchedualNumber(ParamsQuery).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.temp_device_numbers = response.data.data.number
            for (let index = 0; index < this.temp_device_numbers.length; index++) {
              const device_number = this.temp_device_numbers[index]
              this.temp_device_numbers[index]['number'] = device_number['zone_name'] + '-' + device_number['number']
            }
            this.zone_beds = this.temp_device_numbers

            this.form.bed_id = this.dialysis_order.id == 0 ? this.schedule.bed_id : this.dialysis_order.bed_id
            if (this.dialysis_order.id == 0) {
              let isFilter = true
              for (let i = 0; i < this.zone_beds.length; i++) {
                if (this.zone_beds[i].id == this.schedule.bed_id) {
                  isFilter = false

                }
              }
              if (isFilter) {
                this.form.bed_id = this.zone_beds[0].id
              }
            } else {
              for (let i = 0; i < this.device_numbers.length; i++) {
                if (this.device_numbers[i].id == this.dialysis_order.bed_id) {
                  let obj = {}
                  obj = this.device_numbers[i]
                  if (obj['number'].indexOf(this.device_numbers[i]['zone_name']) == -1) {
                    obj['number'] = this.device_numbers[i]['zone_name'] + '-' + this.device_numbers[i]['number']
                  }
                  this.zone_beds.unshift(obj)
                  this.zone_beds.sort((a, b) => a.id - b.id)
                }
              }
            }
          }
        })
      },
      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)
        }

        if (this.dialysis_order.id == 0) {
          let now = new Date()
          let hour = now.getHours()
          if (hour >= 6 && hour < 12) {
            this.schedual_type = 1

          } else if (hour >= 12 && hour < 18) {

            this.schedual_type = 2

          } else if (hour >= 18) {
            this.schedual_type = 3
          }
        } else {

          this.schedual_type = this.dialysis_order.schedual_type
        }

        if (this.dialysis_order.id > 0) {
          for (let i = 0; i < this.special_premission.length; i++) {
            if (this.$store.getters.xt_user.user.id == this.special_premission[i].admin_user_id) {
              this.isPremission = true
            }
          }
        }

        if (this.dialysis_order.id > 0) {
          if (this.dialysis_order.creator == 0) {
            this.creator = this.dialysis_order.start_nurse
          } else {
            this.creator = this.dialysis_order.creator
          }
        }

        this.GetSchedualNumber()
      },
      hide: function() {
        this.visible = false
      },
      submit: function() {
        if (this.form.start_time == '' || this.form.start_time == null) {
          this.$message.error('开始时间不能为空')
          return
        }
        console.log(this.dialysis_order)
        this.loading = true
        let mode = '1'

        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, this.schedual_type, mode).then(rs => {
          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])
            }
            this.hide()
            this.$emit('monitor', resp.data.monitor)
            this.$message.success('上机成功')
          } else {
            this.$message.error(resp.msg)
          }
        })
      }, editOrder() {
        let ParamsQuery = {}
        ParamsQuery['schedual_type'] = this.schedual_type
        ParamsQuery['id'] = this.dialysis_order.id
        ParamsQuery['nurse'] = this.form.nurse_id
        ParamsQuery['bed'] = this.form.bed_id
        ParamsQuery['start_time'] = this.form.start_time
        ParamsQuery['puncture_nurse'] = this.form.puncture_nurse_id

        ParamsQuery['mode'] = "2"
        if (this.dialysis_order.creator != this.$store.getters.xt_user.user.id) {
          ParamsQuery['mode'] = "3"
        }
        PostModifyStartDialysis(ParamsQuery).then(rs => {
          var resp = rs.data
          if (resp.state == 1) {
            this.$message.success('修改成功')

            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])
              this.$emit('assessmentAfterDislysis', resp.data.after)

            }

          } else {
            this.$message.error(resp.msg)
          }
        })
      }

    }
  }
</script>

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