<template>
  <div class="prescriptionTable">
    <el-table v-if="activeType  == 1" :data="prescription.advices" 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" prop="drug_name" label="名称">
        <template slot-scope="scope">{{ scope.row.drug_name }}</template>
      </el-table-column>

      <el-table-column align="center" prop="single_dose" width="80" :label="'单次\n用量'">
        <template slot-scope="scope">
          <div style="display:flex;align-items:center;">
            <el-input v-model="scope.row.single_dose" style="width:60%;"></el-input>
            <div>{{scope.row.min_unit}}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="delivery_way" width="100" label="用法">
        <template slot-scope="scope">
          <el-select v-model="scope.row.delivery_way" placeholder="请选择">
            <el-option
              v-for="(item,index) in drugways"
              :key="index"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="execution_frequency" width="100" label="频率">
        <template slot-scope="scope">
          <el-select v-model="scope.row.execution_frequency" placehold er="请选择">
            <el-option
              v-for="item,index in efs"
              :key="index"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="prescribing_number" width="80" label="总量">
        <template slot-scope="scope">
          <div style="display:flex;align-items:center;">
            <el-input v-model="scope.row.prescribing_number" style="width:60%" placeholder=""></el-input>
            <div> {{scope.row.min_unit}}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="retail_price" width="60" label="单价">
        <template slot-scope="scope">
          <el-input v-model="scope.row.retail_price" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="remark" width="50" label="备注">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remark" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" width="40" prop="name" label="操作">
        <template slot-scope="scope">
          <i class="el-icon-delete" @click="deleteDrug(scope.$index, scope.row)"></i>
        </template>
      </el-table-column>
    </el-table>

    <el-table v-if="activeType == 2" :data="prescription.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="40" label="序号"></el-table-column> -->
      <el-table-column align="center" prop="project_name" label="名称">
        <template slot-scope="scope">{{ scope.row.project_name }}</template>
      </el-table-column>
      <el-table-column align="center" prop="statistical_classification" width="100" label="组">
        <template slot-scope="scope">
          {{getGroup(scope.row.statistical_classification)}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="single_dose" width="80" :label="'单次\n用量'">
        <template slot-scope="scope">
          <el-input v-model="scope.row.single_dose" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="delivery_way" width="50" label="用法">
        <template slot-scope="scope">
          <el-input v-model="scope.row.delivery_way" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="execution_frequency" width="50" label="频率">
        <template slot-scope="scope">
          <el-input v-model="scope.row.execution_frequency" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="number_days" width="50" label="天数">
        <template slot-scope="scope">
          <el-input v-model="scope.row.number_days" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="total" width="50" label="总量">
        <template slot-scope="scope">
          <div style="display:flex;">
            <el-input v-model="scope.row.total" style="width:50" placeholder=""></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" width="50" label="单价">
        <template slot-scope="scope">
          <el-input v-model="scope.row.price" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" width="50" label="备注">
        <template slot-scope="scope">
           <el-input v-model="scope.row.remark"></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" width="40" prop="name" label="操作">
        <template slot-scope="scope">
          <i class="el-icon-delete" @click="deleteProject(scope.row,scope.$index)"></i>
        </template>
      </el-table-column>
    </el-table>

    <!--<div class="additionalBox">-->
      <!--<div class="additionalOne" v-for="(item,index) in 8" :key="index">-->
        <!--<span>治疗费</span>-->
        <!--<el-input v-model="input" placeholder="" style="width:50px;"></el-input>-->
        <!--共-->
        <!--<el-input v-model="input" placeholder="" style="width:50px;"></el-input>-->
        <!--次-->
        <!--<i class="el-icon-delete deleteIcon"></i>-->
      <!--</div>-->
    <!--</div>-->



  </div>
</template>

<script>
  import { getDictionaryDataConfig} from "@/utils/data";
  import { getInitData,delHisAdvice,delHisProject } from '@/api/his/his'

  export default {
    props: {
      preDrugs: Array,
      activeType: Number,
      prescription:{
        type:Object,
        default: function () {
          return {
            name:"",
            advices:[],
            project:[],
            drugways:[],
            efs:[],
          };
        }
      },
    },
    data() {
      return {
        advices:[],
        tableData: [],
        newoptions: [{
          value: '1',
          label: '1'
        }, {
          value: '2',
          label: '2'
        }, {
          value: '3',
          label: '3'
        }, {
          value: '4',
          label: '4'
        }, {
          value: '5',
          label: '5'
        }],
        value: '1',
        input: 1,
      }
    },
   methods:{
      getInitData(){
        getInitData().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.drugways = response.data.data.drugways
            this.efs = response.data.data.efs
          }
        })

      },deleteDrug:function(index, row){
       if(this.curPrescriptions.order_status >= 2){
         this.$message.error('该处方已经结算或者退费，无法删除')
         return
       }
       this.$confirm("药品删除后不可恢复，是否确认删除", "删除", {
         confirmButtonText: "确 定",
         cancelButtonText: "取 消",
         type: "warning"
       }).then(() => {
         this.$nextTick(function(){
           // this.prescription.advices.splice(index, 1)
           if(row.advice_id == 0){
           }else{
             let params = {
               'id': row.advice_id,
             }
             delHisAdvice(params).then(response => {
               if (response.data.state == 0) {
                 this.$message.error(response.data.msg)
                 return false
               } else {
                 var temp2 = this.deepClone(this.prescription.advices)
                 temp2.splice(index, 1)
                 this.prescription.advices = temp2
                 this.$message.success(response.data.data.msg)
               }
             })


           }

         });


       })
         .catch(() => {});





      },deepClone(source) {
       if (!source && typeof source !== 'object') {
         throw new Error('error arguments', 'shallowClone')
       }
       const targetObj = source.constructor === Array ? [] : {}
       Object.keys(source).forEach((keys) => {
         if (source[keys] && typeof source[keys] === 'object') {
           targetObj[keys] = this.deepClone(source[keys])
         } else {
           targetObj[keys] = source[keys]
         }
       })
       return targetObj
     },

      setNewData:function(data){
        this.prescription = data

        // this.prescription.advices = data.advices
      },
      getDictionaryDataConfig(module, filed_name) {
        return getDictionaryDataConfig(module, filed_name)
      },
      getGroup(id){
         var name = ""
          var statistics_category =  getDictionaryDataConfig('system','statistics_category')
          console.log("2235",statistics_category)
          for(let i=0;i<statistics_category.length;i++){
             if(id == statistics_category[i].id){
                 name = statistics_category[i].name
             }
          }
         return name
      },
      deleteProject(row,i){
        if(this.curPrescriptions.order_status >= 2){
          this.$message.error('该处方已经结算或者退费，无法删除')
          return
        }
        this.$confirm("项目删除后不可恢复，是否确认删除", "删除", {
          confirmButtonText: "确 定",
          cancelButtonText: "取 消",
          type: "warning"
        }).then(() => {
          if(row.id == 0){
            this.prescription.project.splice(i, 1)

          }else{
            let params = {
              'id': row.id,
            }
            delHisProject(params).then(response => {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg)
                return false
              } else {
                for (let i = 0; i < this.prescription.project.length; i++){
                  if(this.prescription.project[i].id == row.id){
                    this.prescription.project.splice(i, 1)
                  }
                }
                this.$message.success(response.data.data.msg)
              }
            })
          }
        })
          .catch(() => {});

      },
    },mounted(){
      this.getInitData()
    },
  }
</script>

<style lang="scss">
  .prescriptionTable {
    .additionalBox{
      margin-top: 20px;
      display: flex;
      flex-wrap: wrap;
      .additionalOne{
        margin-right:20px;
        margin-bottom:10px;
        display: flex;
        align-items: center;
        >span{
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          width:60px;
          display: inline-block;
          font-size: 14px;
        }
      }
      .deleteIcon{
        color:red;
        margin-left:5px;
      }
    }
    .el-table th .cell, .el-table td .cell {
      padding: 0 2px;
      white-space: pre-line;
    }

    .el-icon-delete {
      color: red;
    }

  }
</style>
