<template>
  <div class="main-contain outpatientChargesManagement">
    <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>
      <div style="margin-bottom: 10px;">
        <el-date-picker
          size="small"
          v-model="start_time"
          prefix-icon="el-icon-date"
          @change="handleStartTimeChange"
          :editable="false"
          :clearable="false"
          style="width: 196px; "
          type="date"
          placeholder="选择开始日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          align="right"
        ></el-date-picker>
        <span style="text-align: center;">-</span>
        <el-date-picker
          size="small"
          v-model="end_time"
          prefix-icon="el-icon-date"
          @change="handleEndTimeChange"
          :editable="false"
          :clearable="false"
          style="width: 196px; "
          type="date"
          placeholder="选择结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          align="right"
        ></el-date-picker>
        <el-button size="small" type="primary"
                   @click="query">查询
        </el-button>
      </div>

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

        <el-table-column align="center" width="90" prop="name" label="患者名字">
          <template slot-scope="scope">{{ scope.row.patient.name }}</template>
        </el-table-column>

        <el-table-column align="center" width="90" prop="name" label="患者性别">
          <template slot-scope="scope">{{ scope.row.patient.gender == 1 ? '男':'女' }}</template>
        </el-table-column>


        <el-table-column
          align="center"
          width="100"
          prop="name"
          label="开票日期"
        >
          <template slot-scope="scope">
            {{getTimes(scope.row.ctime) }}
          </template>
        </el-table-column>


        <el-table-column align="center" width="90" prop="name" label="开票员">
          <template slot-scope="scope">
            {{scope.row.creator}}
          </template>
        </el-table-column>

        <el-table-column align="center" width="90" prop="name" label="医疗费总额">
          <template slot-scope="scope">
            {{scope.row.medfee_sumamt}}
          </template>
        </el-table-column>

        <el-table-column align="center" width="90" prop="name" label="基金支付">
          <template slot-scope="scope">
            {{scope.row.fund_pay_sumamt}}
          </template>
        </el-table-column>

        <el-table-column align="center" width="90" 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">

            <el-button v-if="scope.row.id > 0 && scope.row.is_red_washed == 1" size="mini" type="primary"
                       @click="hongchong(scope.row)">
              红冲
            </el-button>
            <el-button size="mini" type="primary" v-if="scope.row.id > 0 && scope.row.is_red_washed == 0"
                       @click="yulan(scope.row)">
              下载
            </el-button>
<!--            <el-button size="mini" type="primary" @click="download(scope.row)"  v-if="scope.row.id > 0 && scope.row.is_red_washed == 0">-->
<!--              下载-->
<!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 50, 100]"
        :page-size="10"
        :current-page.sync="page"
        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 { getDoctorList } from '@/api/his/his'


// import NewStatementPrint from './newStatementPrint'
import { adminMainView} from "@/api/role/admin";
import { fetchAllAdminUsers } from '@/api/doctor'
import axios from 'axios'
import { jsGetAge, uParseTime } from '@/utils/tools'
import {
  getFapiaoList,
} from '../../api/fapiao'

import { getDialysisRecordInitData } from '@/api/dialysis_record'
import { number } from 'echarts/lib/export'

const moment = require('moment')
export default {
  name: 'newSettleDetail',
  components: {
    BreadCrumb,
  },
  props: {
    patient_id: number
  },
  data() {
    return {
      staff:"",
      admins:[],
      orderObj: {},
      batchOrderObj: {},
      fapiao_number:"",
      med_options: [
        { id: 0, text: '全部' },
        { id: 11, text: '普通门诊' },
        { id: 14, text: '门诊特殊病' }
      ],
      zone_options: [
        { id: 0, text: '全部分区' }
      ],
      statementListVisible: false,
      statementVisible: false,
      batchStatementVisible: false,
      statementVisible9504: false,
      orderObj9504: {},
      crumbs: [
        { path: false, name: '门诊收费' },
        { path: false, name: '项目消费明细汇总' }
      ],
      tableData: [],
      tableData2:[],
      selecting_schs: [],
      dialogfapiaoVisible:false,
      targeOrderId:0,
      targeObj:{},
      limit: 10,
      page: 1,
      keywords: '',
      sch_type: '0',//班次
      zoneVal: 0,//分区
      medTypeVal: 0,
      start_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
      end_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
      pay_time:"",
      total: '',
      doctors: [],
      sort_type: 1,
      sort_types: [
        { value: 1, label: '处方时间' },
        { value: 2, label: '结算时间' }
      ],
      admin_user_id: '',
      listVisible: false,
      allListVisible: false,
      adminUserOptions: [],
      invoiceVisible: false,
      paramsObj: {},
      invoiceParams: {},
      settlementVisible: false,
      settlementObj: {},
      accounts_click_vi:false,
    }
  },

  created() {
    // this.getInitData()
    // this.fetchAllAdminUsers()
    // this.getDoctorList()
    this.getHisOrderList()
    // this.getAllstaff()
  },

  methods: {
    getData(patient_id){
      this.patient_id = patient_id
      this.getHisOrderList()

    },
    auth(){

    },
    query(){

    },download(){


    },
    blue(row){

    }, yulan(row){


    },hongchong(row){

    },
    getAllstaff(){
      adminMainView()
        .then(rs => {

          var resp = rs.data;
          if (resp.state === 1) {
            var adminData = resp.data.admins
            this.admins.push(...adminData);
          } else {
            this.$message.error(resp.msg);
          }
        })
        .catch(err => {
          this.$message.error(err);
        });


    },

    handleSelectionChange(val) {
      this.selecting_schs = val
    },
    handleSchType(val) {
      this.sch_type = val
      this.getHisOrderList()
    }, handleMedChange() {
      this.getHisOrderList()
    },
    handleZoneChange(val) {
      this.zoneVal = val
      this.getHisOrderList()

    },
    getInitData: function() {
      getDialysisRecordInitData().then((rs) => {
        var resp = rs.data
        if (resp.state == 1) {
          var zones = resp.data.zones
          var zone_options = [{ id: 0, text: '全部' }]
          for (let z_i = 0; z_i < zones.length; z_i++) {
            const zone = zones[z_i]
            zone_options.push({ id: zone.id, text: zone.name })
          }
          this.zone_options = zone_options

        } else {
          this.$message.error(resp.msg)
        }
      })
    },



    getMedType(med_type) {
      var med_type = parseInt(med_type)
      switch (med_type) {
        case 11:
          return '普通门诊'
          break
        case 12:
          return '门诊挂号'
          break
        case 13:
          return '急诊'
          break
        case 14:
          return '门诊特殊病'
          break
        case 15:
          return '门诊统筹'
          break
        case 16:
          return '门诊慢性病'
          break
        case 21:
          return '普通住院'
          break
        case 140104:
          return '城乡门诊特殊病(140104)'
          break
        case 992102:
          return '单病种(992102)'
          break
      }
    },


    fetchAllAdminUsers() {
      fetchAllAdminUsers().then((response) => {
        if (response.data.state == 1) {
          this.adminUserOptions = response.data.data.users
        }
      })
    },
    getName(admin_user_id) {
      for (let i = 0; i < this.adminUserOptions.length; i++) {
        if (this.adminUserOptions[i].id == admin_user_id) {
          return this.adminUserOptions[i].name
        }
      }
    },



    handleSizeChange(limit) {
      this.limit = limit
      this.getHisOrderList()
    },
    handleCurrentChange(page) {
      this.page = page
      this.getHisOrderList()
    },
    handleStartTimeChange() {
      this.page = 1
      this.keywords = ''
      this.getHisOrderList()
    },
    handleEndTimeChange() {
      this.page = 1
      this.keywords = ''
      this.getHisOrderList()
    },

    getTimes(time) {
      return uParseTime(time, '{y}-{m}-{d}')
    },

    getTime(value, temp) {
      if (value != undefined) {
        return uParseTime(value, temp)
      }
      return ''
    },
    getHisOrderList() {
      let params = {
        page: this.page,
        limit: this.limit,
        keywords: this.keywords,
        start_time: this.start_time,
        end_time: this.end_time,
      }
      getFapiaoList(params).then((response) => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg)
          return false
        } else {
          this.tableData = response.data.data.fapiao_record
          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
        }
      })
    },
    open(index) {
      if (index == 1) {
        this.listVisible = true
      } else if (index == 2) {
        this.allListVisible = true
      }
    }, unique(array) {
      // res用来存储结果
      var res = []
      for (var i = 0, arrayLen = array.length; i < arrayLen; i++) {
        for (var j = 0, resLen = res.length; j < resLen; j++) {
          if (array[i].id === res[j].id && array[i].price === res[j].price) {
            break
          }
        }
        // 如果array[i]是唯一的，那么执行完循环，j等于resLen
        if (j === resLen) {
          res.push(array[i])
        }
      }
      return res
    },camcleModifyFaPiaoCode(){
      this.dialogfapiaoVisible = false
      this.invoiceVisible = true
      var obj = this.targeObj
      let paramsObj = {
        order_id: obj.id,
        patient_id: obj.patient_id,
        number: obj.mdtrt_id,
        name: obj.patient.name,
        age: obj.age,
        gend: obj.patient.gender,
        setl_time: obj.setl_time,
        chargeName: this.getName(obj.creator)
      }
      this.dialogfapiaoVisible = false
      this.invoiceParams = paramsObj
      this.invoiceVisible = true
    },modifyFaPiaoCode(){
      let params = {
        id:this.targeOrderId,
        fapiao_number: this.fapiao_number
      }
      modifyFapiaoCodetwo(params).then((response) => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg)
          return false
        } else {
          var obj = this.targeObj
          let paramsObj = {
            order_id: obj.id,
            patient_id: obj.patient_id,
            number: obj.mdtrt_id,
            name: obj.patient.name,
            age: obj.age,
            gend: obj.patient.gender,
            setl_time: obj.setl_time,
            chargeName: this.getName(obj.creator)
          }
          this.dialogfapiaoVisible = false
          this.invoiceParams = paramsObj
          this.invoiceVisible = true
          this.getHisOrderList()
        }
      })

    },

    invoicePrint(obj) {
      if(this.$store.getters.xt_user.org_id == 10480 || this.$store.getters.xt_user.org_id == 0){
        this.fapiao_number  = obj.fa_piao_number
        this.dialogfapiaoVisible = true
        this.targeOrderId = obj.id
        this.targeObj = obj
      }else{
        let paramsObj = {
          order_id: obj.id,
          patient_id: obj.patient_id,
          number: obj.mdtrt_id,
          name: obj.patient.name,
          age: obj.age,
          gend: obj.patient.gender,
          setl_time: obj.setl_time,
          chargeName: this.getName(obj.creator)
        }
        this.dialogfapiaoVisible = false
        this.invoiceParams = paramsObj
        this.invoiceVisible = true


      }


    }
  }
}
</script>
<style lang="scss">
.table{
tr td{
  padding:5px 0;
}
}
</style>
