<template>
  <div>
    <div class="Dialog">
      <div class="DialogTit">
        <span @click="close()" class="iconfont">&#xe6e9;</span>
        <h1 class="name">双人查对</h1>
        <span @click="commitInfo" class="success">保存</span>
      </div>
      <div style="height: 8rem" class="DialogContent choose">
        <el-form :inline="true" class="demo-form-inline" :model="doubleReview" label-width="160px">
          <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="160px">
            <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="160px">
          <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="160px">
            <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="160px">
          <el-form-item label="血管通路核查 : ">
            <!--<el-radio-group v-model="form.appetite">-->
            <!--<el-radio label="正确"></el-radio>-->
            <!--<el-radio label="整改"></el-radio>-->
            <!--</el-radio-group>-->
            <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="160px">
            <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="160px">
          <el-form-item label="管道连接核查 : ">
            <!--<el-radio-group v-model="form.condition">-->
            <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-radio-group>-->
          </el-form-item>
          <el-form-item label="差错描述 : " label-width="160px">
            <el-input v-model="doubleReview.pipeline_connection_desc"></el-input>
          </el-form-item>
        </el-form>

        <div class="item">
          <h2 class="name">核对时间</h2>
          <div class="content">
            <span
              class="text"
              style="width: 160px"
              @click="selectCheckTimeAction"
            >{{ check_time_str }}</span>
            <span class="iconfont">&#xe6f9;</span>
          </div>
        </div>

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

        <!-- <div class="button">
          <button @click="commitInfo" class="submitButton">提交</button>
        </div>-->
        <!--<div class="footer">-->
        <!--处方医生：黄梦燕  医生-->
        <!--</div>-->
      </div>
      <!-- <div class="footer">
          处方医生：黄梦燕  护士
      </div>-->
    </div>
    <mt-datetime-picker
      ref="start_time_picker"
      type="datetime"
      @confirm="didSelectCheckTime"
      v-model="check_time"
    ></mt-datetime-picker>
  </div>
</template>

<script>
import { commitDoubleCheck } from "@/api/dialysis";
import { Toast } from "vant";
import { uParseTime } from "@/utils/tools";
import { parseTime } from "@/utils";

export default {
  name: "DoubleDialog",
  data() {
    return {
      admin_users: [],
      first_check_isShow: false,
      second_check_isShow: false,
      first_check_desc: "",
      second_check_desc: "",
      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: 0,
        modifier: 0,
        created_time: 0,
        updated_time: 0,
        check_time: 0,
        first_check_time: 0
      },
      check_time: new Date(),
      check_time_str: "",
      patient: {
        id: 0
      },
      doctor: "",
      record_date: ""
    };
  },
  props: {
    record: {
      type: Object,
      default: () => {
        return { id: 0 };
      }
    },

    patient_prop: {
      type: Object
    },
    admin_users_prop: {
      type: Array
    }
  },
  created() {
    console.log(new Date());
    this.admin_users = this.admin_users_prop;
    if (this.record.id == "") {
      this.doubleReview.dialysis_item_check = "1";
      this.doubleReview.dialysis_parameter_check = "1";
      this.doubleReview.vascular_access_verification = "1";
      this.doubleReview.pipeline_connection_check = "1";
      this.doubleReview.dialysis_item_desc = "";
      this.doubleReview.dialysis_parameter_desc = "";
      this.doubleReview.vascular_access_desc = "";
      this.doubleReview.pipeline_connection_desc = "";
    } else {
      this.doubleReview.dialysis_item_check =
        this.record.dialysis_item_check + "";
      this.doubleReview.dialysis_parameter_check =
        this.record.dialysis_parameter_check + "";
      this.doubleReview.vascular_access_verification =
        this.record.vascular_access_verification + "";
      this.doubleReview.pipeline_connection_check =
        this.record.pipeline_connection_check + "";
      this.doubleReview.dialysis_item_desc = this.record.dialysis_item_desc;
      this.doubleReview.dialysis_parameter_desc = this.record.dialysis_parameter_desc;
      this.doubleReview.vascular_access_desc = this.record.vascular_access_desc;
      this.doubleReview.pipeline_connection_desc = this.record.pipeline_connection_desc;

      this.doubleReview.created_time = this.record.created_time;
      this.doubleReview.creater = this.record.creater;
      this.doubleReview.updated_time = this.record.updated_time;
      this.doubleReview.modifier = this.record.modifier;
      this.doubleReview.check_time = this.record.check_time;
      this.doubleReview.first_check_time = this.record.first_check_time;
    }

    var date = this.$route.query && this.$route.query.date;
    date *= 1000;
    var newDate = new Date(date);
    var y = newDate.getFullYear();
    var m = newDate.getMonth() + 1;
    var d = newDate.getDate();
    if (isNaN(y) || isNaN(m) || isNaN(d)) {
      newDate = new Date();
      y = newDate.getFullYear();
      m = newDate.getMonth() + 1;
      d = newDate.getDate();
    }
    this.record_date =
      y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
    // this.patient.id = this.patient_prop.id
  },
  methods: {
    didSelectCheckTime: function(time) {
      this.check_time_str = parseTime(time, "{y}-{m}-{d} {h}:{i}" + ":00");
    },
    selectCheckTimeAction: function() {
      this.$refs.start_time_picker.open();
    },
    checkInf: function() {},
    // initData(doubleReview, patient, doctor) {
    //   //上层传值
    //   this.patient = patient;
    //   this.doubleReview = doubleReview;
    //   this.doctor = doctor;
    // },
    commitInfo: function() {
      Toast.loading({ forbidClick: true, duration: 0 });
      let ParamsQuery = this.doubleReview;
      // ParamsQuery["patient"] = this.patient.id
      ParamsQuery["patient"] = this.$route.query.patient_id;
      ParamsQuery["record_date"] = this.record_date;
      ParamsQuery["check_time"] = this.check_time_str;
      commitDoubleCheck(ParamsQuery).then(response => {
        if (response.data.state == 0) {
          // this.$toast({message: response.data.msg});
          Toast.fail(response.data.msg);
          return false;
        } else {
          Toast.success("提交成功");

          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;

          for (const key in response.data.data.doubleCheck) {
            this.record[key] = response.data.data.doubleCheck[key];
          }
          this.$emit("did_update", response.data.data.doubleCheck);
        }
      });
    },
    close: function() {
      this.$emit("close");
    },
    getFirstCheckDesc: function() {
      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}"
          );
          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}"
          );
          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}"
        );
        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.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}"
          );
          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}"
          );
          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}"
        );
        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;
      }
    },
    getDate: function(val) {
      return parseTime(val, "{y}-{m}-{d} {h}:{i}");
    }
  },
  mounted() {
    if (this.record.id == 0) {
      this.check_time_str = parseTime(
        this.check_time,
        "{y}-{m}-{d} {h}:{i}" + ":00"
      );
    } else {
      if (this.record.modifier == 0) {
        this.check_time_str = parseTime(
          this.check_time,
          "{y}-{m}-{d} {h}:{i}" + ":00"
        );
      } else {
        this.check_time_str = parseTime(
          this.record.check_time,
          "{y}-{m}-{d} {h}:{i}" + ":00"
        );
      }
    }
  }
};
</script>

<style style="stylesheet/scss" lang="scss" scoped>
.choose {
  .demo-form-inline {
    border-bottom: 1px #e5e5e5 solid;
    .el-form-item {
      margin-bottom: 0;
      padding: 0.2rem 0.2rem;
      margin-right: 0;
    }
    p {
      color: $title-color;
    }
  }

  .button {
    text-align: center;
    margin-top: 1rem;
    .submitButton {
      width: 3rem;
      height: 0.8rem;
      line-height: 0.8rem;
      background: $main-color;
      color: #fff;
      font-size: 0.3rem;
      text-align: center;
      border-radius: 6px;
    }
  }
}
</style>
<style lang="scss">
.el-radio__inner {
  @media only screen and (min-width: 768px) {
    width: 20px;
    height: 20px;
  }
}
.el-radio__input {
  @media only screen and (min-width: 768px) {
    margin-top: -6px;
  }
}
</style>

