<template>
  <div>
    <el-dialog title="透析下机" :visible.sync="visible" width="854px" :modal-append-to-body="false">
      <div class="txsj">
      </div>
      <el-form :model="form" label-width="80px">
        <el-form-item label="下机护士">
          <el-select v-model="form.nurse_id" :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="下机时间 :" style="width:275px">
          <el-date-picker
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="选择时间"
            v-model="end_time"
            style="width:100%;"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button v-if="(dialysis_order.id != 0 && dialysis_order.stage == 1)" @click="submit" :loading="loading"
                     type="primary">执行下机
          </el-button>
          <!--<el-button v-else-if="(dialysis_order.id != 0 && dialysis_order.stage == 2)" :disabled="true" type="info">-->
            <!--已下机-->
          <!--</el-button>-->
          <!--<el-button v-else :disabled="true" type="info">未上机</el-button>-->
          <el-button
            v-if="dialysis_order.stage == 2 "
            type="primary" @click="modifyFinish">修改下机
          </el-button>


        </el-form-item>

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

<script>
  import { finishDialysis,PostModifyFinishDialysis } from '@/api/dialysis_record'
  import { parseTime } from '@/utils'

  export default {
    name: 'FinishDialog',
    data() {
      return {
        visible: false,
        loading: false,
        creator: 0,
        patient_id: 0,
        schedule_date: 0,
        isPremission:false,
        end_time: '',

        form: {
          nurse_id: 0
        }
      }
    },
    props: {
      prescription: { // 透析处方
        type: Object,
      },
      dialysis_order: {
        type: Object
      },
      schedule: {
        type: Object
      },
      admins: {
        type: Array
      }, special_premission: {
        type: Array,
      },
    },
    created() {

    },
    watch: {
      'dialysis_order.id': function() {
        if (this.dialysis_order.id == 0) {
          this.form.nurse_id = 0
        } else if (this.dialysis_order.stage == 1) {
          this.form.nurse_id = this.$store.getters.xt_user.user.id
        } else {
          this.form.nurse_id = this.dialysis_order.finish_nurse
        }
      }
    },
    methods: {
      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 time =
          nowYear +
          '-' +
          (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
          '-' +
          (nowDay < 10 ? '0' + nowDay : nowDay) + ' ' + (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)
        this.patient_id = this.$route.query.patient_id
        this.schedule_date = this.$route.query.date

        if (this.dialysis_order.id == 0) {
          this.form.nurse_id = 0
        } else if (this.dialysis_order.stage == 1) {
          this.form.nurse_id = this.$store.getters.xt_user.user.id
        } else {
          this.form.nurse_id = this.dialysis_order.finish_nurse
        }


        if (this.dialysis_order.finish_creator > 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.finish_creator > 0) {
            this.creator = this.dialysis_order.finish_creator
        }


        if (this.dialysis_order.id == "") {
          //没有上下机记录
          this.end_time = this.dialysis_order.finish_creator == 0 ? time : this.getTime(this.dialysis_order.end_time, '{y}-{m}-{d} {h}:{i}')

        } else {
          if (this.dialysis_order.start_time > 0) {
            if (this.prescription.id != "") {
              let endTime = 0;
              endTime =
                this.dialysis_order.start_time +
                this.prescription.dialysis_duration_hour * 3600 +
                this.prescription.dialysis_duration_minute * 60;
              this.end_time = parseTime(endTime, "{y}-{m}-{d} {h}:{i}");
            } else {
              this.end_time =
                parseTime(Date.parse(new Date()) / 1000, "{y}-{m}-{d} {h}:{i}")
            }
            if (this.dialysis_order.end_time > 0) {
              this.end_time =
                parseTime(this.dialysis_order.end_time, "{y}-{m}-{d} {h}:{i}");
            }
          } else {
            this.end_time = this.dialysis_order.finish_creator == 0 ? time : this.getTime(this.dialysis_order.end_time, '{y}-{m}-{d} {h}:{i}')

          }
        }


      },
      hide: function() {
        this.visible = false
      },modifyFinish:function(){
        let ParamsQuery = {};
        ParamsQuery["id"] = this.dialysis_order.id;
        ParamsQuery["nurse"] = this.form.nurse_id;
        ParamsQuery["end_time"] = this.end_time;
        ParamsQuery["mode"] = "2"
        if(this.dialysis_order.finish_creator  != this.$store.getters.xt_user.user.id){
          ParamsQuery["mode"] = "3"
        }
        PostModifyFinishDialysis(ParamsQuery).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)

            return false;
          } else {
            this.$message.success("修改成功")
            var record = this.dialysis_order;
            for (const key in response.data.data.dialysis_order) {
              this.$set(record, key, response.data.data.dialysis_order[key]);
            }
            console.log(response.data.data.after)
            this.$emit('assessmentAfterDislysis', response.data.data.after)
          }
        });


      },
      submit: function() {
        this.loading = true
        let mode = "1"
        finishDialysis(this.patient_id, parseTime(this.schedule_date, '{y}-{m}-{d}'), this.end_time, this.form.nurse_id,mode).then(rs => {
          this.loading = false
          var resp = rs.data
          if (resp.state == 1) {
            var dialysis_order = resp.data.dialysis_order
            var this_order = this.dialysis_order
            for (const key in dialysis_order) {
              this.$set(this_order, key, dialysis_order[key])
            }
            this.hide()
            this.$emit('assessmentAfterDislysis', resp.data.assessmentAfterDislysis)

          } else {
            this.$message.error(resp.msg)
          }
        })
      }, getTime(value, temp) {
        if (value != undefined) {
          return parseTime(value, temp)
        }
        return ''
      }
    }
  }
</script>


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