<template>
    <div id='invoice-print'>
        <div style="display:flex;">
            <div style="position: absolute;top:50px;left:70px;">{{ list.order_number }}</div>
            <!-- <div>{{ list.id_card_no }}</div> -->
        </div>
        <div style="display:flex;justify-content: space-between;">
            <!-- <div>{{ list.department_name }}</div> -->
            <div  style="position: absolute;top:50px;left:300px">{{ list.id_card_no }}</div>
            <div></div>
            <div  style="position: absolute;left:560px;top:50px;">
                <span>{{ printDate.slice(0,4) }}</span>
                <span>{{ printDate.slice(5,7) }}</span>
                <span>{{ printDate.slice(8,11) }}</span>
            </div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <!-- <div>{{ list.department_name }}</div> -->
            <div  style="position: absolute;top:70px;left:70px">门诊</div>
            <div></div>
            <div  style="position: absolute;top:70px;left:300px">{{ list.number }}</div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <div style="position: absolute;top:90px;left:120px;"> {{ paramsObj.name }}</div>
            <div style="position: absolute;top:90px;left:240px;">√</div>
            <!-- <div>{{ list.pay_way }}</div> -->
            <div style="position: absolute;top:70px;left:460px;">{{ paramsObj.setl_time.split(' ')[0] }}</div>
            <div style="position: absolute;top:70px;left:560px;">{{ paramsObj.setl_time.split(' ')[0] }}</div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <!-- <div>{{ paramsObj.gend == 1 ? '男' : '女' }}</div> -->
            <div style="position: absolute;top:110px;left:70px;">√</div>
            <div style="position: absolute;top:110px;left:260px;">{{ totalPrice - list.psn_cash_money }}</div>
            <div style="position: absolute;top:110px;left:440px;">{{ list.psn_cash_money }}</div>
            <div style="position: absolute;top:110px;left:580px;">现金</div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <div style="position: absolute;top:160px;left:40px">西药</div>
            <div style="position: absolute;top:160px;left:110px">{{ list.westernMedicineCostTotal }}</div>
            <div style="position: absolute;top:160px;left:180px">检查费</div>
            <div style="position: absolute;top:160px;left:260px">{{ list.checkCostTotal }}</div>
            <div style="position: absolute;top:160px;left:340px">治疗费</div>
            <div style="position: absolute;top:160px;left:420px">{{ list.treatCostTotal }}</div>
            <div style="position: absolute;top:160px;left:500px">床位费</div>
            <div style="position: absolute;top:160px;left:580px">{{ list.bedCostTotal }}</div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <div style="position: absolute;top:190px;left:40px">中成药</div>
            <div style="position: absolute;top:190px;left:110px">{{ list.chineseTraditionalMedicineCostTotal }}</div>
            <div style="position: absolute;top:190px;left:180px">化验费</div>
            <div style="position: absolute;top:190px;left:260px">{{ list.laboratoryCostTotal }}</div>
            <div style="position: absolute;top:190px;left:340px">手术费</div>
            <div style="position: absolute;top:190px;left:420px">{{ list.operationCostTotal }}</div>
            <div style="position: absolute;top:190px;left:500px">其他费</div>
            <div style="position: absolute;top:190px;left:580px">{{ list.otherCostTotal }}</div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <div style="position: absolute;top:210px;left:40px"></div>
            <div style="position: absolute;top:210px;left:110px"></div>
            <div style="position: absolute;top:210px;left:180px"></div>
            <div style="position: absolute;top:210px;left:260px"></div>
            <div style="position: absolute;top:210px;left:340px">材料费</div>
            <div style="position: absolute;top:210px;left:420px">{{ list.materialCostTotal }}</div>
            <div style="position: absolute;top:210px;left:500px"></div>
            <div style="position: absolute;top:210px;left:580px"></div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <div style="position: absolute;top:230px;left:40px"></div>
            <div style="position: absolute;top:230px;left:110px"></div>
            <div style="position: absolute;top:230px;left:180px">补收</div>
            <div style="position: absolute;top:230px;left:260px">{{ list.psn_cash_money }}</div>
            <div style="position: absolute;top:230px;left:340px"></div>
            <div style="position: absolute;top:230px;left:420px"></div>
            <div style="position: absolute;top:230px;left:500px"></div>
            <div style="position: absolute;top:230px;left:580px"></div>
        </div>
        <div>
            <span>
            <span  style="position: absolute;top:260px;left:180px">
                <span v-if="zhongwen.indexOf('万') > -1 && zhongwen.indexOf('拾') > -1 && zhongwen.indexOf('拾') == 1">
                    {{ zhongwen.substring(0,1) }}
                </span>
                <span v-else>零</span>
            </span>
            <span  style="position: absolute;top:260px;left:220px">
                <span v-if="zhongwen.indexOf('万') > -1">
                    {{ zhongwen.substring(zhongwen.indexOf('万') - 1,zhongwen.indexOf('万')) }}
                </span>
                <span v-else>零</span>
            </span>
            <span  style="position: absolute;top:260px;left:260px">
                <span v-if="zhongwen.indexOf('仟') > -1">
                    {{ zhongwen.substring(zhongwen.indexOf('仟') - 1,zhongwen.indexOf('仟')) }}
                </span>
                <span v-else>零</span>
            </span>
            <span  style="position: absolute;top:260px;left:300px">
                <span v-if="zhongwen.indexOf('佰') > -1">
                    {{ zhongwen.substring(zhongwen.indexOf('佰') - 1,zhongwen.indexOf('佰')) }}
                </span>
                <span v-else>零</span>
            </span>
            <span style="position: absolute;top:260px;left:340px">
                <span v-if="zhongwen.indexOf('拾') > -1">
                    {{ zhongwen.substring(zhongwen.indexOf('拾') - 1,zhongwen.indexOf('拾')) }}
                </span>
                <span v-else>零</span>
            </span>
            <span  style="position: absolute;top:260px;left:380px">
                <span v-if="zhongwen.indexOf('元') > -1 && zhongwen.substring(zhongwen.indexOf('元') - 1,zhongwen.indexOf('元')) != '拾'">
                    {{ zhongwen.substring(zhongwen.indexOf('元') - 1,zhongwen.indexOf('元')) }}
                </span>
                <span v-else>零</span>
            </span>
            <span  style="position: absolute;top:260px;left:420px">
                <span v-if="zhongwen.indexOf('角') > -1">
                    {{ zhongwen.substring(zhongwen.indexOf('角') - 1,zhongwen.indexOf('角')) }}
                </span>
                <span v-else>零</span>
            </span>
            <span  style="position: absolute;top:260px;left:460px">
                <span v-if="zhongwen.indexOf('分') > -1">
                    {{ zhongwen.substring(zhongwen.indexOf('分') - 1,zhongwen.indexOf('分')) }}
                </span>
                <span v-else>零</span>
            </span>
            </span>
            <span style="position: absolute;top:260px;left:540px">{{ totalPrice }}</span>
        </div>
        <!-- <div style="position: absolute;top:300px;left:460px">现金：{{ list.psn_cash_money }}</div> -->
        <div>
            <div style="position: absolute;top:340px;left:120px">{{ org_name }}</div>
            <div style="position: absolute;top:340px;left:420px">高慧裕</div>
        </div>
    </div>
</template>


<script>
import { getInvoice } from '@/api/project/project'
export default {
    props:{
        paramsObj:Object
    },
    data(){
        return{
            list:{},
            printDate:'',
            zhongwen:'',
            totalPrice:0.0,
            org_id:'',
            org_name:''
        }
    },
    created(){
        console.log('paramsObj',this.paramsObj)
        let params = {
            order_id: this.paramsObj.order_id,
            patient_id: this.paramsObj.patient_id,
            number: this.paramsObj.number,
        }
        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
                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.totalPrice)
                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;
            })
        },
        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);  //遍历转化为大写的数字
            }

            this.zhongwen =  str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
            console.log(this.zhongwen)
        },

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

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