<template>
    <div id='invoice-print'>
        <div v-for='(i,index) in pageArr.length' :key="index" >
            <div :style="{position: 'absolute',top:(20 + (index * 660))  + 'px',left:200+ 'px',}">盐城大丰悦达金骆驼血液透析中心</div>
            <div :style="{position: 'absolute',top:(70 + (index * 660))  + 'px',left:150+ 'px',}">{{ list.order_number }}</div>
            <div style="display:flex;justify-content: space-between;">
                <div :style="{position: 'absolute',top:(100 + (index * 660))  + 'px',left:100+ 'px',}">{{ paramsObj.name }}</div>
            </div>
            <div :style="{position: 'absolute',top:(190 + (index * 660))  + 'px',left:120+ 'px',}">
                <div v-if="list.westernMedicineCostTotal">西药 {{ list.westernMedicineCostTotal }}</div>
                <div v-if="list.treatCostTotal">治疗费 {{ list.treatCostTotal }}</div>
                <div v-if="list.bedCostTotal">床位费 {{ list.bedCostTotal }}</div>
                <div v-if="list.chineseTraditionalMedicineCostTotal">中成药 {{ list.chineseTraditionalMedicineCostTotal }}</div>
                <div v-if="list.laboratoryCostTotal">化验费 {{ list.laboratoryCostTotal }}</div>
                <div v-if="list.operationCostTotal">手术费 {{ list.operationCostTotal }}</div>
                <div v-if="list.otherCostTotal">其他费 {{ list.otherCostTotal }}</div>
                <div v-if="list.materialCostTotal">材料费 {{ list.materialCostTotal }}</div>
            </div>
            <div :style="{position: 'absolute',top:(190 + (index * 660))  + 'px',left:300+ 'px'}">
                <div v-for="item in list.order_info.slice(index * 10,(index * 10) + pageArr[index])">
                    <span style="display:inline-block;width:200px;">
                        <span v-if="item.advice.id == 0">
                            <span v-if="item.project.type == 2"> {{ item.project.project.project_name }}</span>
                            <span v-if="item.project.type == 3">{{ item.project.good_info.good_name }}</span>
                        </span>
                        <span v-else>{{ item.advice.advice_name }}</span>
                    </span>
                    <span style="display:inline-block;width:50px;">
                        <span v-if="item.advice.id == 0">
                            <span v-if="item.project.type == 2">{{ item.project.count }}{{ item.project.unit }}</span>
                            <span v-if="item.project.type == 3">{{ item.project.count }}{{ item.project.unit }}</span>
                        </span>
                        <span v-else>{{ item.advice.prescribing_number }}{{ item.advice.prescribing_number_unit }}</span>
                    </span>
                    <span>
                        <span v-if="item.advice.id == 0">
                            <span v-if="item.project.type == 2">{{ (item.project.count * item.pric).toFixed(2) }}</span>
                            <span v-if="item.project.type == 3">{{ (item.project.count * item.pric).toFixed(2) }}</span>
                        </span>
                        <span v-else>{{ (item.advice.prescribing_number * item.pric).toFixed(2) }}</span>
                    </span>
                </div>
            </div>
            <div :style="{position: 'absolute',top:(390 + (index * 660))  + 'px',left:130+ 'px'}">{{ zhongwen }}</div>
            <div :style="{position: 'absolute',top:(390 + (index * 660))  + 'px',left:480+ 'px'}">{{ list.medfee_sumamt }}</div>
            <div v-if="index == pageArr.length - 1" :style="{position: 'absolute',top:(420 + (index * 660))  + 'px',left:110+ 'px',width:100 + '%',fontSize:12+'px'}">
                <div style="display:flex;">
                  <div style="width:140px">医疗总费用：{{ list.order.medfee_sumamt }}</div>
                  <div style="width:140px">基金支付总额：{{ list.order.fund_pay_sumamt }}</div>
                  <div style="width:140px">统筹支出：{{ list.order.hifp_pay }}</div>
                  <div style="width:140px">大病支出：{{list.order.hifmi_pay}}</div>

                </div>
                <div style="display:flex;">
                  <div style="width:140px">个人账户支付：{{ list.order.acct_pay }}</div>
                  <div style="width:140px">现金支出：{{ list.order.psn_cash_pay }}</div>
                  <div style="width:140px">本次账户：0</div>
                  <div style="width:140px">上次账户：0</div>
                </div>
              <div style="display:flex;">
                <div style="width:140px">账户余额：{{ list.order.balc }}</div>
                <div style="width:140px">其他支出：{{ list.order.oth_pay }}</div>
                <div style="width:140px">大病补充保险支出：0</div>
                <div style="width:140px">民政支出：0</div>
              </div>
              <div style="display:flex;">
                <div style="width:220px">医疗救助基金支出：{{ list.order.maf_pay }}</div>
                <div style="width:280px">企业补充医疗保险基金支出：{{ list.order.hifes_pay }}</div>
              </div>

            </div>
            <div>
                <div :style="{position: 'absolute',top:(480 + (index * 660))  + 'px',left:140+ 'px'}">盐城大丰悦达金骆驼血液透析中心</div>
                <div :style="{position: 'absolute',top:(480 + (index * 660))  + 'px',left:410+ 'px'}">{{ paramsObj.chargeName }}</div>
                <div :style="{position: 'absolute',top:(480 + (index * 660))  + 'px',left:550+ 'px'}">
                    <span>{{  paramsObj.setl_time ? paramsObj.setl_time.split(' ')[0].slice(0,4) : getTime(list.date, '{y}-{m}-{d}').slice(0,4) }}</span>
                </div>
                <div :style="{position: 'absolute',top:(480 + (index * 660))  + 'px',left:610+ 'px'}">
                    <span>{{ paramsObj.setl_time ? parseInt(paramsObj.setl_time.split(' ')[0].slice(5,7)) : getTime(list.date, '{y}-{m}-{d}').slice(5,7) }}</span>
                </div>
                <div :style="{position: 'absolute',top:(480 + (index * 660))  + 'px',left:650+ 'px'}">
                    <span>{{ paramsObj.setl_time ? parseInt(paramsObj.setl_time.split(' ')[0].slice(8,11)) : getTime(list.date, '{y}-{m}-{d}').slice(8,11) }}</span>
                </div>
            </div>
        </div>
    </div>
</template>


<script>
import { getInvoice } from '@/api/project/project'
import { uParseTime } from '@/utils/tools'
export default {
    props:{
        paramsObj:Object
    },
    data(){
        return{
            list:{},
            printDate:'',
            zhongwen:'',
            totalPrice:0.0,
            org_id:'',
            org_name:'',
            page:1,
            pageArr:[],
        }
    },
    created(){
        console.log('paramsObj',this.paramsObj)
        let params = {
            order_id: this.paramsObj.order_id,
            patient_id: this.paramsObj.patient_id,
        }
        this.getInvoice(params)
        // var data = new Date();
        // var month =data.getMonth() < 9 ? "0" + (data.getMonth() + 1) : data.getMonth() + 1;
        // var date = data.getDate() <= 9 ? "0" + data.getDate() : data.getDate();
        // this.printDate = data.getFullYear() + "-" + month + "-" + date;
        // this.smalltoBIG(982732.21)
        this.org_id = this.$store.getters.xt_user.org_id
        this.org_name = this.$store.getters.xt_user.org.org_name
    },
    methods:{
        getInvoice(params){
            getInvoice(params).then((res) => {
                // console.log('res',res)
                this.list = res.data.data
                // console.log(this.list,'表单内容打印记得注释')
                this.totalPrice = this.list.westernMedicineCostTotal + this.list.checkCostTotal + this.list.treatCostTotal + this.list.bedCostTotal + this.list.chineseTraditionalMedicineCostTotal +
                                    this.list.laboratoryCostTotal + this.list.operationCostTotal + this.list.otherCostTotal + this.list.materialCostTotal
                this.smalltoBIG(this.list.medfee_sumamt)
                var data = new Date(res.data.data.date * 1000);
                var month =data.getMonth() < 9 ? "0" + (data.getMonth() + 1) : data.getMonth() + 1;
                var date = data.getDate() <= 9 ? "0" + data.getDate() : data.getDate();
                this.printDate = data.getFullYear() + "-" + month + "-" + date;
                this.pageArr = []
                this.getPage()
                this.pageArr.push(10)
                console.log('pageArr',this.pageArr.length)
            })
        },
        smalltoBIG(n) {
            // let fraction = ['角', '分'];
            // let digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
            // let unit = [['元.', '万,', '亿,'], ["元",'拾', '佰', '仟']];
            // let head = price < 0 ? '欠' : '';
            // price = Math.abs(price);
            // let upper = '';
            // for (let i = 0; i < fraction.length; i++) {
            // upper += (digit[Math.floor(price * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
            // }
            // upper = upper || '整';
            // price = Math.floor(price);
            // for (let i = 0; i < unit[0].length && price > 0; i++) {
            // let p = '';
            // for (let j = 0; j < unit[1].length && price > 0; j++) {
            //     p = digit[price % 10] + unit[1][j] + p;
            //     price = Math.floor(price / 10);
            // }
            // upper = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + upper;
            // }
            // this.zhongwen = head + upper.replace(/(零.)*零圆/, '圆').replace(/(零.)+/g, '零').replace(/^整$/, '零圆整')
            // console.log('6666',head + upper.replace(/(零.)*零圆/, '圆').replace(/(零.)+/g, '零').replace(/^整$/, '零圆整'))
            if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)){
                return "数据非法";  //判断数据是否大于0
            }

            var unit = "仟佰拾亿仟佰拾万仟佰拾元角分", str = "";
            n += "00";

            var indexpoint = n.indexOf('.');  // 如果是小数，截取小数点前面的位数

            if (indexpoint >= 0){

                n = n.substring(0, indexpoint) + n.substr(indexpoint+1, 2);   // 若为小数，截取需要使用的unit单位
            }

            unit = unit.substr(unit.length - n.length);  // 若为整数，截取需要使用的unit单位
            for (var i=0; i < n.length; i++){
                str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i);  //遍历转化为大写的数字
            }
            console.log("str",str)
            if(str == '零元零角零分'){
                this.zhongwen = '零'
            }else{
                this.zhongwen =  str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
            }

            console.log(this.zhongwen)
        },
        getTime(value, temp) {
            if (value == 0) {
                return ''
            }
            if (value != undefined) {
                return uParseTime(value, temp)
            }
            return ''
        },
        getPage(){
        if(this.list.order_info.length <= 10){
            this.page = 1
            this.pageArr.push(this.list.order_info.length)
        }else if(this.list.order_info.length > 10){
            this.page = parseInt(this.list.order_info.length / 10)
            let num = this.list.order_info.length % 10
            for (var i=0;i<this.page;i++){
                this.pageArr.push(10)
            }
            if(num != 0){
                this.pageArr.push(num)
            }
        }
      }

    },
    watch:{
        paramsObj:{//深度监听，可监听到对象、数组的变化
            handler(val, oldVal){
                let params = {
                    order_id: val.order_id,
                    patient_id: val.patient_id,
                }
                this.getInvoice(params)

            },
            deep:true
        }
    }
}
</script>
