<template>
  <div>
    <div class="Dialog" v-show="isShowDialog">
      <div class="DialogTit">
        <span @click="close()" class="iconfont">&#xe6e9;</span>
        <h1 class="name">治疗小结</h1>
        <span @click="commitInfo" class="success" v-if="hasPermission">保存</span>
        <span style="visibility: hidden;" class="success" v-if="!hasPermission">保存</span>
      </div>
      <div class="warnTxt" v-if="showTxt != ''">
        {{ showTxt }}
      </div>

      <div class="DialogContent choose" id="dialogTop">

        <el-form :model="dialysisSummary" label-width="120px">
        <el-row>
         <el-col v-if="isShow('宣教知识')">
            <el-form-item label="宣教知识 : ">
              <el-input
                class="treatmentInput"
                readonly
                v-model="dialysisSummary.propagandaAndEducationContentSelect"
                placeholder="请选择"
                @focus="showSubMenu('education')"
              ></el-input>
            </el-form-item>
         </el-col>
         </el-row>
         <el-row>
          <el-col v-if="isShow('宣教知识')">
            <el-form-item label-width="0">
              <el-input
                class="newTextarea treatmentInput"
                type="textarea"
                v-model="dialysisSummary.propagandaAndEducationContent"
                :rows="5"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-col v-if="isShow('透析小结')">
          <el-form-item label="透析小结 : ">
            <el-input
              readonly
              class="treatmentInput"
              v-model="dialysisSummary.summaryContentSelect"
              placeholder="请选择"
              @focus="showSubMenu('summary')"
            ></el-input>
          </el-form-item>
       </el-col>
       </el-row>
       <el-row>
        <el-col v-if="isShow('透析小结')">
            <el-form-item label-width="0">
              <el-input
                class="newTextarea treatmentInput"
                @focus="lastInputFocus"
                @blur="lastInputBlur"
                type="textarea"
                v-model="dialysisSummary.summaryContent"
                :rows="5"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
          <el-row>
            <el-col v-if="isShow('透析护理记录')">
              <el-form-item label="透析护理记录:">
                <el-input
                    class="treatmentInput"
                    readonly
                    v-model="dialysisSummary.nursingRecordSelect"
                    placeholder="请选择"
                    @focus="showSubMenu('nursing_record')"
                ></el-input>
              </el-form-item>
            </el-col>
        </el-row>
         <el-row>
          <el-col v-if="isShow('透析护理记录')">
            <el-form-item label-width="0">
              <el-input
                class="newTextarea treatmentInput"
                type="textarea"
                v-model="dialysisSummary.nursing_record"
                :rows="5"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col v-if="isShow('特殊记录')">
              <el-form-item label="特殊记录:">
                <el-input
                      class="treatmentInput"
                      readonly
                      v-model="dialysisSummary.specialRecordSelect"
                      placeholder="请选择"
                      @focus="showSubMenu('special_record')"
                  ></el-input>
            </el-form-item>
           </el-col>
        </el-row>
          <el-row>
            <el-col v-if="isShow('特殊记录')">
              <el-form-item label-width="0">
                <el-input
                  class="newTextarea treatmentInput"
                  type="textarea"
                  v-model="dialysisSummary.special_record"
                  :rows="5"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>

    <check-box-sub-menu
      :visibility="visibility"
      v-on:menu-cancle="menuCancle"
      v-on:menu-comfirm="menuComfirm"
      :propsForm="propForm"
    ></check-box-sub-menu>

    <infor-sub-menu
      :visibility="infor_visibility"
      v-on:menu-cancle-three="menuCancleFour"
      v-on:menu-comfirm-three="menuComfirmFour"
      >
     </infor-sub-menu>
  </div>
</template>

<script>
import { commitTreatmentSummary,saveMobileInformation } from "@/api/dialysis";
import { Toast } from "vant";
import CheckBoxSubMenu from "./subMenu/checkBoxSubMenu";
import { getDataConfig } from "@/utils/data";

import request from "@/utils/request";
import InforSubMenu from './subMenu/InforSubMenu'
export default {
  name: "TreatmentDialog",

  data() {
    return {
      showTxt: "",
      hasPermission: true,
      isShowDialog: true,

      visibility: false,
      propForm: {
        title: "",
        list: [],
        optionList: [],
        isMultiple: 2,
        result: [], // 选中的值
        type: 1, // 用来区分不同子菜单，方便对返回值进行赋值
        selectId: 0
      },

      dialysisSummary: {
        text: "",
        propagandaAndEducationContent: "",
        propagandaAndEducationContentSelect: "",
        text2: "",
        summaryContent: "",
        summaryContentSelect: "",
        changeMedicalNurse: "",
        treatNurse: "",
        checkStaff: "",
        deboardNurse: "",
        treatDoctor: "",
        nursing_record: "",
        special_record: "",
        nursingRecordSelect: "",
        specialRecordSelect: ""
      },
      summary: [],
      summaryObj: {},
      teach: [],
      teachObj: {},
      title: "治疗小结 ",
      record_date: "",
      patient: {
        id: 0
      },
      nursingRecord: [],
      specialRecord: [],
      infor_visibility:false,
    };
  },
  props: {
    record: {
      type: Object
    },
    patient_prop: {
      type: Object
    }
  },
  created() {
    this.getPermission();
    // this.summary = this.$store.getters.summary;
    this.summary = getDataConfig("summary", "summary");
    // console.log("透后选叫", getDataConfig("summary", "summary"));
    // console.log("this.summary",this.summary)
    // this.teach = this.$store.getters.teach;
    this.teach = getDataConfig("education", "education");
    // console.log("this", this.teach);
    if (this.teach.length > 0) {
      var tlen = this.teach.length;
      for (let index = 0; index < tlen; index++) {
        this.teach[index].name = this.teach[index].text;
        this.teachObj[this.teach[index].id] = this.teach[index];
      }
    }
    if (this.summary.length > 0) {
      var tlen = this.summary.length;
      for (let index = 0; index < tlen; index++) {
        this.summary[index].name = this.summary[index].text;
        this.summaryObj[this.summary[index].id] = this.summary[index];
      }
    }
    this.nursingRecord = getDataConfig("nursing_record", "nursing_record");
    // console.log("透析护理记录", this.nursingRecord);

    if (this.nursingRecord.length > 0) {
      var tlen = this.nursingRecord.length;
      for (let index = 0; index < tlen; index++) {
        this.nursingRecord[index].name = this.nursingRecord[index].text;
        this.summaryObj[this.nursingRecord[index].id] = this.nursingRecord[
          index
        ];
      }
    }
    this.specialRecord = getDataConfig("special_record", "special_record");
    if (this.specialRecord.length > 0) {
      var tlen = this.specialRecord.length;
      for (let index = 0; index < tlen; index++) {
        this.specialRecord[index].name = this.specialRecord[index].text;
        this.summaryObj[this.specialRecord[index].id] = this.specialRecord[
          index
        ];
      }
    }

    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

    if (this.record != null && this.record.id != "") {
      for (const key in this.record) {
        this.dialysisSummary[key] = this.record[key];
      }
      if (this.record.mission != "undefined") {
        this.dialysisSummary.propagandaAndEducationContent = this.record.mission;
      }
      if (this.record.dialysis_summary != "undefined") {
        this.dialysisSummary.summaryContent = this.record.dialysis_summary;
      }
    }

    if(this.$store.getters.user.template_info.org_id == 10340 || this.$store.getters.user.template_info.org_id == 3877){
       console.log("oohhhhwh2h33h223h",this.record)
       if(this.record!=undefined && this.record.id == 0){
         this.dialysisSummary.summaryContent = "安全下机"
       }
    }  
  },
  methods: {
    lastInputFocus: function(event) {
      var input = event.target;
      setTimeout(function() {
        input.style.marginBottom = "2rem";
        input.parentNode.scrollIntoView();
      }, 0);
    },
    lastInputBlur: function(event) {
      var input = event.target;
      setTimeout(function() {
        input.style.marginBottom = "";
      }, 0);
    },
    showSubMenu: function(val) {
      switch (val) {
        case "education":
          this.propForm.type = 1;
          this.isShowDialog = false;
          this.propForm.title = "透后宣教";
          this.visibility = true;
          this.propForm.list = [];
          this.propForm.optionList = this.teach;
          this.propForm.isMultiple = 1;
          this.propForm.selectId = this.dialysisSummary.xjid;
          break;
        case "summary":
          this.propForm.type = 2;
          this.isShowDialog = false;
          this.propForm.title = "透析小结";
          this.visibility = true;
          this.propForm.list = [];
          this.propForm.optionList = this.summary;
          console.log("summary", this.summary);
          this.propForm.isMultiple = 1;
          this.propForm.selectId = this.dialysisSummary.xxjid;
          break;
        case "nursing_record":
          this.propForm.type = 3;
          this.isShowDialog = false;
          this.propForm.title = "透析护理记录";
          this.visibility = true;
          this.propForm.list = [];
          this.propForm.optionList = this.nursingRecord;
          this.propForm.isMultiple = 1;
          this.propForm.selectId = this.dialysisSummary.xxxjid;
          break;
        case "special_record":
          this.propForm.type = 4;
          this.isShowDialog = false;
          this.propForm.title = "特殊记录";
          this.visibility = true;
          this.propForm.list = [];
          this.propForm.optionList = this.specialRecord;
          this.propForm.isMultiple = 1;
          this.propForm.selectId = this.dialysisSummary.xxxxjid;
          break;
      }
    },
    menuCancle: function() {
      this.visibility = false;
      this.isShowDialog = true;
    },
    menuComfirm: function(val) {
      this.visibility = false;
      this.isShowDialog = true;
      switch (val.type) {
        case 1:
          if (val.selectId in this.teachObj) {
            var theValue = this.teachObj[val.selectId].value;
            this.dialysisAfterTeachSelectChange(theValue);
            this.dialysisSummary.xjid = val.selectId;
            this.dialysisSummary.propagandaAndEducationContentSelect = this.teachObj[
              val.selectId
            ].name;
          }
          break;
        case 2:
          if (val.selectId in this.summaryObj) {
            var theValue = this.summaryObj[val.selectId].value;
            this.dialysisSummarySelectChange(theValue);
            this.dialysisSummary.xxjid = val.selectId;
            this.dialysisSummary.summaryContentSelect = this.summaryObj[val.selectId].name;
            // console.log("透析======",this.summaryObj[val.selectId].name)
          }
          break;
        case 3:
          if (val.selectId in this.summaryObj) {
            var theValue = this.summaryObj[val.selectId].value;
            this.dialysisSummaryNuserocoderChange(theValue);
            this.dialysisSummary.xxxjid = val.selectId;
            this.dialysisSummary.nursingRecordSelect = this.summaryObj[
              val.selectId
            ].name;
          }
          break;
        case 4:
          if (val.selectId in this.summaryObj) {
            var theValue = this.summaryObj[val.selectId].value;
            this.dialysisSummarySpecialcoderChange(theValue);
            this.dialysisSummary.xxxxjid = val.selectId;
            this.dialysisSummary.specialRecordSelect = this.summaryObj[
              val.selectId
            ].name;
          }
          break;
      }
    },

    dialysisSummarySelectChange: function(values) {
      if (this.dialysisSummary.summaryContent == null) {
        this.dialysisSummary.summaryContent == "";
      }

      if (this.dialysisSummary.summaryContent == "") {
        this.dialysisSummary.summaryContent = values;
      } else {
        if (this.dialysisSummary.summaryContent.indexOf(values) == -1) {
          if (
            this.dialysisSummary.summaryContent
              .charAt(this.dialysisSummary.summaryContent.length - 1)
              .indexOf("。") == -1
          ) {
            this.dialysisSummary.summaryContent =
              this.dialysisSummary.summaryContent + "," + values;
          } else {
            this.dialysisSummary.summaryContent =
              this.dialysisSummary.summaryContent + values;
          }
        }
      }
    },
    dialysisAfterTeachSelectChange: function(values) {
      
      if (this.dialysisSummary.propagandaAndEducationContent == null) {
        this.dialysisSummary.propagandaAndEducationContent == "";
      }
      if (this.dialysisSummary.propagandaAndEducationContent == "") {
        this.dialysisSummary.propagandaAndEducationContent = values;
      } else {
        
        if (
          this.dialysisSummary.propagandaAndEducationContent.indexOf(values) ==
          -1
        ) {
          if (
            this.dialysisSummary.propagandaAndEducationContent
              .charAt(
                this.dialysisSummary.propagandaAndEducationContent.length - 1
              )
              .indexOf("。") == -1
          ) {
            this.dialysisSummary.propagandaAndEducationContent =
              this.dialysisSummary.propagandaAndEducationContent + "," + values;
          } else {
            this.dialysisSummary.propagandaAndEducationContent =
              this.dialysisSummary.propagandaAndEducationContent + values;
          }
        }
      }
    },
    dialysisSummaryNuserocoderChange: function(values) {
      if (this.dialysisSummary.nursing_record == null) {
        this.dialysisSummary.nursing_record == "";
      }
      if (this.dialysisSummary.nursing_record == "") {
        this.dialysisSummary.nursing_record = values;
      } else {
        if (this.dialysisSummary.nursing_record.indexOf(values) == -1) {
          if (
            this.dialysisSummary.nursing_record
              .charAt(this.dialysisSummary.nursing_record.length - 1)
              .indexOf("。") == -1
          ) {
            this.dialysisSummary.nursing_record =
              this.dialysisSummary.nursing_record + "," + values;
          } else {
            this.dialysisSummary.nursing_record =
              this.dialysisSummary.nursing_record + values;
          }
        }
      }
    },
    dialysisSummarySpecialcoderChange: function(values) {
      if (this.dialysisSummary.special_record == null) {
        this.dialysisSummary.special_record == "";
      }
      if (this.dialysisSummary.special_record == "") {
        this.dialysisSummary.special_record = values;
      } else {
        if (this.dialysisSummary.special_record.indexOf(values) == -1) {
          if (
            this.dialysisSummary.special_record
              .charAt(this.dialysisSummary.special_record.length - 1)
              .indexOf("。") == -1
          ) {
            this.dialysisSummary.special_record =
              this.dialysisSummary.special_record + "," + values;
          } else {
            this.dialysisSummary.special_record =
              this.dialysisSummary.special_record + values;
          }
        }
      }
    },
    commitInfo: function() {
      Toast.loading({ forbidClick: true, duration: 0 });
      let ParamsQuery = this.dialysisSummary;
      if(this.$route.query.patient_id){
        ParamsQuery["patient"] = this.$route.query.patient_id;
      }else{
        ParamsQuery["patient"] = this.patient_prop.id
      }
      
      ParamsQuery["record_date"] = this.record_date;
      ParamsQuery["mode"] = "1";

      if (this.record.id > 0) {
        ParamsQuery["mode"] = "2";
        if (this.record.creater != this.$store.getters.user.admin.id) {
          ParamsQuery["mode"] = "3";
        }
      }

      commitTreatmentSummary(ParamsQuery)
        .then(response => {
          if (response.data.state == 0) {
             Toast.fail(response.data.msg);
            
            if(response.data.code == 600000008){
              this.visibility = false;
              this.infor_visibility = true
            }
            return false;
          } else {
            Toast.success("提交成功");

            this.$emit("did_update", response.data.data.summary);
            for (const key in response.data.data.summary) {
              this.record[key] = response.data.data.summary[key];
            }
          }
        })
        .catch(err => {
          Toast.fail(err);
        });
    },
    close: function() {
      this.$emit("close");
    },
    open: function() {
      this.isShowDialog = true;
      this.visibility = false;
      var dialogTop = document.querySelector("#dialogTop");
      if (dialogTop != null) {
        this.$nextTick(() => {
          dialogTop.scrollTop = 0;
        });
      }
    },
    getPermission() {
      request
        .get("/m/api/permission/get", {
          params: {
            create_url: "/m/api/dialysis/treatmentSummary?mode=1",
            modify_url: "/m/api/dialysis/treatmentSummary?mode=2",
            modify_other_url: "/m/api/dialysis/treatmentSummary?mode=3",
            module: 9
          }
        })
        .then(res => {
          console.log(res);
          console.log("小结", this.record);
          if (res.data.state == 0) {
            this.hasPermission = false;
          } else if (res.data.state == 1) {
            if (this.record.id != "" && this.record.creater != 0) {
              // 有数据
              if (this.record.creater == this.$store.getters.user.admin.id) {
                // 创建人是自己
                if (res.data.data.is_has_modify == false) {
                  this.hasPermission = false;
                  this.showTxt = "你没有修改治疗小结权限";
                }
              } else {
                // 创建人不是自己
                if (res.data.data.is_has_modify_other == false) {
                  this.hasPermission = false;
                  this.showTxt = "你没有修改他人治疗小结权限";
                }
              }
            } else if (this.record.id == "" || this.record.creater == 0) {
              if (res.data.data.is_has_create == false) {
                this.hasPermission = false;
                this.showTxt = "你没有新增治疗小结权限";
              }
            }
          }
        });
    },
    isShow(name) {
      var filedList = this.$store.getters.user.fileds;

      for (let i = 0; i < filedList.length; i++) {
        if (
          filedList[i].module == 6 &&
          filedList[i].filed_name_cn == name &&
          filedList[i].is_show == 1
        ) {
          return true;
        }
      }
      return false;
    },
   menuCancleFour:function(){
      this.infor_visibility = false
    },
    menuComfirmFour:function(start_time,remark){
        var params = {
          patient_id:this.patient_prop.id,
          record_date:this.switchMsec(this.record_date),
          start_time:start_time,
          module:10,
          remark:remark,
        }
        
        saveMobileInformation(params).then(response=>{
          if(response.data.state == 1){
            this.infor_visibility = false
            Toast.success('申请成功')
          }
        })
      },
     switchMsec (time){ // 日期转换成毫秒
      let date = new Date(time);
      // 有三种方式获取
      let time1 = date.getTime(); // 精确到毫秒;
      return (time1/1000)-28800
    },
  },

  components: {
    CheckBoxSubMenu,
     InforSubMenu
  }
};
</script>

<style style="stylesheet/scss" lang="scss" scoped>
.choose {
  padding: 0.6rem 0.6rem 0 0.6rem;
  .button {
    text-align: center;
    .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;
    }
  }
}
.warnTxt {
  font-size: 0.5rem;
  height: 1rem;
  line-height: 1rem;
  text-align: center;
  background: #faa331;
  color: #fff;
}
</style>
<style lang="scss">
.newTextarea {
  font-size: 0.45rem;
}
.treatmentInput {
  .el-input__inner {
    font-size: 0.45rem;
  }
  .el-textarea__inner {
    font-size: 0.45rem;
    background: #f6f6f6;
  }
}
</style>
