<template>
  <div class="main-contain incomeStatistics">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
    </div>
    <div class="app-container">
      <div style="display: flex;justify-content: space-between;margin-bottom:10px;">
        <div>
          <el-input size="small" style="width:150px;"  v-model="keywords"
                    class="filter-item"/>
          <el-button size="small" style="margin-left:10px;" class="filter-item" type="primary" @click="searchAction">
            搜索
          </el-button>

          <el-date-picker size="small" v-model="start_time" prefix-icon="el-icon-date"
                          @change="handleStartTimeChange" :editable="false" :clearable="false"
                          style="width: 196px;margin-right:10px;" type="date" placeholder="选择开始日期"
                          format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd"
                          align="right"></el-date-picker>

          <el-date-picker size="small" v-model="end_time" prefix-icon="el-icon-date"
                          @change="handleEndTimeChange" :editable="false" :clearable="false"
                          style="width: 196px;margin-right:10px;" type="date" placeholder="选择结束日期"
                          format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd"
                          align="right"></el-date-picker>
          <el-radio v-model="radio" label="1" @change="changeType">汇总</el-radio>
          <el-radio v-model="radio" label="2"  @change="changeType">明细</el-radio>
        </div>
        <el-button size="small" type="primary" @click="export_detail">报表下载</el-button>
      </div>
      <el-table :data="tableData" border style="width: 100%;" :row-style="{ color: '#303133' }"
                :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" highlight-current-row  v-if="radio == '1'">
        <el-table-column
          prop="date"
          label="序号"
          width="60"
          align="center"
          type="index"
        >
        </el-table-column>
        <el-table-column align="center" prop="name" label="收费日期">
          <template slot-scope="scope" >{{ scope.row.settle_accounts_date }}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="总金额">
          <template slot-scope="scope">{{scope.row.medfee_sumamt}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="医保统筹金额">
          <template slot-scope="scope">{{scope.row.fund_pay_sumamt}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="大额基金金额">
          <template slot-scope="scope">{{scope.row.hifob_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="大病基金金额">
          <template slot-scope="scope">{{scope.row.hifmi_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="补充保险基金金额">
          <template slot-scope="scope">{{scope.row.hifes_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="医疗救助基金金额">
          <template slot-scope="scope">{{scope.row.maf_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="公务员基金">
          <template slot-scope="scope">{{scope.row.cvlserv_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="个人支付基金">
          <template slot-scope="scope">{{scope.row.psn_cash_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="折扣金额">
          <template slot-scope="scope">{{scope.row.discount_price}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="优惠金额">
          <template slot-scope="scope">{{scope.row.preferential_price}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="个人账户支付金额">
          <template slot-scope="scope">{{scope.row.acct_pay}}</template>
        </el-table-column>
        <!--<el-table-column align="center" prop="name" label="预交金">-->
          <!--<template slot-scope="scope">{{scope.row.medfee_sumamt}}</template>-->
        <!--</el-table-column>-->

        <!--<el-table-column align="center" prop="name" label="现金">-->
          <!--<template slot-scope="scope">{{scope.row.medfee_sumamt}}</template>-->
        <!--</el-table-column>-->

        <!--<el-table-column align="center" prop="name" label="微信">-->
          <!--<template slot-scope="scope">{{scope.row.medfee_sumamt}}</template>-->
        <!--</el-table-column>-->

        <!--<el-table-column align="center" prop="name" label="操作">-->
          <!--<template slot-scope="scope">-->
            <!--<el-button size="mini" type="primary" @click="toDetail(scope.row)">详情</el-button>-->
          <!--</template>-->
        <!--</el-table-column>-->
      </el-table>

      <el-table :data="tableData" border style="width: 100%;" :row-style="{ color: '#303133' }"  v-if="radio == '2'"
                :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" highlight-current-row>
        <el-table-column
                prop="date"
                label="序号"
                width="60"
                align="center"
                type="index"
        >
        </el-table-column>

        <el-table-column align="center" prop="name" label="收费日期">
          <template slot-scope="scope" >{{ scope.row.setl_time }}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="姓名">
          <template slot-scope="scope" >{{ scope.row.psn_name }}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="姓名">
          <template slot-scope="scope" >{{ scope.row.psn_name }}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="总金额">
          <template slot-scope="scope">{{scope.row.medfee_sumamt}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="基金支付">
          <template slot-scope="scope">{{scope.row.fund_pay_sumamt}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="个人支付基金">
          <template slot-scope="scope">{{scope.row.psn_cash_pay}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="床位费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'01')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="诊察费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'02')}}</template>
        </el-table-column>


        <el-table-column align="center" prop="name" label="检查费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'03')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="化验费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'04')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="治疗费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'05')}}</template>
        </el-table-column>


        <el-table-column align="center" prop="name" label="手术费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'06')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="护理费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'07')}}</template>
        </el-table-column>


        <el-table-column align="center" prop="name" label="卫生材料费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'08')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="西药费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'09')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="中药饮片费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'10')}}</template>
        </el-table-column>


        <el-table-column align="center" prop="name" label="中成药费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'11')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="一般诊疗费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'12')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="挂号费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'13')}}</template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="其他费">
          <template slot-scope="scope">{{getPrice(scope.row.order_info,'14')}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="医保支付">
          <template slot-scope="scope">{{scope.row.psn_cash_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="现金">
        <template slot-scope="scope">{{scope.row.psn_cash_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="微信">
        <template slot-scope="scope">{{scope.row.psn_cash_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="支付宝">
          <template slot-scope="scope">{{scope.row.psn_cash_pay}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="银行卡">
          <template slot-scope="scope">{{scope.row.psn_cash_pay}}</template>
        </el-table-column>

      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 50, 100]"
        :page-size="10"
        background
        style="margin-top:20px;float: right"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"

      >
      </el-pagination>
    </div>


  </div>
</template>


<script>
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import { getIncomeStatisticsDataList,getDoctorList,getExportConsumeDetailList } from '@/api/his/his'
  import { uParseTime } from '@/utils/tools'

  export default {
    components: {
      BreadCrumb
    },
    data() {
      return {
        crumbs: [
          { path: false, name: '门诊收费' },
          { path: false, name: '项目消费明细汇总' }
        ],
        tableData: [],
        radio:'1',
        limit:10,
        page:1,
        keywords:"",
        start_time:"",
        end_time:"",
        total:"",
        doctors:[],
        admin_user_id:"",

      }
    },
    methods: {
      getPrice(order_info, number){





      },
      changeType(val){

        let params= {
          page : this.page,
          limit: this.limit,
          keywords:this.keywords,
          start_time:this.start_time,
          end_time: this.end_time,
          statistics_type: this.radio
        }
        getIncomeStatisticsDataList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.tableData = response.data.data.list
            this.total = response.data.data.total
          }
        })


      },
      export_detail(){
        let params= {
          start_time:this.start_time,
          end_time: this.end_time,
        }
        getExportConsumeDetailList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {

            let list = []
            for (let i =0; i < response.data.data.order.length; i++){
            let order =   response.data.data.order[i]
              let number = ""
              let name = ""
              let department = ""
              let doctor_name = ""
              let balance_accounts = ""

              let order_status = ""



              if(order.his_patient != null && order.his_patient.number.length > 0){
                number = order.his_patient.number

              }

              if(order.his_patient != null ){
                if(order.his_patient.balance_accounts_type == 1){
                  balance_accounts = "自费"
                }

                if(order.his_patient.balance_accounts_type == 2){
                  balance_accounts = "医保"
                }
              }

              if(order.patient != null && order.patient.name.length > 0){
                name = order.patient.name

              }

              if(order.p_info != null && order.p_info.department != null && order.p_info.department.name.length > 0){
                department = order.p_info.department.name

              }

              if(order.p_info != null && order.p_info.doctor.length > 0){
                doctor_name = order.p_info.doctor
              }

              if(order.order_status == 1){
                order_status = "待结算"
              }

              if(order.order_status == 2){
                order_status = "已结算"
              }

              if(order.order_status == 3){
                order_status = "已退费"
              }


              let obj = {
                "就诊号":number,
                "患者姓名":name,
                "应收金额":order.medfee_sumamt,
                "实收金额":order.medfee_sumamt,
                "科室":department,
                "医生姓名":doctor_name,
                "收费类别":balance_accounts,
                "收费者姓名":doctor_name,
                "总金额":order.medfee_sumamt,
                "现金支付":"",
                "账户支付":"",
                "支付宝支付":"",
                "微信支付":"",
                "其他支付":"",
                "收费日期":"",
                "收费状态":order_status,
                "退费日期":"",
              }
              list.push(obj)
            }
            import('@/vendor/Export2Excel').then(excel => {
              const tHeader = ['就诊号', '患者姓名', '应收金额', '实收金额', '科室',"医生姓名","收费类别","收费者姓名","总金额","现金支付","账户支付","支付宝支付","微信支付","其他支付","收费日期","收费状态","退费日期"]
              const filterVal = ['就诊号', '患者姓名', '应收金额', '实收金额', '科室',"医生姓名","收费类别","收费者姓名","总金额","现金支付","账户支付","支付宝支付","微信支付","其他支付","收费日期","收费状态","退费日期"]
              const data = this.formatJson(filterVal, list)
              excel.export_json_to_excel({
                header: tHeader,
                data,
                filename: "消费明细"
              })
            })

          }
        })

      },  formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },

      handleSizeChange(limit){
        this.limit = limit;
        this.getIncomeStatisticsDataList()

      },handleCurrentChange(page){
        this.page = page;
        this.getIncomeStatisticsDataList()


      },
      handleStartTimeChange(){
        this.page = 1
        this.keywords=""
        this.getIncomeStatisticsDataList()
      },handleEndTimeChange(){
        this.page = 1
        this.keywords=""
        this.getIncomeStatisticsDataList()
      },
      searchAction(){
        this.page = 1
        this.getIncomeStatisticsDataList()


      },
      getTimes(time) {
        return uParseTime(time, "{y}-{m}-{d}");
      },
      toDetail(row) {
        this.$router.push('/outpatientCharges/summaryDetail?patient_id='+row.patient.id+"&number="+row.number)
      }, getTime(value, temp) {
        if (value != undefined) {
          return uParseTime(value, temp)
        }
        return ''
      },getIncomeStatisticsDataList(){
        let params= {
          page : this.page,
          limit: this.limit,
          keywords:this.keywords,
          start_time:this.start_time,
          end_time: this.end_time,
          statistics_type: this.radio
        }
        getIncomeStatisticsDataList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.tableData = response.data.data.list
            this.total = response.data.data.total
          }
        })

      },getDoctorList(){
        getDoctorList().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.doctors = response.data.data.doctors
          }
        })


      }
    }, created() {
     this.getDoctorList()
     this.getIncomeStatisticsDataList()


    }
  }
</script>
