<template>
  <el-dialog title="双人核对"
             :visible.sync="isVisibility"
             :modal-append-to-body="false"
             width="854px">

    <el-form :inline="true" class="demo-form-inline" :model="doubleReview" label-width="110px">
      <el-form-item label="透析物品核查 : ">
        <el-radio v-model="doubleReview.dialysis_item_check" label="1">正确</el-radio>
        <el-radio v-model="doubleReview.dialysis_item_check" label="2">错误</el-radio>
      </el-form-item>
      <el-form-item label="差错描述 : " label-width="80px">
        <el-input v-model="doubleReview.dialysis_item_desc"></el-input>
      </el-form-item>
    </el-form>
    <el-form :inline="true" class="demo-form-inline" :model="doubleReview" label-width="110px">
      <el-form-item label="透析参数核查 : ">
        <el-radio v-model="doubleReview.dialysis_parameter_check" label="1">正确</el-radio>
        <el-radio v-model="doubleReview.dialysis_parameter_check" label="2">错误</el-radio>
      </el-form-item>
      <el-form-item label="差错描述 : " label-width="80px">
        <el-input v-model="doubleReview.dialysis_parameter_desc"></el-input>
      </el-form-item>
    </el-form>
    <el-form :inline="true" class="demo-form-inline" :model="doubleReview" label-width="110px">
      <el-form-item label="血管通路核查 : ">

        <el-radio v-model="doubleReview.vascular_access_verification" label="1">正确</el-radio>
        <el-radio v-model="doubleReview.vascular_access_verification" label="2">错误</el-radio>

      </el-form-item>
      <el-form-item label="差错描述 : " label-width="80px">
        <el-input v-model="doubleReview.vascular_access_desc"></el-input>
      </el-form-item>
    </el-form>
    <el-form :inline="true" class="demo-form-inline" :model="doubleReview" label-width="110px">
      <el-form-item label="管道连接核查 : ">
        <el-radio v-model="doubleReview.pipeline_connection_check" label="1">正确</el-radio>
        <el-radio v-model="doubleReview.pipeline_connection_check" label="2">错误</el-radio>
      </el-form-item>
      <el-form-item label="差错描述 : " label-width="80px">
        <el-input v-model="doubleReview.pipeline_connection_desc"></el-input>
      </el-form-item>
    </el-form>

    <el-form :inline="true" class="demo-form-inline" :model="doubleReview" label-width="120px">
      <el-form-item label="第一核对时间:">
        <el-time-picker
          v-model="first_check_time"
          format='HH:mm'
          value-format="HH:mm"
          placeholder="请输入核对时间">
        </el-time-picker>
      </el-form-item>

      <el-form-item label="第一核对人员">
        <el-select v-model="doubleReview.creater" placeholder="请选择核对人">
          <el-option v-for="(admin, index) in admin_users" :key="index" :value="admin.id"
                     :label="admin.name"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="第二核对时间:">
        <el-time-picker
          v-model="check_time"
          format='HH:mm'
          value-format="HH:mm"
          placeholder="请输入核对时间">
        </el-time-picker>
      </el-form-item>

      <el-form-item label="第二核对人员">
        <el-select v-model="doubleReview.modifier" placeholder="请选择核对人">
          <el-option v-for="(admin, index) in admin_users" :key="index" :value="admin.id"
                     :label="admin.name"></el-option>
        </el-select>
      </el-form-item>

    </el-form>


    <el-form :inline="true" class="demo-form-inline" :model="doubleReview" label-width="110px">
      <p style="margin-left: 13px;margin-top: 10px" v-if="doubleReview.creater != 0">{{getFirstCheckDesc()}}</p>
      <p style="margin-left: 13px;margin-top: 10px" v-if="doubleReview.modifier != 0">{{getScondCheckDesc()}}</p>
    </el-form>


    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancle">取 消</el-button>
      <el-button type="primary" @click="handleComfirm">保 存</el-button>
    </div>
  </el-dialog>


</template>

<script>
  import { postDoubleCheck } from '@/api/dialysis'
  import { uParseTime } from '@/utils/tools'
  import { parseTime } from '@/utils'

  export default {
    name: 'doubleCheckDialog',
    data() {
      return {
        isVisibility: false,
        time: '',
        isClose: false,
        check_time: 0,
        first_check_time: 0,

        doubleReview: {
          dialysis_item_check: '1',
          dialysis_parameter_check: '1',
          vascular_access_verification: '1',
          pipeline_connection_check: '1',
          dialysis_item_desc: '',
          dialysis_parameter_desc: '',
          vascular_access_desc: '',
          pipeline_connection_desc: '',
          collator: '',

          creater: this.$store.getters.xt_user.user.id,
          modifier: '',
          created_time: 0,
          updated_time: 0,
          check_time: 0,
          first_check_time: 0
        },

        record_date: ''
      }
    }, methods: {
      show() {
        console.log(this.double_check)

        this.isVisibility = true
        if (this.double_check.id > 0) {

          this.first_check_time = uParseTime(
            this.double_check.first_check_time,
            '{y}-{m}-{d} {h}:{i}'
          ).split(' ')[1]


          if(this.$store.getters.xt_user.user.id == this.double_check.creater || this.double_check.modifier > 0){
            if(this.double_check.modifier > 0){
                this.check_time = uParseTime(
                this.doubleReview.check_time,
                  '{y}-{m}-{d} {h}:{i}'
                ).split(' ')[1]

                console.log('真性感')
            }

          }else if(this.$store.getters.xt_user.user.id != this.double_check.creater && this.double_check.modifier == 0){
            this.check_time = uParseTime(
              new Date(),
              '{y}-{m}-{d} {h}:{i}'
            ).split(' ')[1]
            this.doubleReview.modifier = this.$store.getters.xt_user.user.id
          }
          if(this.doubleReview.modifier == 0){
            this.doubleReview.modifier = ""
          }
          console.log( this.first_check_time )
          console.log( this.check_time )


        } else {
          var nowDate = new Date()
          var nowHours = nowDate.getHours()
          var nowMinutes = nowDate.getMinutes()
          if(this.$store.getters.xt_user.user.id != this.double_check.creater && this.double_check.modifier == 0){
            this.check_time = (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)
          }
          this.first_check_time = (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)
          console.log( this.first_check_time )
          console.log( this.check_time )

        }
      },
      hide() {
        this.isVisibility = false
      },
      handleCancle: function() {
        this.isVisibility = false

      }, handleComfirm: function() {

        if (this.patient.id <= 0) {
          this.$message.error('没有选择患者')
          return
        }
        var first_check_time = this.record_date + ' ' + this.first_check_time
        var check_time = this.record_date + ' ' + this.check_time

        var first_time = new Date(first_check_time).getTime()
        var second_time = new Date(check_time).getTime()




        if (first_time >= second_time) {
          this.$message.error('第一次核对时间不能大于或者等于第二次核对时间')
          return
        }

        if (this.doubleReview.creater == this.doubleReview.modifier) {
          this.$message.error('第一次核对人不能与第二次核对人相同')
          return
        }

        let ParamsQuery = this.doubleReview
        ParamsQuery['patient'] = this.patient.id
        ParamsQuery['record_date'] = this.record_date
        if(this.check_time){
          ParamsQuery['check_time'] = this.record_date + ' ' + this.check_time
        }
        // ParamsQuery['check_time'] = this.record_date + ' ' + this.check_time
        if(this.first_check_time){
          ParamsQuery['first_check_time'] = this.record_date + ' ' + this.first_check_time
        }
        
        ParamsQuery['creater'] = this.doubleReview.creater
        ParamsQuery['modifier'] = this.doubleReview.modifier


        postDoubleCheck(ParamsQuery).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success',
              duration: 2000
            })

            this.doubleReview.creater = response.data.data.doubleCheck.creater
            this.doubleReview.check_time =
              response.data.data.doubleCheck.check_time
            this.doubleReview.first_check_time =
              response.data.data.doubleCheck.first_check_time
            this.doubleReview.modifier = response.data.data.doubleCheck.modifier

            this.first_check_time = uParseTime(
              this.doubleReview.first_check_time,
              '{y}-{m}-{d}  {h}:{i}'
            ).split(' ')[1]

            // if(this.$store.getters.xt_user.user.id != this.double_check.creater && this.double_check.modifier == 0){
            //   this.check_time = uParseTime(
            //     this.doubleReview.check_time,
            //     '{y}-{m}-{d}  {h}:{i}'
            //   ).split(' ')[1]
            // }

            let doublecheck_resp = response.data.data.doubleCheck
            var doubleCheck = this.double_check
            for (var index in doublecheck_resp) {
              // doubleCheck[index] = doublecheck_resp[index];
              this.$set(doubleCheck, index, doublecheck_resp[index])
            }
            this.hide()
          }
        })

      },
      getFirstCheckDesc: function () {
        if(this.double_check.creater > 0){
          if (
            this.doubleReview.first_check_time > 0 &&
            this.doubleReview.check_time > 0
          ) {
            if (this.doubleReview.first_check_time < this.doubleReview.check_time) {
              let time = uParseTime(
                this.doubleReview.first_check_time,
                "{y}-{m}-{d}  {h}:{i}:{s}"
              );
              if (this.doubleReview.creater <= 0) {
                return "";
              }

              var desc = "";
              if (
                this.admin_users == null ||
                typeof this.admin_users.length == "undefined"
              ) {
                return "";
              }

              var leng = this.admin_users.length;
              if (leng == 0) {
                return "";
              }

              for (let index = 0; index < leng; index++) {
                if (this.admin_users[index].id == this.doubleReview.creater) {
                  let name = this.admin_users[index].name;
                  desc = "第一核对人员:" + name + " 第一核对时间:" + time;

                  break;
                }
              }
              return desc;
            } else {
              let time = uParseTime(
                this.doubleReview.check_time,
                "{y}-{m}-{d}  {h}:{i}:{s}"
              );
              if (this.doubleReview.modifier <= 0) {
                return "";
              }

              var desc = "";
              if (
                this.admin_users == null ||
                typeof this.admin_users.length == "undefined"
              ) {
                return "";
              }

              var leng = this.admin_users.length;
              if (leng == 0) {
                return "";
              }

              for (let index = 0; index < leng; index++) {
                if (this.admin_users[index].id == this.doubleReview.modifier) {
                  let name = this.admin_users[index].name;
                  desc = "第一核对人员:" + name + " 第一核对时间:" + time;

                  break;
                }
              }
              return desc;
            }
          } else {
            let time = uParseTime(
              this.doubleReview.first_check_time,
              "{y}-{m}-{d}  {h}:{i}:{s}"
            );
            if (this.doubleReview.creater <= 0) {
              return "";
            }

            var desc = "";
            if (
              this.admin_users == null ||
              typeof this.admin_users.length == "undefined"
            ) {
              return "";
            }

            var leng = this.admin_users.length;
            if (leng == 0) {
              return "";
            }

            for (let index = 0; index < leng; index++) {
              if (this.admin_users[index].id == this.doubleReview.creater) {
                let name = this.admin_users[index].name;
                desc = "第一核对人员:" + name + " 第一核对时间:" + time;

                break;
              }
            }
            return desc;
          }
        }
      },
      getScondCheckDesc: function () {
        if(this.double_check.modifier > 0){

          if (
            this.doubleReview.first_check_time > 0 &&
            this.doubleReview.check_time > 0
          ) {
            if (this.doubleReview.first_check_time < this.doubleReview.check_time) {
              let time = uParseTime(
                this.doubleReview.check_time,
                "{y}-{m}-{d} {h}:{i}:{s}"
              );
              if (this.doubleReview.modifier <= 0) {
                return "";
              }
              var desc = "";
              if (
                this.admin_users == null ||
                typeof this.admin_users.length == "undefined"
              ) {
                return desc;
              }
              var leng = this.admin_users.length;
              if (leng == 0) {
                return desc;
              }
              for (let index = 0; index < leng; index++) {
                if (this.admin_users[index].id == this.doubleReview.modifier ) {
                  let name = this.admin_users[index].name;
                  desc = "第二核对人员:" + name + " 第二核对时间:" + time;
                  break;
                }
              }
              return desc;
            } else {
              let time = uParseTime(
                this.doubleReview.first_check_time,
                "{y}-{m}-{d} {h}:{i}:{s}"
              );
              if (this.doubleReview.creater <= 0) {
                return "";
              }
              var desc = "";
              if (
                this.admin_users == null ||
                typeof this.admin_users.length == "undefined"
              ) {
                return desc;
              }
              var leng = this.admin_users.length;
              if (leng == 0) {
                return desc;
              }
              for (let index = 0; index < leng; index++) {
                if (this.admin_users[index].id == this.doubleReview.creater) {
                  let name = this.admin_users[index].name;
                  desc = "第二核对人员:" + name + " 第二核对时间:" + time;
                  break;
                }
              }
              return desc;
            }
          } else {
            let time = uParseTime(
              this.doubleReview.check_time,
              "{y}-{m}-{d} {h}:{i}:{s}"
            );
            if (this.doubleReview.modifier <= 0) {
              return "";
            }
            var desc = "";
            if (
              this.admin_users == null ||
              typeof this.admin_users.length == "undefined"
            ) {
              return desc;
            }
            var leng = this.admin_users.length;
            if (leng == 0) {
              return desc;
            }
            for (let index = 0; index < leng; index++) {
              if (this.admin_users[index].id == this.doubleReview.modifier) {
                let name = this.admin_users[index].name;
                desc = "第二核对人员:" + name + " 第二核对时间:" + time;
                break;
              }
            }
            return desc;
          }
        }
      },
    },

    props: {
      admin_users: { //系统用户列表
        type: Array,
        default: () => {
          return []
        }
      },

      double_check: { // 双人核对
        type: Object,
        default: () => {
          return { id: 0 }
        }
      },
      patient: { // 患者信息
        type: Object,
        default: () => {
          return { id: 0 }
        }
      }

    },
    watch: {
      isVisibility(val) {

      }, 'double_check.id': function() {
        if (this.double_check.id > 0) {
          for (var index in this.doubleReview) {
            this.doubleReview[index] = this.double_check[index] + ''
          }
          this.doubleReview.created_time = this.double_check['created_time']
          this.doubleReview.creater = this.double_check['creater']
          this.doubleReview.updated_time = this.double_check['updated_time']
          this.doubleReview.modifier = this.double_check['modifier']

          this.doubleReview.dialysis_item_desc = this.double_check['dialysis_item_desc']
          this.doubleReview.dialysis_parameter_desc = this.double_check['dialysis_parameter_desc']
          this.doubleReview.vascular_access_desc = this.double_check['vascular_access_desc']
          this.doubleReview.pipeline_connection_desc = this.double_check['pipeline_connection_desc']

          this.doubleReview.created_time = this.double_check['created_time']
          this.doubleReview.creater = this.double_check['creater']
          this.doubleReview.updated_time = this.double_check['updated_time']
          this.doubleReview.modifier = this.double_check['modifier']
          this.doubleReview.check_time = this.double_check['check_time']
          this.doubleReview.first_check_time = this.double_check['first_check_time']

          this.first_check_time = uParseTime(
            this.doubleReview.first_check_time,
            '{y}-{m}-{d}  {h}:{i}'
          ).split(' ')[1]

          if(this.$store.getters.xt_user.user.id == this.double_check.creater || this.double_check.modifier > 0){
            this.check_time = uParseTime(
              this.doubleReview.check_time,
              '{y}-{m}-{d}  {h}:{i}'
            ).split(' ')[1]
          }else if(this.$store.getters.xt_user.user.id != this.double_check.creater && this.double_check.modifier == 0){
            this.check_time = uParseTime(
              new Date(),
              '{y}-{m}-{d}  {h}:{i}'
            ).split(' ')[1]
            this.doubleReview.modifier = this.$store.getters.xt_user.user.id
          }

        } else {
          this.dialysis_item_check = '1'
          this.dialysis_parameter_check = '1'
          this.vascular_access_verification = '1'
          this.pipeline_connection_check = '1'

          this.doubleReview.dialysis_item_desc = ''
          this.doubleReview.dialysis_parameter_desc = ''
          this.doubleReview.vascular_access_desc = ''
          this.doubleReview.pipeline_connection_desc = ''

          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()
          if(this.$store.getters.xt_user.user.id != this.double_check.creater && this.double_check.modifier == 0){
            this.check_time = (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)
          }
          this.first_check_time = (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)

        }
      }

    },
    created() {
      var date = this.$route.query && this.$route.query.date
      this.record_date = uParseTime(date, '{y}-{m}-{d}')

      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()
      if(this.$store.getters.xt_user.user.id != this.double_check.creater && this.double_check.modifier == 0){
        this.check_time = (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)
      }
      this.first_check_time = (nowHours < 10 ? '0' + nowHours : nowHours) + ':' + (nowMinutes < 10 ? '0' + nowMinutes : nowMinutes)

    }

  }
</script>

<style scoped>

</style>
