<template>
  <div>
  <div>
  <el-dialog
    title="接诊评估"
    class="newDialog"
    :modal = 'false'
    :modal-append-to-body="false"
    :close-on-click-modal="false"
    :visible.sync="isVisibility"
    width="854px"
  >
    <div class="warnTxt" v-if="showTxt != ''">{{ showTxt }}</div>
    <el-form
      :model="receiveTreatmentAsses"
      ref="receiveTreatmentAsses"
      label-width="86px"
    >
      <el-form-item
        label="入室方式 : "
        :prop="isName('入室方式')"
        :rules="isCheckmust('入室方式')"
        v-if="isShow('入室方式')"
      >
        <el-radio-group v-model="receiveTreatmentAsses.way">
        <el-radio
          v-for="(item, index) in way_arr"
          :key="index"
          :label="item.id"
          >{{ item.name }}
        </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="病人意识 : "
        v-if="isShow('病人意识')"
        :prop="isName('病人意识')"
        :rules="isCheckmust('病人意识')"
      >
        <el-radio-group v-model="receiveTreatmentAsses.consciousness">
        <el-radio
          v-for="(item, index) in consciousness_arr"
          :key="index"
          :label="item.id"
          >{{ item.name }}
        </el-radio>
        </el-radio-group>

        <!--<el-radio v-model="receiveTreatmentAsses.consciousness" label="1">清醒</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.consciousness" label="2">嗜睡</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.consciousness" label="3">昏迷</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.consciousness" label="4">模糊</el-radio>-->
      </el-form-item>
      <el-form-item
        label="病人食欲 : "
        :prop="isName('病人食欲')"
        :rules="isCheckmust('病人食欲')"
        v-if="isShow('病人食欲')"
      >
      <el-radio-group v-model="receiveTreatmentAsses.appetite">
        <el-radio
          v-for="(item, index) in appetite_arr"
          :key="index"
          :label="item.id"
          >{{ item.name }}
        </el-radio>
      </el-radio-group>
        <!--<el-radio v-model="receiveTreatmentAsses.appetite" label="1">正常</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.appetite" label="2">减退</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.appetite" label="3">恶心</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.appetite" label="4">呕吐</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.appetite" label="5">腹泻</el-radio>-->
      </el-form-item>
      <el-form-item
        label="病人情况 : "
        prop="condition"
        :rules="isCheckmust('病人情况')"
        v-if="isShow('病人情况')"
      >
      <el-radio-group v-model="receiveTreatmentAsses.condition">
        <el-radio label="1"
          >住院</el-radio
        >
        <el-radio label="2"
          >门诊</el-radio
        >
        <el-radio label="3"
          >手术期</el-radio
        >
        <el-radio label="4"
          >急诊</el-radio
        >
      </el-radio-group>

      </el-form-item>

      <el-form-item
        label="住院号 : "
        :prop="isName('住院号')"
        :rules="isCheckmust('住院号')"
        v-if="this.receiveTreatmentAsses.condition == '1'"
      >
        <el-input v-model="receiveTreatmentAsses.admission_number"></el-input>
      </el-form-item>

      <el-form-item
        label="住院科室 : "
        :prop="isName('住院科室')"
        :rules="isCheckmust('住院科室')"
        v-if="isShow('住院科室') && this.receiveTreatmentAsses.condition == '1'"
      >
        <el-input v-model="receiveTreatmentAsses.his_department"></el-input>
      </el-form-item>

      <el-form-item
        label="住院床位 : "
        :prop="isName('住院床位')"
        :rules="isCheckmust('住院床位')"
        v-if="isShow('住院床位') && this.receiveTreatmentAsses.condition == '1'"
      >
        <el-input v-model="receiveTreatmentAsses.his_bed"></el-input>
      </el-form-item>

      <el-form-item
        label="诊断:"
        :prop="isName('诊断')"
        :rules="isCheckmust('诊断')"
        v-if="isShow('诊断') && this.receiveTreatmentAsses.condition == '1'"
      >
        <el-input v-model="receiveTreatmentAsses.diacrisis"></el-input>
      </el-form-item>

      <el-form-item
        label="体位 : "
        :prop="isName('体位')"
        :rules="isCheckmust('体位')"
        v-if="isShow('体位')"
      >
      <el-radio-group v-model="receiveTreatmentAsses.posture">
           <el-radio
          v-for="(item, index) in posture_arr"
          :key="index"
          :label="item.id"
          >{{ item.name }}
        </el-radio>
      </el-radio-group>

      </el-form-item>

      <el-form-item
        label="病情: "
        :prop="isName('病情')"
        :rules="isCheckmust('病情')"
        v-if="isShow('病情')"
      >
      <el-radio-group v-model="receiveTreatmentAsses.sick_condition">
        <el-radio
          v-for="(item, index) in sick_condition_arr"
          :key="index"
          :label="item.id"
          >{{ item.name }}
        </el-radio>
      </el-radio-group>

        <!--<el-radio v-model="receiveTreatmentAsses.sick_condition" label="1">一般</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.sick_condition" label="2">严重</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.sick_condition" label="3">危</el-radio>-->
      </el-form-item>

      <el-form-item
        label-width="100px"
        label="其他病情 : "
        :prop="isName('其他病情')"
        :rules="isCheckmust('其他病情')"
        v-if="isShow('其他病情')"
      >
        <el-input
          v-model="receiveTreatmentAsses.sick_condition_other"
        ></el-input>
      </el-form-item>

      <el-form-item
        label-width="150px"
        label="跌倒风险评估评分 : "
        :prop="isName('跌倒风险评估评分')"
        :rules="isCheckmust('跌倒风险评估评分')"
        v-if="isShow('跌倒风险评估评分')"
      >
        <el-input
          v-model="receiveTreatmentAsses.score"
          style="width: 100px"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="风险程度: "
        :prop="isName('风险程度')"
        :rules="isCheckmust('风险程度')"
        v-if="isShow('风险程度')"
      >
      <el-radio-group v-model="receiveTreatmentAsses.danger_level">
        <el-radio label="1"
          >无</el-radio
        >
        <el-radio label="2"
          >低风险</el-radio
        >
        <el-radio label="3"
          >中度风险</el-radio
        >
        <el-radio label="4"
          >高风险</el-radio
        >
      </el-radio-group>

      </el-form-item>

      <div style="position: relative;">
        <span v-if="isrequired" style="color: red;position: absolute;top: 10px;">*</span>
        <el-form-item
          label-width="138px"
          label="跌倒风险预防措施: "
          prop="precautionTwo"
          :rules="isCheckmust('跌倒风险预防措施')"
          v-if="isShow('跌倒风险预防措施')"
          style="display: inline-block;"
        >
          <!--v-if="isShow('跌倒风险预防措施')" isFalldown prop="precautionTwo" @change="precautionchange" isCheckmust('跌倒风险预防措施')
            :prop="isName('跌倒风险预防措施')" :rules="isCheckmust('跌倒风险预防措施')"
            [{required: true, message: '请选择跌倒风险预防措施', trigger: 'change'}]-->

          <el-checkbox-group v-model="precautionTwo" @change="precautionchange" >
            <el-checkbox
              v-for="item in precaution_arr"
              :label="item.name"
              :key="item.id"
              >{{ item.name }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </div>
      <el-form-item
        label-width="200px"
        label="其他跌倒风险预防措施 : "
        :prop="isName('其他跌倒风险预防措施')"
        :rules="isCheckmust('其他跌倒风险预防措施')"
        v-if="isShow('其他跌倒风险预防措施')"
      >
        <el-input v-model="receiveTreatmentAsses.precaution_other"></el-input>
      </el-form-item>

      <el-form-item
        label="摄入量: "
        :prop="isName('摄入量')"
        :rules="isCheckmust('摄入量')"
        v-if="isShow('摄入量')"
      >
        <!--<el-radio v-model="receiveTreatmentAsses.intake" label="1">正常</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.intake" label="2">减少</el-radio>-->
       <el-radio-group v-model="receiveTreatmentAsses.intake">
        <el-radio
          v-for="(item, index) in intake_arr"
          :key="index"
          :label="item.id"
          >{{ item.name }}
        </el-radio>
       </el-radio-group>

      </el-form-item>
      <el-form-item
        label="营养状况: "
        :prop="isName('营养状况')"
        :rules="isCheckmust('营养状况')"
        v-if="isShow('营养状况')"
      >
        <!--<el-radio v-model="receiveTreatmentAsses.nutrition" label="1">正常</el-radio>-->
        <!--<el-radio v-model="receiveTreatmentAsses.nutrition" label="2">营养不良</el-radio>-->
        <el-radio-group v-model="receiveTreatmentAsses.nutrition">
        <el-radio
          v-for="(item, index) in nutrition_arr"
          :key="index"
          :label="item.id"
          >{{ item.name }}
        </el-radio>
        </el-radio-group>

      </el-form-item>
      <el-form-item
        label="心理评估: "
        :prop="isName('心理评估')"
        :rules="isCheckmust('心理评估')"
        v-if="isShow('心理评估')"
      >
      <el-radio-group v-model="receiveTreatmentAsses.psychological_assessment">
        <el-radio
          label="1"
          >正常</el-radio
        >
        <el-radio
          label="2"
          >异常</el-radio
        >
      </el-radio-group>

      </el-form-item>
      <el-form-item
        label-width="150px"
        label="心理评估异常情况 : "
        :prop="isName('心理评估异常情况')"
        :rules="isCheckmust('心理评估异常情况')"
        v-if="
          isShow('心理评估异常情况') &&
          receiveTreatmentAsses.psychological_assessment == 2
        "
      >
        <el-input
          v-model="receiveTreatmentAsses.psychological_assessment_other"
        ></el-input>
      </el-form-item>

      <el-form-item
        label-width="150px"
        label="其他心理评估 : "
        :prop="isName('其他心理评估')"
        :rules="isCheckmust('其他心理评估')"
        v-if="isShow('其他心理评估')"
      >
        <el-input
          v-model="receiveTreatmentAsses.psychological_other"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="是否跌倒: "
        :prop="isName('是否跌倒')"
        :rules="isCheckmust('是否跌倒')"
        v-if="isShow('是否跌倒')"
      >
      <el-radio-group v-model="receiveTreatmentAsses.tumble">
        <el-radio label="1">是</el-radio>
        <el-radio label="2">否</el-radio>
      </el-radio-group>

      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancle">取 消</el-button>
      <!-- v-if="hasPermission"-->
      <el-button type="primary"   @click="handleComfirm('receiveTreatmentAsses')"
        >保 存</el-button
      >
    </div>
  </el-dialog>


  <el-dialog
      title="提示"
      :visible.sync="infoDialogVisible"
      width="30%">
      <span>
        <el-form>
          <el-row>
            <span>申请日期:</span>
            <span>
                <el-date-picker
                  type="datetime"
                  format="yyyy-MM-dd HH:mm"
                  value-format="yyyy-MM-dd HH:mm"
                  placeholder="选择时间"
                  v-model="selected_date"
              ></el-date-picker>
            </span>
          </el-row>
          <el-row>
            <span>备注:</span>
            <span>
                <el-input v-model="remark" style="width:200px"></el-input>
            </span>
          </el-row>
        </el-form>

      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="infoDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveInformation">确 定</el-button>
      </span>
    </el-dialog>
   </div>
 </div>
</template>

<script>
import { postAccepts,saveInformation } from "@/api/dialysis";
import { uParseTime } from "@/utils/tools";
import store from "@/store";
import { getDataConfig } from "@/utils/data";
import { parseTime } from "@/utils";
import request from "@/utils/request";

export default {
  name: "acceptsTreatmentDialog",
  data() {
    return {
      isrequired:false,
      showTxt: "",
      hasPermission: true,
      isVisibility: false,
      record_date: "",
      way_arr: [],
      consciousness_arr: [],
      appetite_arr: [],
      posture_arr: [],
      sick_condition_arr: [],
      precaution_arr: [],
      intake_arr: [],
      nutrition_arr: [],
      receiveTreatmentAsses: {
        way: "1",
        consciousness: "1",
        appetite: "1",
        condition: "1",
        posture: "1",
        sick_condition: "1",
        danger_level: "1",
        intake: "1",
        nutrition: "1",
        psychological_assessment: "1",
        psychological_assessment_other: "",
        sick_condition_other: "",
        score: "",
        psychological_other: "",
        precaution: "",
        precaution_other: "",
        admission_number: "",
        diacrisis: "",
        tumble: "",
        his_department: "",
        his_bed: "",
        falldown:[],
      },
      precautionTwo: [],
      arr:[],
      infoDialogVisible:false,
      selected_date:"",
      remark:""
    };
  },
  methods: {
    isShow(name) {
      var filedList = store.getters.xt_user.fileds;
      for (let i = 0; i < filedList.length; i++) {
        if (
          filedList[i].module == 2 &&
          filedList[i].filed_name_cn == name &&
          filedList[i].is_show == 1
        ) {
          return true;
        }
      }
      return false;
    },
    show(accepts) {
      console.log("accepts333333", accepts);

      console.log("入库时方式",this.way_arr)

      console.log("体委",this.posture_arr)
      if (accepts.id > 0) {
        accepts.tumble = accepts.tumble.toString();
      }else{
        console.log("hh2h3h2h3h32h23h23h32h23h")
        // this.receiveTreatmentAsses.way = "1"
        // this.receiveTreatmentAsses.posture ="1"
        // this.receiveTreatmentAsses.condition = "2"

        accepts.way =1
        accepts.posture=1
        accepts.condition =2

      }
      this.accepts = accepts;
     console.log("accepts", accepts);
      var arr = [];
      if (this.accepts.precaution) {
        var precaution = this.accepts.precaution.split(",");
        for (let i = 0; i < precaution.length; i++) {
          for (let j = 0; j < this.precaution_arr.length; j++) {
            if (parseInt(precaution[i]) == this.precaution_arr[j].id) {
              this.arr.push(this.precaution_arr[j].name);
            }
          }
        }
        this.precautionTwo = this.arr;
      }

      this.getPermission();
      for (var index in this.receiveTreatmentAsses) {
        if (
          index == "condition" ||
          index == "danger_level" ||
          index == "psychological_assessment"
        ) {
          this.receiveTreatmentAsses[index] =
            this.receiver_treatment_access[index] + "";
        } else {
          this.receiveTreatmentAsses[index] =
            this.receiver_treatment_access[index];
        }
      }
      this.isVisibility = true;
    },
    hide() {
      this.isVisibility = false;
    },
    handleCancle: function () {
      this.isVisibility = false;
    },
    // 复选框跌倒风险措施
    precautionchange(){

      if(this.precautionTwo.length>0){
        this.$refs.receiveTreatmentAsses.clearValidate('precautionTwo');
      }
    },

    handleComfirm: function (formName) {
      // var arr = [];
      for (let i = 0; i < this.precaution_arr.length; i++) {
        for (let j = 0; j < this.precautionTwo.length; j++) {
          if (this.precaution_arr[i].name == this.precautionTwo[j]) {
            this.arr.push(this.precaution_arr[i].id);
          }
        }
      }
      // this.precautionchange()
      var obj = this.arr.join(",");
      this.receiveTreatmentAsses.precaution = obj;
      let ParamsQuery = this.receiveTreatmentAsses;

      console.log("ParamsQuery", ParamsQuery);
      ParamsQuery["patient"] = this.patient.id;
      ParamsQuery["record_date"] = this.record_date;
      //console.log(this.receiver_treatment_access);
      ParamsQuery["mode"] = "1";
      if (this.receiver_treatment_access.id > 0) {
        ParamsQuery["mode"] = "2";
        if (
          this.$store.getters.xt_user.user.id !=
          this.receiver_treatment_access.creater
        ) {
          ParamsQuery["mode"] = "3";
        }
      }
      console.log("数据", ParamsQuery);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postAccepts(ParamsQuery).then((response) => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg);
               if(response.data.code == 600000008){
                 this.infoDialogVisible = true
               }
              return false;
            } else {
              this.$notify({
                title: "成功",
                message: "提交成功",
                type: "success",
                duration: 2000,
              });

              let receive_treatment_asses_resp =
                response.data.data.receiveTreatmentAsses;

              //pror
              var receiver_treatment_access = this.receiver_treatment_access;
              for (var index in receive_treatment_asses_resp) {
                // receiver_treatment_access[index] = receive_treatment_asses_resp[index];
                this.$set(
                  receiver_treatment_access,
                  index,
                  receive_treatment_asses_resp[index]
                );
              }
              this.hide();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    isName(name) {
      let filedList = store.getters.xt_user.fileds;
      for (let i = 0; i < filedList.length; i++) {
        if (filedList[i].module == 2 && filedList[i].filed_name_cn == name) {
          return filedList[i].filed_name;
        }
      }
    },

    isCheckmust(name) {
      let filedList = store.getters.xt_user.fileds;
      for (let i = 0; i < filedList.length; i++) {

        if (
          filedList[i].module == 2 &&
          filedList[i].filed_name_cn == name &&
          filedList[i].is_write == 1
        ) {
          // name == '跌倒风险预防措施' && this.precautionTwo.length<=0
          if (name == "风险程度"||name == "病人情况"||name == '心理评估') {
            return [
              { required: true, message: `请输入${name}`,trigger: ['blur','change'] },
              // {
              //   required: true,
              //   pattern: /^[1-9]\d*$/,
              //   message: `请输入${name}`,
              //   trigger: ['blur','change']
              // },
            ];
          }
          else if(name == "跌倒风险预防措施"){
            if(this.precautionTwo.length<=0){
              this.isrequired=false
              return [{ required: true, message: `请输入${name}`,trigger: ['blur','change'] }];

            }
            this.precautionchange()
            this.isrequired=true
            // return [{ required: true, message: '',trigger: ['blur','change'] }];
          }else{
            return [{ required: true, message: `请输入${name}`,trigger: ['blur','change'] }];

          }

        }
      }
    },
    // isFalldown(name){
    //   let filedList = store.getters.xt_user.fileds;
    //   for (let i = 0; i < filedList.length; i++) {
    //     if (
    //       filedList[i].module == 2 &&
    //       filedList[i].filed_name_cn == name &&
    //       filedList[i].is_write == 1
    //     ) {
    //       if (name == "跌倒风险预防措施" && this.precautionTwo.length<=0) {
    //         return [
    //           { required: true, message: `请选择${name}`,trigger: ['blur','change'] },
    //           {
    //             required: true,
    //             pattern: /^[1-9]\d*$/,
    //             message: `请选择${name}`,
    //             trigger: ['blur','change']
    //           },
    //         ];
    //       }
    //     }
    //   }
    // },
    getPermission() {
      request
        .get("/api/func_per/get", {
          params: {
            create_url: "/api/dialysis/accepts?mode=1",
            modify_url: "/api/dialysis/accepts?mode=2",
            modify_other_url: "/api/dialysis/accepts?mode=3",
            module: 2,
          },
        })
        .then((res) => {
          // console.log(res);
          //console.log("接诊", this.accepts);
          if (res.data.state == 0) {
            this.hasPermission = false;
          } else if (res.data.state == 1) {
            if (this.accepts.id != "" && this.accepts.creater != 0) {
              //有数据
              if (this.accepts.creater == this.$store.getters.xt_user.user.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.accepts.id == "" || this.accepts.creater == 0) {
              if (res.data.data.is_has_create == false) {
                this.hasPermission = false;
                this.showTxt = "你没有新增接诊评估权限";
              }
            }
          }
        });
    },

   saveInformation(){
        var params = {
          selected_date:this.selected_date,
          patient_id:this.$route.query.patient_id,
          record_date:this.$route.query.date,
          module:2,
          remark:this.remark,
        }
        saveInformation(params).then(response=>{
           if(response.data.state == 1){
             var information = response.data.data.information
             this.$message.success("提交成功!")
             this.infoDialogVisible = false
           }
        })
      }
  },
  props: {
    receiver_treatment_access: {
      // 双人核对
      type: Object,
      default: () => {
        return { id: 0 };
      },
    },
    patient: {
      // 患者信息
      type: Object,
      default: () => {
        return { id: 0 };
      },
    },
  },
  watch: {
    isVisibility(val) {},
    "receiver_treatment_access.id": function () {
      if (this.receiver_treatment_access.id > 0) {
        for (var index in this.receiveTreatmentAsses) {
          this.receiveTreatmentAsses[index] =
            this.receiver_treatment_access[index];
        }
      }
    },
  },
  created() {
    // console.log("接诊评估", this.receiver_treatment_access);
    var date = this.$route.query && this.$route.query.date;
    this.record_date = date
      ? uParseTime(date, "{y}-{m}-{d}")
      : parseTime(new Date(), "{y}-{m}-{d}");
    console.log("this.record_date", this.record_date);

    this.way_arr = getDataConfig("hemodialysis", "way");
    this.consciousness_arr = getDataConfig("hemodialysis", "consciousness");
    this.appetite_arr = getDataConfig("hemodialysis", "appetite");
    this.posture_arr = getDataConfig("hemodialysis", "posture");
    this.sick_condition_arr = getDataConfig("hemodialysis", "sick_condition");
    this.precaution_arr = getDataConfig("hemodialysis", "precaution");
    // console.log("风险", this.precaution_arr);
    this.intake_arr = getDataConfig("hemodialysis", "intake");
    this.nutrition_arr = getDataConfig("hemodialysis", "nutrition");
  },
};
</script>

<style scoped>
.warnTxt {
  text-align: center;
  margin: 0 auto;
  background: #faa331;
  max-width: 240px;
  padding: 10px 20px;
  border-radius: 4px;
  margin-bottom: 10px;
  color: #fff;
}
</style>
<style lang="scss">
.newDialog {
  .el-dialog__body {
    padding: 10px 20px 30px;
  }
}
</style>
