<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"
      show-summary
      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" label="处方日期">
        <template slot-scope="scope">{{
          getTime(scope.row.record_date)
        }}</template>
      </el-table-column>
      <el-table-column align="center" label="费用编码">
        <template slot-scope="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>
      <el-table-column align="center" 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" 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" label="数量">
        <template slot-scope="scope">
          {{ scope.row.cnt }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="单位">
        <template slot-scope="scope">
          <span v-if="scope.row.advice_id > 0">{{
            scope.row.advice.prescribing_number_unit
          }}</span>
          <span v-if="scope.row.project_id > 0">
            <span v-if="scope.row.project.type == 2">{{
              scope.row.project.project.unit
            }}</span>
            <span v-if="scope.row.project.type == 3">{{
              scope.row.project.good_info.packing_unit
            }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="单价">
        <template slot-scope="scope">
          {{ scope.row.pric }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="金额" prop="total_price">
        <template slot-scope="scope">
          <div>
            {{ scope.row.total_price }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="备注">
        <template slot-scope="scope">
          <div>
            <span v-if="scope.row.advice_id > 0">{{
              scope.row.advice.remark
            }}</span>
            <span v-if="scope.row.project_id > 0">
              <span v-if="scope.row.project.type == 2">{{
                scope.row.project.remark
              }}</span>
              <span v-if="scope.row.project.type == 3">{{
                scope.row.project.remark
              }}</span>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex; justify-content: space-around; margin-top: 25px">
      <div>总费用:{{ getAllPice() }}</div>
      <div>
        个人支付: 
        {{getActPay()}}
        <!-- <span v-if="his_patient.balance_accounts_type == 2">{{ getAllPice() }}</span>  
        <span v-if="his_patient.balance_accounts_type != 2">{{ getActPay() }}</span> -->
      
      </div>
      <div>基金支付记账:{{ getFundPaySumamt() }}</div>
      <div>补充医疗支付记账:{{ getHifesPay() }}</div>
      <div>救助支付金额:{{ getMafPay() }}</div>
    </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: 2, name: "项目" },
        { id: 3, name: "耗材" },
      ],
      list: [],
      his_patient: {},
      id: 0,
      start_time: "",
      end_time: "",
      org_id:0,
    };
  },
  methods: {
    getTime(val) {
      if (val <= 0) {
        return "";
      }
      if (val == "") {
        return "";
      } else {
        return uParseTime(val, "{y}-{m}-{d}");
      }
    },
    searchAction() {
      let table_id = this.$store.getters.pagedata.list.table_id;
      if (table_id == undefined) {
        this.$store.commit("SET_PAGEDATA", {
          table_id: 0,
          type_id: this.id,
          keywords: this.keywords,
          start_time: this.chargeDate[0],
          end_time: this.chargeDate[1],
          patient_id: this.patient_id,
        });
      } else {
        this.$store.commit("SET_PAGEDATA", {
          table_id: table_id,
          type_id: this.id,
          keywords: this.keywords,
          start_time: this.chargeDate[0],
          end_time: this.chargeDate[1],
          patient_id: this.patient_id,
        });
      }
      this.getHisSummaryDetailList();
    },
    changeDate() {
      this.start_time = this.chargeDate[0];
      this.end_time = this.chargeDate[1];
      this.getHisSummaryDetailList();
    },
    changeItem(id) {
      this.id = id;
      let table_id = this.$store.getters.pagedata.list.table_id;
      if (table_id == undefined) {
        this.$store.commit("SET_PAGEDATA", {
          table_id: 0,
          type_id: this.id,
          keywords: this.keywords,
          start_time: this.chargeDate[0],
          end_time: this.chargeDate[1],
          patient_id: this.patient_id,
        });
      } else {
        this.$store.commit("SET_PAGEDATA", {
          table_id: table_id,
          type_id: this.id,
          keywords: this.keywords,
          start_time: this.chargeDate[0],
          end_time: this.chargeDate[1],
          patient_id: this.patient_id,
        });
      }
      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,
      };
      getHisSummaryDetailList(params).then((response) => {
        if (response.data.state == 1) {
          var list = response.data.data.list;
          this.his_patient = response.data.data.his_patient;
          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);

          var new_arr = [];

          let list_1 = this.$store.getters.pagedata.list;
          if (list_1.keywords == undefined) {
            this.keywords = "";
          } else {
            this.keywords = list_1.keywords;
          }
          this.id = list_1.type_id;
          this.item_type = this.id;
          if(this.item_type == undefined){
            this.item_type = "0"
          }else{
            this.item_type = this.id;
          }

          
          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]);
                    }
                  }
                }
              }
            }
            var objs = { index: "合计", total_price: 0, record_date: "0" };
            objs.total_price = this.getPrice(new_arr);
            this.tableData = new_arr;
          }

          if (this.id == 1) {
            var obj = { index: "合计", total_price: 0, record_date: "0" };
            var new_arr = [];
            for (let i = 0; i < this.tableData.length; i++) {
              if (this.tableData[i].index != "合计") {
                if (this.tableData[i].advice_id > 0) {
                  new_arr.push(this.tableData[i]);
                }
              }
            }
            this.tableData = [];
            obj.total_price = this.getPrice(new_arr);
            this.tableData = new_arr;
          }
          if (this.id == 2) {
            var obj = { index: "合计", total_price: 0, record_date: "0" };
            var new_arr = [];
            for (let i = 0; i < this.tableData.length; i++) {
              if (this.tableData[i].index != "合计") {
                if (this.tableData[i].project_id > 0) {
                  if (this.tableData[i].project.type == 2) {
                    new_arr.push(this.tableData[i]);
                  }
                }
              }
            }
            this.tableData = [];
            obj.total_price = this.getPrice(new_arr);
            this.tableData = new_arr;
          }

          if (this.id == 3) {
            var new_arr = [];
            var obj = { index: "合计", total_price: 0, record_date: "0" };
            for (let i = 0; i < this.tableData.length; i++) {
              if (this.tableData[i].index != "合计") {
                if (this.tableData[i].project_id > 0) {
                  if (this.tableData[i].project.type == 3) {
                    new_arr.push(this.tableData[i]);
                  }
                }
              }
            }
            this.tableData = [];
            obj.total_price = this.getPrice(new_arr);
            this.tableData = new_arr;
          }
        }
      });
    },

    getAllPice() {
      var total_price = 0;
      for (let i = 0; i < this.tableData.length; i++) {
        total_price += this.tableData[i].pric * this.tableData[i].cnt;
      }
      return total_price.toFixed(2);
    },
    getActPay() {
      var arrNew = []
      var act_pay = 0;
      for (let i = 0; i < this.list.length; i++) {
        for (let j = 0; j < this.list[i].orders.length; j++) {
          //个人支付
          console.log("is",this.list[i].orders[j])
          if(this.list[i].orders[j].is_medicine_insurance == 0){
             arrNew.push(this.list[i].orders[j])
          }
        }
      }
      if(arrNew.length > 0){
        for(let i=0;i<arrNew.length;i++){
           for(let j=0;j<arrNew[i].order_info.length;j++){
             act_pay += arrNew[i].order_info[j].cnt * arrNew[i].order_info[j].pric
           }
        }
      }
      console.log("arrNew2332323223",arrNew)
      return act_pay.toFixed(2);
    },
    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.start_time +
          "&end_time=" +
          this.end_time +
          "&type=" +
          this.item_type +
          "&keyword=" +
          this.keywords +
          "&id=" +
          this.id +
          "&balance_accounts_type=" +
          this.his_patient.balance_accounts_type,
      });

      // ======打印时将查询参数保存至store中======
      // table_id: 左栏表格下标
      // type_id: 下拉框id
      // keywords: 输入框
      // start_time: this.chargeDate[0],
      // end_time: this.chargeDate[1],
      // patient_id:this.patient_id
      let table_id = this.$store.getters.pagedata.list.table_id;
      if (table_id == undefined) {
        this.$store.commit("SET_PAGEDATA", {
          table_id: 0,
          type_id: this.id,
          keywords: this.keywords,
          start_time: this.chargeDate[0],
          end_time: this.chargeDate[1],
          patient_id: this.patient_id,
        });
      } else {
        this.$store.commit("SET_PAGEDATA", {
          table_id: table_id,
          type_id: this.id,
          keywords: this.keywords,
          start_time: this.chargeDate[0],
          end_time: this.chargeDate[1],
          patient_id: this.patient_id,
        });
      }
    },
    getPrice(val) {
      var total_price = 0;
      for (let i = 0; i < val.length; i++) {
        total_price += val[i].cnt * val[i].pric;
      }
      return total_price.toFixed(2);
    },
  },
  created() {
    let list_1 = this.$store.getters.pagedata.list;
          if (!list_1.start_time ||list_1.start_time == "") {
            this.chargeDate[0] = this.chargeDate[0] = moment(new Date()).add("year", 0).format("YYYY-MM-DD");;
          } else {
            this.chargeDate[0] = list_1.start_time;
          }
           if (!list_1.end_time ||list_1.end_time == "") {
            this.chargeDate[1] = this.chargeDate[0] = moment(new Date()).add("year", 0).format("YYYY-MM-DD");;
          } else {
            this.chargeDate[1] = list_1.end_time;
          }
    this.start_time = this.chargeDate[0];
    this.end_time = this.chargeDate[1];
    this.getHisSummaryDetailList();
  },
  watch: {
    patient_id: function () {
      this.patient_id = this.patient_id;
      this.getHisSummaryDetailList();
    },
  },
};
</script>
