<template>
  <div>
    <div style="display: flex;justify-content: space-between;margin-bottom:10px;">
      <div>
        <el-select size="small" v-model="item_type" placeholder="请选择"
                   style="width:150px;margin-left:10px;" @change="changeItem">
          <el-option
              label="全部"
              value="0">
          </el-option>
          <el-option
              v-for="(item,index) in items"
              :key="index"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>

        <el-input
          size="small"
          style="width: 150px"
          v-model="keywords"
          class="filter-item"
          placeholder="请输入项目名称"
        />
        <el-button
          size="small"
          style="margin: 0 10px"
          class="filter-item"
          type="primary"
          @click="searchAction"
        >搜索
        </el-button>
        <el-date-picker
            v-model="chargeDate"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            @change="changeDate"
            end-placeholder="结束日期">
        </el-date-picker>
        <el-button  type="primary" size="small" @click="toPrint">打印</el-button>
      </div>
      <div>
      </div>
    </div>
    <el-table :data="tableData" border :row-style="{ color: '#303133' }" ref="table"
              :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}"
              max-height="600"
              v-loading="detail_loading"
              highlight-current-row>
      <el-table-column type="index" label="序号" width="60px" align="center">
        <template slot-scope="scope">
          {{scope.row.index}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="处方日期">
        <template slot-scope="scope">{{getTime(scope.row.record_date)}}</template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="费用编码">
        <template slot-scope="scope">
          {{scope.row.number}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="项目名称">
        <template slot-scope="scope">
          <span v-if="scope.row.advice_id > 0">{{scope.row.advice.advice_name}}</span>
          <span v-if="scope.row.project_id > 0">
            <span v-if="scope.row.project.type == 2">{{scope.row.project.project.project_name}}</span>
            <span v-if="scope.row.project.type == 3">{{scope.row.project.good_info.good_name}}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="规格">
        <template slot-scope="scope">
          <span v-if="scope.row.advice_id > 0">{{scope.row.advice.drug.dose}}{{scope.row.advice.drug.dose_unit}}*{{scope.row.advice.drug.min_number}}{{scope.row.advice.drug.min_unit}}/{{scope.row.advice.drug.max_unit}}</span>
          <span v-if="scope.row.project_id > 0">
            <span v-if="scope.row.project.type == 2">{{scope.row.project.project.project_name}}</span>
            <span v-if="scope.row.project.type == 3">{{scope.row.project.good_info.specification_name}}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="数量">
        <template slot-scope="scope">
          {{scope.row.cnt}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="单价">
        <template slot-scope="scope">
          {{scope.row.pric}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="金额">
        <template slot-scope="scope">
          <div>
            {{scope.row.total_price}}
          </div>
        </template>
      </el-table-column>
     
    </el-table>
     <div>总费用:{{getAllPice()}}</div>
     <div>个人支付:{{getActPay()}}</div>
     <div>基金支付记账:{{getFundPaySumamt()}}</div>
     <div>补充医疗支付记账:{{getHifesPay()}}</div>
    <div>救助支付金额:{{getMafPay()}}</div>
  </div>

  <!-- </div> -->
</template>


<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { getHisSummaryDetailList } from '@/api/his/his_tools'
const moment = require('moment')
import { uParseTime } from '@/utils/tools'
export default {
  components: {
    BreadCrumb

  },
  props: {
    patient_id: {
      type: Number,
      default: 0
    }
  },

  data() {

    return {
      detail_loading: false,
      tempArr: [],
      pos: 0,
      search_input: '',

      sameRowArr: [],
      keywords: '',
      tableData: [],
      chargeDate: [moment(new Date()).add('year', 0).format('YYYY-MM-DD'), moment(new Date()).add('year', 0).format('YYYY-MM-DD')],
      item_type: '0',
      items: [
        { id: 1, name: '药品' },
        { id: 3, name: '耗材' },
        { id: 2, name: '项目' }

      ],
      list:[],

    }
  },
  methods: {
    getTime(val) {
      if(val <= 0){
        return ""
      }
      if(val == ""){
      return ""
      }else {
      return uParseTime(val, '{y}-{m}-{d}')
      }
     },
    searchAction(){
     this.getHisSummaryDetailList()
    },
    changeDate() {
      this.getHisSummaryDetailList()
    },
    changeItem() {
      this.getHisSummaryDetailList()
    },
    getHisSummaryDetailList() {
      let start_time = this.chargeDate[0]
      let end_time = this.chargeDate[1]
      let params = {
        patient_id:this.patient_id,
        start_time: start_time,
        end_time: end_time,
        type: this.item_type,
        keyword: this.keywords
      }
    //  console.log("patient_id 233232323232323",this.patient_id)
     getHisSummaryDetailList(params).then(response=>{
       if(response.data.state == 1){
          var list =  response.data.data.list
         
          this.list = list
          this.tableData = []
          for(let i=0;i<list.length;i++){
            for(let j=0;j<list[i].orders.length;j++){
              for(let z=0;z<list[i].orders[j].order_info.length;z++){
                  list[i].orders[j].order_info[z].record_date = list[i].orders[j].settle_accounts_date
                  list[i].orders[j].order_info[z].number = list[i].orders[j].number
                  this.tableData.push(list[i].orders[j].order_info[z])
               }
            }
          }
          var obj = {index:"合计",total_price:0,record_date:"0"}
          for(let i=0;i<this.tableData.length;i++){
            this.tableData[i].index = i+1
            this.tableData[i].total_price = 0
            this.tableData[i].total_price = (this.tableData[i].cnt * this.tableData[i].pric).toFixed(2)
            obj.total_price += (this.tableData[i].cnt * this.tableData[i].pric)  
          }
          obj.total_price = (obj.total_price).toFixed(2)
          // console.log("tabledate23232332233223",this.tableData)
          this.tableData.push(obj)
          var new_arr = []
       
          if(this.keywords!=""){
            for(let i=0;i<this.tableData.length;i++){
              if(this.tableData[i].index!="合计"){
                if(this.tableData[i].advice_id > 0){
                  if(this.tableData[i].advice.advice_name.indexOf(this.keywords) > -1){
                  new_arr.push(this.tableData[i])
                }
                }
                if(this.tableData[i].project_id >0){
                  if(this.tableData[i].project.type == 2){
                    if(this.tableData[i].project.project.project_name.indexOf(this.keywords) > -1){
                        new_arr.push(this.tableData[i])
                    }
                  }
                  if(this.tableData[i].project.type == 3){
                    if(this.tableData[i].project.good_info.good_name.indexOf(this.keywords)> - 1){
                      new_arr.push(this.tableData[i])   
                    }
                  }
                }
              }
            }
            if(this.keywords!=""){
              this.tableData.push(obj)
            }
            this.tableData = new_arr
          }
       }
     })
    },
    getAllPice(){
      var total_price = 0
      for(let i=0;i<this.tableData.length;i++){
        if(this.tableData[i].index =="合计"){
          total_price = this.tableData[i].total_price
        }
      }
      return total_price
    },
    getActPay(){
      // console.log("hh23h323223323",this.list)
      var act_pay = 0
      for(let i=0;i<this.list.length;i++){
        for(let j=0;j<this.list[i].orders.length;j++){
           act_pay +=this.list[i].orders[j].acct_pay
        }
      }
      return act_pay
    },
    getFundPaySumamt(){
      var fund_pay_sumamt = 0
      for(let i=0;i<this.list.length;i++){
        for(let j=0;j<this.list[i].orders.length;j++){
           fund_pay_sumamt +=this.list[i].orders[j].fund_pay_sumamt
        }
      }
      return fund_pay_sumamt
    },
    getHifesPay(){
      var hifes_pay = 0
      for(let i=0;i<this.list.length;i++){
        for(let j=0;j<this.list[i].orders.length;j++){
          hifes_pay +=this.list[i].orders[j].hifes_pay
        }
      }
      return hifes_pay
    },
    getMafPay(){
      var maf_pay = 0
      for(let i=0;i<this.list.length;i++){
        for(let j=0;j<this.list[i].orders.length;j++){
          maf_pay +=this.list[i].orders[j].maf_pay
        }
      }
      return maf_pay
    },
    toPrint(){
      this.$router.push({path:"/hisTool/detailPrint?patient_id="+this.patient_id+"&start_time="+this.chargeDate[0]+"&end_time="+this.chargeDate[1]+"&type="+this.item_type +"&keyword="+this.keywords})
    }

  },
  created() {
    this.getHisSummaryDetailList()
  },
  watch:{
    "patient_id":function(){
      this.patient_id = this.patient_id
      this.getHisSummaryDetailList()
    }
  }
}
</script>
