package com.mq.zhongzhuanxiezai;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.mq.mylibrary.base.BaseNfcActivity;
import com.mq.mylibrary.db.grain_type.GrainTypeDao;
import com.mq.mylibrary.db.grain_type.GrainTypeEntity;
import com.mq.mylibrary.tool.ETTool;
import com.mq.mylibrary.tool.MusicTool;
import com.mq.mylibrary.tool.StringTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.zhongzhuanxiezai.db.XieZaiEntity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 创建人 mQ
 * 创建时间 2019/8/20 0020 19:16
 * 文件名称 DetailActivity
 * 说明:
 **/
public class DetailActivity extends BaseNfcActivity {


    @BindView(R.id.tv_check_number)
    TextView mTvCheckNumber;
    @BindView(R.id.tv_work_number)
    TextView mTvWorkNumber;
    @BindView(R.id.tv_customer)
    TextView mTvCustomer;
    @BindView(R.id.tv_grain)
    TextView mTvGrain;
    @BindView(R.id.tv_car)
    TextView mTvCar;
    @BindView(R.id.tv_weight)
    TextView mTvWeight;
    @BindView(R.id.tv_haulier)
    TextView mTvHaulier;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;
    @BindView(R.id.tv_platform)
    TextView mTvPlatform;
    @BindView(R.id.tv_position)
    TextView mTvPosition;
    @BindView(R.id.tv_finish_count)
    TextView mTvFinishCount;
    @BindView(R.id.tv_work_team)
    TextView mTvWorkTeam;
    @BindView(R.id.tv_work_group)
    TextView mTvWorkGroup;
    @BindView(R.id.tv_entd)
    TextView mTvEntd;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;

    private XieZaiEntity mEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ButterKnife.bind(this);

        //取出读卡数据
        mEntity = (XieZaiEntity) getIntent().getSerializableExtra("bean");

        ETTool.setData(mTvCheckNumber, mEntity.check_number);
        ETTool.setData(mTvWorkNumber, mEntity.order_number);
        ETTool.setData(mTvCustomer, mEntity.customer);
        GrainTypeEntity grain = GrainTypeDao.get().getGrain(App.getDBHelper(), mEntity.grain);
        if (grain != null)
            ETTool.setData(mTvGrain, grain.grain_name);
        ETTool.setData(mTvCar, mEntity.car);
        ETTool.setData(mTvWeight, mEntity.weight);
        ETTool.setData(mTvHaulier, mEntity.haulier);
        ETTool.setData(mTvPhone, mEntity.phone);
        ETTool.setData(mTvPlatform, mEntity.platform);
        ETTool.setData(mTvPosition, mEntity.position);
        ETTool.setData(mTvFinishCount, mEntity.finish_count);
        ETTool.setData(mTvWorkTeam, mEntity.work_team);
        ETTool.setData(mTvWorkGroup, mEntity.work_group);
        ETTool.setData(mTvEntd, mEntity.entd);
        ETTool.setData(mTvStartTime, mEntity.start_time);
        ETTool.setData(mTvEndTime, mEntity.end_time);
    }


    @SuppressLint("StaticFieldLeak")
    @OnClick({R.id.bar_back, R.id.btn_card})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                onBackPressed();
                break;
            case R.id.btn_card:
                //写卡
                showPd();
                new AsyncTask<Void, Void, String>() {

                    @Override
                    protected String doInBackground(Void... voids) {
                        String writeData = mEntity.getWriteData();
                        return writeTag(writeData);
                    }

                    @Override
                    protected void onPostExecute(String s) {
                        super.onPostExecute(s);
                        if (StringTool.isEmpty(s)) {
                            ToastTool.showLong("补卡成功");
                            new AlertDialog.Builder(getContext())
                                    .setMessage("补卡成功")
                                    .setPositiveButton("完成", null)
                                    .create().show();
                            MusicTool.load(getContext());
                        } else
                            ToastTool.showLong(s);
                        cancelPd();
                    }
                }.execute();
                break;
        }
    }

}
