@extends('layouts.app')
@section('title')采购管理-财务-对账单@endsection

@section('content')
    @component('procurement.finance.menu')@endcomponent
    <div class="container-fluid" id="list">
        <div id="form_div" class="mt-1"></div>
        <div class="row mt-2">
        <span class="dropdown ml-3">
        <button class="btn btn-outline-dark btn-sm form-control-sm dropdown-toggle tooltipTarget"
                data-toggle="dropdown" title="导出所有页将会以搜索条件得到的筛选结果，将其全部记录（每一页）导出">
            导出Excel
        </button>
        <div class="dropdown-menu">
            <a class="dropdown-item" @click="checkBillExport(false)" href="javascript:">导出勾选内容</a>
            <a class="dropdown-item" @click="checkBillExport(true)" href="javascript:">导出所有页</a>
        </div>
        </span>
        </div>
        <table class="table table-sm table-striped table-bordered table-hover text-nowrap card-body mt-2 td-min-width-80" id="table">
            <tr v-for="(procurementCheckSheet,i) in procurementCheckSheets">
                <td>
                    <input class="checkItem" type="checkbox" :value="procurementCheckSheet.id">
                </td>
                <td><span>@{{ procurementCheckSheet.procurement_code }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.procurement_at }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.procurement_delivery_at }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.supplier_name }}</span></td>
                <td class="tooltipTarget" style="max-width: 200px;overflow:hidden"><span>@{{ procurementCheckSheet.material_code }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.material_name }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.procurement_quantity }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.amount }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.signer_name }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.signed_at }}</span></td>
                <td class="text-muted"><span>@{{ procurementCheckSheet.account_payable }}</span></td>
                <td><span>@{{ procurementCheckSheet.invoice_number }}</span></td>
                <td><span>@{{ procurement_check_sheet_status[procurementCheckSheet.status] }}</span></td>
                <td class="text-center">
                    <a class="text-primary" @click="fillInvoiceNumber(procurementCheckSheet.id)" v-if="!procurementCheckSheet.invoice_number">填写发票号</a>
                    <span v-else>-</span>
                </td>
            </tr>
        </table>
        @can('采购管理-财务-对账单')
            @include('procurement.finance._fillInvoiceNumber')
        @endcan
        <div class="text-info h5 btn btn">{{$procurementCheckSheets->count()}}/@{{ sum }}</div>
        <div>{{$procurementCheckSheets->appends($paginateParams)->links()}}</div>
    </div>
@endsection
@section('lastScript')
    <script type="text/javascript" src="{{mix('js/queryForm/export.js')}}"></script>
    <script type="text/javascript" src="{{mix('js/queryForm/queryForm.js')}}"></script>
    <script type="text/javascript" src="{{mix('js/queryForm/header.js')}}"></script>{{--新版2--}}
    <script>
        let vue = new Vue({
            el: '#list',
            data: {
                procurementCheckSheets: [
                    @foreach($procurementCheckSheets as $procurementCheckSheet)
                    {
                        id:'{{$procurementCheckSheet->id ??''}}',invoice_number:'{{$procurementCheckSheet->invoice_number ??''}}',account_payable:'{{$procurementCheckSheet->account_payable ??''}}',
                        created_at:'{{$procurementCheckSheet->created_at ??''}}',auditor:'{{$procurementCheckSheet->auditor ??''}}',status:'{{$procurementCheckSheet->status ??''}}',

                        @if($procurementCheckSheet->procurementDelivery)
                        amount:'{{$procurementCheckSheet->procurementDelivery->amount ??''}}',initiator:'{{$procurementCheckSheet->procurementDelivery->initiator ??''}}',
                        signer:'{{$procurementCheckSheet->procurementDelivery->signer ??''}}',signed_at:'{{$procurementCheckSheet->procurementDelivery->signed_at ??''}}',
                        procurement_delivery_at:'{{$procurementCheckSheet->procurementDelivery->created_at ?? ''}}',@endif

                        @if($procurementCheckSheet->procurementDelivery->procurement)
                        procurement_quantity:'{{$procurementCheckSheet->procurementDelivery->procurement->quantity ??''}}',
                        procurement_code:'{{$procurementCheckSheet->procurementDelivery->procurement->code ??''}}',
                        procurement_at:'{{$procurementCheckSheet->procurementDelivery->procurement->created_at??''}}',@endif

                        @if($procurementCheckSheet->procurementDelivery->receiver)
                        signer_name:'{{$procurementCheckSheet->procurementDelivery->receiver->name ??''}}',@endif

                        @if($procurementCheckSheet->procurementDelivery->procurement->supplier??'')
                        supplier_id:'{{$procurementCheckSheet->procurementDelivery->procurement->supplier->id ??''}}',
                        supplier_name:'{{$procurementCheckSheet->procurementDelivery->procurement->supplier->name ??''}}',@endif

                        @if($procurementCheckSheet->procurementDelivery->procurement->ownerMaterial->material??'')
                        material_id:'{{$procurementCheckSheet->procurementDelivery->procurement->ownerMaterial->material->id ??''}}',
                        material_name:'{{$procurementCheckSheet->procurementDelivery->procurement->ownerMaterial->material->name ??''}}',
                        material_code:'{{$procurementCheckSheet->procurementDelivery->procurement->ownerMaterial->material_code ??''}}',@endif

                    },
                    @endforeach
                ],
                suppliers:[
                        @foreach($suppliers as $supplier)
                    {name:'{{$supplier->id}}',value:'{{$supplier->name}}'},
                    @endforeach
                ],
                materials: [
                        @foreach($materials as $material)
                    {name:'{{$material->id}}',value:'{{$material->name}}'},
                    @endforeach
                ],
                sum:{!! $procurementCheckSheets->total() !!},
                procurement_check_sheet_status:{!! json_encode(\App\ProcurementCheckSheet::status,JSON_UNESCAPED_UNICODE) !!},
                invoice_number:'',procurementCheckSheetId:null,
            },
            mounted: function () {
                $(".tooltipTarget").tooltip({'trigger': 'hover'});
                $('#list').removeClass('d-none');
                let data = [
                    [
                        {name: 'signed_at', type: 'dateMonth', tip: '选择显示指定的签收日期'},
                        {
                            name: 'supplier_id', type: 'select_multiple_select', tip: ['输入关键词快速定位下拉列表,回车确定', '选择要显示的供应商'],
                            placeholder: ['供应商', '定位或多选供应商'], data: this.suppliers
                        },
                        {
                            name: 'status', type: 'select',
                            tip: '输入关键词快速定位下拉列表,回车确定',
                            placeholder: '对账单状态',
                            data: [{name:0,value:'未出账'},{name:1,value:'待出账'},{name:2,value:'已出账'},{name:3,value:'已完结'},],
                        },
                    ],
                ];
                this.form = new query({
                    el: "#form_div",
                    condition: data,
                });
                this.form.init();
                let column = [
                    {name: 'code', value: '采购编号', neglect: true,class: 'text-center'},
                    {name: 'procurement_at', value: '采购日期',class: 'text-center'},
                    {name: '送货日期', value: '送货日期',class: 'text-center'},
                    {name: 'company_name', value: '供应商名称',class: 'text-center'},
                    {name: 'material_code', value: '耗材编号',class: 'text-center'},
                    {name: 'material_name', value: '耗材',class: 'text-center'},
                    {name: 'quantity', value: '采购数量',class: 'text-center'},
                    {name: 'amount', value: '送货数量',class: 'text-center'},
                    {name: 'signer', value: '签收人',class: 'text-center'},
                    {name: 'signed_at', value: '签收日期',class: 'text-center'},
                    {name: '应付金额', value: '应付金额', neglect: true,class: 'text-center'},
                    {name: '发票号', value: '发票号', neglect: true,class: 'text-center'},
                    {name: '状态', value: '状态', neglect: true,class: 'text-center'},
                    {name: '填写发票号', value: '填写发票号', neglect: true,class: 'text-center'},
                ];

                new Header({
                    el: "table",
                    name: "checkBill",
                    column: column,
                    data: this.procurementCheckSheets,
                    fixedTop: ($('#form_div').height()) + 2,
                }).init();
            },
            methods: {
                fillInvoiceNumber(id){
                    let _this=this;
                    _this.procurementCheckSheetId=id;
                    $('#fill-invoice-number').modal('show');
                },
                fillInvoice(){
                    let _this=this;
                    if (!_this.procurementCheckSheetId)return;
                    if (!_this.invoice_number){
                        tempTip.setDuration(3000);
                        tempTip.show('发票号不可为空!');
                        return;
                    }
                    let url = '{{url('procurement/finance/fillInvoice')}}';
                    let params = {invoice_number:_this.invoice_number,procurementCheckSheetId:_this.procurementCheckSheetId};
                    window.axios.post(url,params).then(function (res) {
                        if (!res.data.success){
                            tempTip.setDuration(3000);
                            tempTip.show(res.data.message);
                        }else {
                            _this.procurementCheckSheets.forEach(function (procurementCheckSheet) {
                                if (procurementCheckSheet.id===_this.procurementCheckSheetId){
                                    procurementCheckSheet.invoice_number=res.data.data;
                                    procurementCheckSheet.status=res.data.status;
                                }
                            });
                            $("#fill-invoice-number").modal('hide');
                            tempTip.setDuration(3000);
                            tempTip.showSuccess('添加发票号成功！');
                        }
                    }).catch(function (err) {
                        tempTip.setDuration(3000);
                        tempTip.show('添加发票号失败！网络错误：'+err);
                    });
                },
                checkBillExport(selectAll){
                    let url = '{{url('procurement/finance/checkBillExport')}}';
                    let token='{{ csrf_token() }}';
                    excelExport(selectAll,checkData,url,this.sum,token);
                },

            }
        });
    </script>
@endsection
