<template>
  <el-dialog
    :title="name"
    :visible.sync="isLastOrNextVisible"
    @close="hide"
    width="1010px"
    :modal-append-to-body="false"
    class="isLastOrNext"
  >
    <div class="txsj" style="text-align:center;">
      <el-button
        round

        @click="openLast(3)"
      >上一方
      </el-button>

      <el-button
        round
        @click="openNext(4)"
      >下一方
      </el-button>
    </div>

    <div v-for="(item,index) in allPrescription" :key="index">
      <div class="quote">引用 {{getTime(item.record_date, '{y}年{m}月{d}日')}} 的处方</div>
      <el-checkbox-group v-model="item.check_group">
        <div v-for="(subItem,index2) in item.prescriptions" :key="index2">
          <el-checkbox style="margin:10px 0;" :label="subItem.id">{{'处方' + (index2+ 1)}}</el-checkbox>
          <el-table v-if="subItem.type  == 1" :data="subItem.doctor_advice" border style="width: 99%;"
                    :row-style="{ color: '#303133' }"
                    :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" highlight-current-row>
            <el-table-column align="center" type="index" width="60" label="序号"></el-table-column>
            <el-table-column align="center" prop="drug_name"  width="160" label="名称">
              <template slot-scope="scope">
                <span>{{ scope.row.advice_name }}</span>
              </template>
            </el-table-column>

            <el-table-column align="center" prop="single_dose" width="90" label="单次用量">
              <template slot-scope="scope">
                  <div>{{scope.row.single_dose}}{{scope.row.single_dose_unit}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="delivery_way" width="100" label="用法">
              <template slot-scope="scope">
                {{scope.row.delivery_way}}
              </template>
            </el-table-column>
            <el-table-column align="center" prop="execution_frequency" width="100" label="频率">
              <template slot-scope="scope">
                {{scope.row.execution_frequency}}
              </template>
            </el-table-column>

            <el-table-column align="center" prop="day" width="60" label="天数">
              <template slot-scope="scope">
                {{scope.row.day}}
              </template>
            </el-table-column>

            <el-table-column align="center" prop="prescribing_number" width="100" label="总量">
              <template slot-scope="scope">
                <div>{{scope.row.prescribing_number}} {{scope.row.prescribing_number_unit}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="retail_price" width="80" label="单价">
              <template slot-scope="scope">
                <div>{{scope.row.retail_price}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="remark" label="备注">
              <template slot-scope="scope">
                <div>{{scope.row.remark}}</div>
              </template>
            </el-table-column>
          </el-table>
          <el-table v-if="subItem.type == 2" :data="subItem.project" border style="width: 99%;"
                    :row-style="{ color: '#303133' }"
                    :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" highlight-current-row>
            <el-table-column align="center" type="index" width="60" label="序号"></el-table-column>
            <el-table-column align="center" prop="project_name" width="160" label="名称">
              <template slot-scope="scope">
                <span>{{ scope.row.type == 2 ?scope.row.project.project_name:scope.row.good_info.good_name}}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="statistical_classification" width="100" label="组">
              <template slot-scope="scope">{{ scope.row.type == 2 ?getGroup(scope.row.statistical_classification):"耗材"}}</template>
            </el-table-column>
            <el-table-column align="center" prop="single_dose" width="90" label="单次用量">
              <template slot-scope="scope">
                <!--<el-input v-model="scope.row.single_dose" placeholder=""></el-input>-->
                <span>{{ scope.row.single_dose }}</span>

              </template>
            </el-table-column>
            <el-table-column align="center" prop="delivery_way" width="100" label="用法">
              <template slot-scope="scope">
                <!--<el-input v-model="scope.row.delivery_way" placeholder=""></el-input>-->
                <span>{{ scope.row.delivery_way }}</span>

              </template>
            </el-table-column>
            <el-table-column align="center" prop="execution_frequency" width="100" label="频率">
              <template slot-scope="scope">
                <!--<el-input v-model="scope.row.execution_frequency" placeholder=""></el-input>-->
                <span>{{ scope.row.execution_frequency }}</span>

              </template>
            </el-table-column>
            <el-table-column align="center" prop="number_days" width="70" label="天数">
              <template slot-scope="scope">
                <!--<el-input v-model="scope.row.number_days" placeholder=""></el-input>-->
                <span>{{ scope.row.number_days }}</span>

              </template>
            </el-table-column>
            <el-table-column align="center" prop="total" width="70" label="总量">
              <template slot-scope="scope">
                  <div>{{ scope.row.total }}{{scope.row.unit}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="name" width="60" label="单价">
              <template slot-scope="scope">
                <!--<el-input v-model="scope.row.price" placeholder="" readonly></el-input>-->
                <span>{{ scope.row.price }}</span>

              </template>
            </el-table-column>
            <el-table-column align="center" prop="name" label="备注">
              <template slot-scope="scope">
                <!--<el-input v-model="scope.row.remark" :title="scope.row.remark"></el-input>-->
                <span>{{ scope.row.remark }}</span>

              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-checkbox-group>

    </div>


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


</template>

<script>
  import { uParseTime } from '@/utils/tools'
  import { getDictionaryDataConfig} from "@/utils/data";

  import {
    getNextOrLastHisPrescription
  } from '@/api/his/his'
  export default {
    name: "nextOrLastPrescription",
    data() {
      return {
        srcPrescriptions:[],
        name:"",
        request_record_date:'',
        patient_id:0,
         allPrescription: [
          {
            check_group: [],
            prescriptions: [
              {
                id: 5,

                type: 1,
                doctor_advice: [],
                project: [],
              },
              {
                id: 8,
                type: 1,
                doctor_advice: [],
                project: [],
              },
              {
                id: 6,

                type: 2,
                doctor_advice: [],
                project: [],
              },
              {
                id: 7,
                type: 2,
                doctor_advice: [],
                project: [],
              },
            ],
          }, {
            check_group: [],
            prescriptions: [
              {
                id: 1,
                type: 2,
                doctor_advice: [],
                project: [],
              },
              {

                id: 2,
                type: 2,
                doctor_advice: [],
                project: [],
              },
              {
                id: 3,

                type: 1,
                doctor_advice: [],
                project: [],
              },
              {
                id: 4,

                type: 1,
                doctor_advice: [],
                project: [],
              },
            ],
          }
        ],

        isLastOrNextVisible: false,

      }
    }, methods: {
      openLast(val) {
       if (val == 3) {
          let params = {
            patient_id: this.patient_id,
            record_time: this.request_record_date,
            type: 1,
            p_type:2,
          };

          this.GetNextOrLastHisPrescription(params)

        }
      },
      openNext(val) {
        if (val == 4) {
          let params = {
            patient_id: this.patient_id,
            record_time: this.request_record_date,
            type: 2,
            p_type:2,

          };
          this.GetNextOrLastHisPrescription(params)
        }
      },
      GetNextOrLastHisPrescription(params){
        getNextOrLastHisPrescription(params).then(response => {
          if (response.data.state == 1) {
            this.allPrescription = response.data.data.prescriptions;
            for (let i = 0; i < this.allPrescription.length; i++){
              this.allPrescription[i].check_group = []
            }
            this.request_record_date = uParseTime(response.data.data.prescriptions[0].record_date, '{y}-{m}-{d}')
          } else {
            this.$message.error(response.data.msg)

          }
        })
      },
      save() {
        let replace_one = [];
        let replace_two = [];

        let checkGroup = [];
        let allPrescriptions = [];
        let targetPrescriptions = [];
        let prescriptions = [];
        for (let i = 0; i < this.allPrescription.length; i++) {
          for (let a = 0; a < this.allPrescription[i].check_group.length; a++) {
            checkGroup.push(this.allPrescription[i].check_group[a])
          }
          for (let a = 0; a < this.allPrescription[i].prescriptions.length; a++) {
            allPrescriptions.push(this.allPrescription[i].prescriptions[a])
          }
        }
        for (let i = 0; i < allPrescriptions.length; i++){
          for (let a = 0; a < checkGroup.length; a++){
            if(allPrescriptions[i].id == checkGroup[a]){
              targetPrescriptions.push(allPrescriptions[i])
            }
          }
        }

        for (let i = 0; i < targetPrescriptions.length; i++) {
          var prescription = targetPrescriptions[i];
          for (let b = 0; b < prescription.doctor_advice.length; b++) {
            console.log(prescription.doctor_advice[b].drug.is_special_diseases)
            if (prescription.med_type == "14" && prescription.doctor_advice[b].drug.is_special_diseases != 1) {
              prescription.doctor_advice.splice(b--,1)
            }
          }
          for (let b = 0; b < prescription.project.length; b++) {
            if(prescription.project[b].type == 2){
              if (prescription.med_type == "14" && prescription.project[b].project.disease_directory != 1) {
                prescription.project.splice(b--,1)
              }
            }else if(prescription.project[b].type == 3){
              if (prescription.med_type == "14" && prescription.project[b].good_info.is_special_diseases != 1) {
                prescription.project.splice(b--,1)
              }
            }
          }
        }


        if (targetPrescriptions.length > 0) {
          for (let i = 0; i < targetPrescriptions.length; i++) {
            var prescription = targetPrescriptions[i];
            let tempAdvice = [];
            let tempProject = [];
            let tempAddition = [];
            for (let b = 0; b < prescription.doctor_advice.length; b++) {
              let index = b + 1
              let obj = {
                advice_id: 0,
                drug_name: prescription.doctor_advice[b].advice_name,
                single_dose: prescription.doctor_advice[b].single_dose,
                delivery_way: prescription.doctor_advice[b].delivery_way,
                execution_frequency: prescription.doctor_advice[b].execution_frequency,
                // retail_price: prescription.doctor_advice[b].price.toString(),
                remark: prescription.doctor_advice[b].remark,
                day:prescription.doctor_advice[b].day,
                prescribing_number:prescription.doctor_advice[b].prescribing_number.toString(),
                single_dose_unit:prescription.doctor_advice[b].single_dose_unit,
                prescribing_number_unit:prescription.doctor_advice[b].prescribing_number_unit,
                medical_insurance_number:prescription.doctor_advice[b].med_list_codg,
                id:prescription.doctor_advice[b].drug_id,
                drug:prescription.doctor_advice[b].drug,
                groupno:index,
                hosp_appr_flag:"1"
              };

              if (obj.prescribing_number_unit == prescription.doctor_advice[b].drug.min_unit){
                obj['retail_price'] =  prescription.doctor_advice[b].drug.min_price
              } else if(obj.prescribing_number_unit == prescription.doctor_advice[b].drug.max_unit){
                obj['retail_price'] =  prescription.doctor_advice[b].drug.retail_price
              } else {
                obj['retail_price'] =  prescription.doctor_advice[b].drug.min_price
              }
              tempAdvice.push(obj)

            }


            for (let b = 0; b < prescription.project.length; b++) {
              let obj = {
                id: 0,
                project_id: prescription.project[b].project_id,
                // project_name: prescription.project[b].project.project_name,
                // statistical_classification: prescription.project[b].project.statistical_classification,
                single_dose: prescription.project[b].single_dose,
                delivery_way: prescription.project[b].delivery_way,
                execution_frequency: prescription.project[b].execution_frequency,
                number_days: prescription.project[b].day,
                total:prescription.project[b].count.toString(),
                // price:prescription.project[b].price,
                remark:prescription.project[b].remark,
                unit: prescription.project[b].unit,
                type: prescription.project[b].type

                // medical_code:prescription.project[b].project.medical_code,
              };
              if(prescription.project[b].type == 2){
                obj['statistical_classification'] =  prescription.project[b].project.statistical_classification
                obj['medical_code'] =  prescription.project[b].project.medical_code
                obj['project_name'] =  prescription.project[b].project.project_name
                obj['price'] = prescription.project[b].project.price
                obj['project']=prescription.project[b].project

              }else if(prescription.project[b].type == 3){
                obj['statistical_classification'] = ""
                obj['medical_code'] =  prescription.project[b].good_info.medical_insurance_number
                obj['project_name'] =  prescription.project[b].good_info.good_name
                obj['price'] = prescription.project[b].good_info.packing_price
                obj['good_info']=prescription.project[b].good_info

              }

              tempProject.push(obj)
            }
            let obj = {
              id: 0,
              advices: tempAdvice,
              project: tempProject,
              addition:tempAddition,
              order_status:0,
              med_type: parseInt(prescription.med_type)
              // type: type,
            };
            prescriptions.push(obj)
          }
        }


        if(this.srcPrescriptions.length == 1){
          if(this.srcPrescriptions[0].advices.length == 0 && this.srcPrescriptions[0].project.length == 0){
            this.srcPrescriptions = [];
            this.srcPrescriptions = prescriptions;
            this.$emit('save', this.srcPrescriptions)
          }else{

            this.srcPrescriptions =  this.srcPrescriptions.concat(prescriptions);
            this.$emit('save', this.srcPrescriptions)
          }
        }else{
          this.srcPrescriptions =  this.srcPrescriptions.concat(prescriptions);
          this.$emit('save', this.srcPrescriptions)
        }



      },show(prescriptions,name,id,srcPrescriptions){
        this.srcPrescriptions = srcPrescriptions;
        this.patient_id = id;
        this.request_record_date = uParseTime(prescriptions[0].record_date, '{y}-{m}-{d}');
        this.name = name;
        this.allPrescription = prescriptions;
        for (let i = 0; i < this.allPrescription.length; i++){
          this.allPrescription[i].check_group = []
        }
        console.log(this.allPrescription);
        this.isLastOrNextVisible = true
      },close(){
        this.isLastOrNextVisible = false
      },hide(){
        this.$emit('cancel')

      },getTime(time, template){
        return uParseTime(time,template)

      }, getGroup(id){
        var name = "";
        var statistics_category =  getDictionaryDataConfig('system','statistics_category');
        for(let i=0;i<statistics_category.length;i++){
          if(id == statistics_category[i].id){
            name = statistics_category[i].name
          }
        }
        return name
      },   getDictionaryDataConfig(module, filed_name) {
        return getDictionaryDataConfig(module, filed_name)
      },
    }
  }
</script>

<style lang="scss">
.isLastOrNext{
  .el-dialog__body{
    padding:0px 20px 30px;
  }
  .quote{
    margin-top: 10px;
    font-size: 14px;
  }
}
</style>
