package com.police.policedatasystem.mine.activity;

import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.github.barteksc.pdfviewer.PDFView;
import com.police.policedatasystem.databinding.ActivityApplyDetailBinding;
import com.police.policedatasystem.mine.model.ApplyDetail;
import com.police.policedatasystem.mine.viewmodel.DetailViewModel;
import com.police.policedatasystem.util.UiUtils;

public class ApplyDetailActivity extends AppCompatActivity {

    private ActivityApplyDetailBinding binding;
    private DetailViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityApplyDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivBack.setOnClickListener(view -> finish());
        binding.rlLoading.setOnClickListener(view -> {

        });
        viewModel = new DetailViewModel(this);
//        if (getIntent().getStringExtra("applyDetail") == null) {
////            viewModel.jumpApplyDetail(getIntent().getStringExtra("jqbh"));
//        } else {
            viewModel.getApplyDetail(getIntent().getStringExtra("applyDetail"));
//        }

    }

    public void applyDetail(ApplyDetail detail) {
        binding.tvDetailFnbh.setText(detail.getBh());
        binding.tvDetailSqsy.setText(detail.getSy());
        binding.tvDetailSqsj.setText(detail.getCreateTimeStr());
        binding.tvDetailJwlx.setText(detail.getLy());
        binding.tvDetailShzt.setText(detail.getSpzt());
//        binding.tvDetailFnxq.setText(detail.getFj());
        // 加载PDF文件
//        File file = new File("/path/to/your/pdf/file.pdf"); // 替换为你的PDF文件路径
        if (detail.getFj() != null && !detail.getFj().isEmpty()) {
            for (String item : detail.getFj()) {
                PDFView pdfView = new PDFView(this, null);
                pdfView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UiUtils.dp2Px(500)));
                pdfView.fromBytes(Base64.decode(item, Base64.DEFAULT))
                        .enableSwipe(true) // 允许滑动翻页
                        .enableDoubletap(true) // 允许双击缩放
                        .enableAnnotationRendering(true) // 允许渲染注释
                        .load();
                binding.llGroup.addView(pdfView);
            }
        }
    }

    public void loadingShow() {
        binding.rlLoading.setVisibility(View.VISIBLE);
    }

    public void loadingNone() {
        binding.rlLoading.setVisibility(View.GONE);
    }
}