<template>
  <el-dialog
    class="newDialog"
    title="治疗小结"
    width="854px"
    :visible.sync="isVisibility"
    :modal-append-to-body="false"
  >
    <div class="warnTxt" v-if="showTxt != ''">{{ showTxt }}</div>
    <el-form :model="treatmentSummary">
      <el-row :gutter="20">
        <el-col :span="24" v-if="isShow('宣教知识')">
          <el-form-item label="宣教知识：">
            <el-select @change="dialysisAfterTeachSelectChange" v-model="value">
              <el-option
                v-for="(item, index) in education"
                :label="item.text"
                :value="item.value"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col v-if="isShow('宣教知识')">
          <el-form-item>
            <el-input
              type="textarea"
              v-model="treatmentSummary.mission"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24" v-if="isShow('透析小结')">
          <el-form-item label="透析小结：">
            <el-select @change="dialysisSummarySelectChange" v-model="value2">
              <el-option
                v-for="(item, index) in summary"
                :label="item.text"
                :value="item.value"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col v-if="isShow('透析小结')">
          <el-form-item>
            <el-input
              type="textarea"
              v-model="treatmentSummary.dialysis_summary"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24" v-if="isShow('透析护理记录')">
          <el-form-item label="透析护理记录:">
            <el-select
              @change="dialysisNusingRecordSelectChange"
              v-model="value3"
            >
              <el-option
                v-for="(item, index) in nursingRecord"
                :label="item.text"
                :value="item.value"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col v-if="isShow('透析护理记录')">
          <el-form-item>
            <el-input
              type="textarea"
              v-model="treatmentSummary.nursing_record"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24" v-if="isShow('特殊记录')">
          <el-form-item label="特殊记录:">
            <el-select
              @change="dialysisSpecialRecordSelectChange"
              v-model="value4"
            >
              <el-option
                v-for="(item, index) in specialRecord"
                :label="item.text"
                :value="item.value"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col v-if="isShow('特殊记录')">
          <el-form-item>
            <el-input
              type="textarea"
              v-model="treatmentSummary.special_record"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancle">取 消</el-button>
      <el-button type="primary" @click="handleComfirm" v-if="hasPermission"
        >保 存</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { getDataConfig } from '@/utils/data'
import { postTreatmentsummary } from '@/api/dialysis'
import { uParseTime } from '@/utils/tools'
import store from '@/store'
import request from '@/utils/request'

export default {
  name: 'treatmentSummaryDialog',

  props: {
    treatment_summary: {
      // 治疗小结
      type: Object,
      default: () => {
        return { id: 0 }
      }
    },
    patient: {
      // 患者信息
      type: Object,
      default: () => {
        return { id: 0 }
      }
    },
  },
  data() {
    return {
      showTxt: '',
      hasPermission: true,
      value: '',
      value2: '',
      value3: '',
      value4: '',
      isVisibility: false,
      record_date: '',
      education: [],
      summary: [],
      nursingRecord: [],
      specialRecord: [],
      treatmentSummary: {
        mission: '',
        dialysis_summary: '',
        sj_nurse: '',
        zl_nurse: '',
        hd_nurse: '',
        xj_nurse: '',
        zl_doctor: '',
        nursing_record: '',
        special_record: ''
      },
      //
      date:''
    }
  },
  methods: {
    show(record,date) {
      this.isVisibility = true
      this.record = record
      console.log(111111111111111,date)
      if(date){
        this.date = date
        this.record_date = uParseTime(this.date, '{y}-{m}-{d}')
        console.log(22222222222,this.record_date)
      }
      
      console.log('治疗小结', record)
      this.getPermission()
    },
    hide() {
      this.isVisibility = false
    },
    dialysisAfterTeachSelectChange: function(values) {
      if (this.treatmentSummary.mission == '') {
        this.treatmentSummary.mission = values
      } else {
        if (this.treatmentSummary.mission.indexOf(values) == -1) {
          if (
            this.treatmentSummary.mission
              .charAt(this.treatmentSummary.mission.length - 1)
              .indexOf('。') == -1
          ) {
            this.treatmentSummary.mission =
              this.treatmentSummary.mission + ',' + values
          } else {
            this.treatmentSummary.mission =
              this.treatmentSummary.mission + values
          }
        }
      }
    },
    dialysisSummarySelectChange: function(values) {
      if (this.treatmentSummary.dialysis_summary == '') {
        this.treatmentSummary.dialysis_summary = values
      } else {
        if (this.treatmentSummary.dialysis_summary.indexOf(values) == -1) {
          if (
            this.treatmentSummary.dialysis_summary
              .charAt(this.treatmentSummary.dialysis_summary.length - 1)
              .indexOf('。') == -1
          ) {
            this.treatmentSummary.dialysis_summary =
              this.treatmentSummary.dialysis_summary + ',' + values
          } else {
            this.treatmentSummary.dialysis_summary =
              this.treatmentSummary.dialysis_summary + ',' + values
            this.treatmentSummary.dialysis_summary =
              this.treatmentSummary.dialysis_summary + values
          }
        }
      }
    },
    dialysisNusingRecordSelectChange: function(values) {
      if (this.treatmentSummary.nursing_record == '') {
        this.treatmentSummary.nursing_record = values
      } else {
        if (this.treatmentSummary.nursing_record.indexOf(values) == -1) {
          if (
            this.treatmentSummary.nursing_record
              .charAt(this.treatmentSummary.nursing_record.length - 1)
              .indexOf('。') == -1
          ) {
            this.treatmentSummary.nursing_record =
              this.treatmentSummary.nursing_record + ',' + values
          } else {
            this.treatmentSummary.nursing_record =
              this.treatmentSummary.nursing_record + ',' + values
            this.treatmentSummary.nursing_record =
              this.treatmentSummary.nursing_record + values
          }
        }
      }
    },
    dialysisSpecialRecordSelectChange(values) {
      if (this.treatmentSummary.special_record == '') {
        this.treatmentSummary.special_record = values
      } else {
        if (this.treatmentSummary.special_record.indexOf(values) == -1) {
          if (
            this.treatmentSummary.special_record
              .charAt(this.treatmentSummary.special_record.length - 1)
              .indexOf('。') == -1
          ) {
            this.treatmentSummary.special_record =
              this.treatmentSummary.special_record + ',' + values
          } else {
            this.treatmentSummary.special_record =
              this.treatmentSummary.special_record + ',' + values
            this.treatmentSummary.special_record =
              this.treatmentSummary.special_record + values
          }
        }
      }
    },
    handleCancle: function() {
      this.isVisibility = false
    },
    handleComfirm: function() {
      const ParamsQuery = this.treatmentSummary
      ParamsQuery['patient'] = this.patient.id
      ParamsQuery['record_date'] = this.record_date
      ParamsQuery['mode'] = '1'
      if (this.treatment_summary.id > 0) {
        ParamsQuery['mode'] = '2'
        if (
          this.treatment_summary.creater != this.$store.getters.xt_user.user.id
        ) {
          ParamsQuery['mode'] = '3'
        }
      }
      postTreatmentsummary(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
          })

          const summary_resp = response.data.data.summary
          // prop
          var treatment_summary = this.treatment_summary
          for (var index in summary_resp) {
            // treatment_summary[index] = summary_resp[index];
            this.$set(treatment_summary, index, summary_resp[index])
          }

          this.hide()
        }
      })
    },
    getPermission() {
      request
        .get('/api/func_per/get', {
          params: {
            create_url: '/api/dialysis/treatmentsummary?mode=1',
            modify_url: '/api/dialysis/treatmentsummary?mode=2',
            modify_other_url: '/api/dialysis/treatmentsummary?mode=3',
            module: 9
          }
        })
        .then(res => {
          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 = store.getters.xt_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
    }
  },
  watch: {
    isVisibility(val) {},
    'treatment_summary.id': {
      immediate: true,
      deep:true,
      handler(oldVal,newVal) {
        if(oldVal == newVal){
          for (var index in this.treatmentSummary) {
            this.treatmentSummary[index] = this.treatment_summary[index]
          }
        }
        if (this.treatment_summary.id > 0) {
          for (var index in this.treatmentSummary) {
            this.value = ''
            this.value2 = ''
            this.value3 = ''
            this.value4 = ''
            this.treatmentSummary[index] = this.treatment_summary[index]
          }
        }else if(this.treatment_summary.id == 0){
          for (var index in this.treatmentSummary) {
            // this.treatmentSummary[index] = this.treatment_summary[index]
            this.value = ''
            this.value2 = ''
            this.value3 = ''
            this.value4 = ''
            this.treatmentSummary = {
              mission: '',
              dialysis_summary: '',
              sj_nurse: '',
              zl_nurse: '',
              hd_nurse: '',
              xj_nurse: '',
              zl_doctor: '',
              nursing_record: '',
              special_record: ''
            }
          }
          
        }
      }
    }
  },
  created() {
    this.education = getDataConfig('education', 'education')
    this.summary = getDataConfig('summary', 'summary')

    this.nursingRecord = getDataConfig('nursing_record', 'nursing_record')

    this.specialRecord = getDataConfig('special_record', 'special_record')

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

    if(this.$route.query && this.$route.query.date){
      var date = this.$route.query && this.$route.query.date
      this.record_date = uParseTime(date, '{y}-{m}-{d}')
    }
  }
}
</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>
