package com.mq.zhuangchefaka;

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.tool.ETTool;
import com.mq.mylibrary.tool.MusicTool;
import com.mq.mylibrary.tool.StringTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.zhuangchefaka.db.ZhuangCheEntity;

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


public class DetailActivity extends BaseNfcActivity {

    @BindView(R.id.tv_inbound_code)
    TextView mTvInboundCode;
    @BindView(R.id.tv_train_code)
    TextView mTvTrainCode;
    @BindView(R.id.tv_customer)
    TextView mTvCustomer;
    @BindView(R.id.tv_gain)
    TextView mTvGain;
    @BindView(R.id.tv_platform)
    TextView mTvPlatform;
    @BindView(R.id.tv_position)
    TextView mTvPosition;
    @BindView(R.id.tv_car)
    TextView mTvCar;
    @BindView(R.id.et_load_count)
    TextView mEtLoadCount;
    @BindView(R.id.tv_work_team)
    TextView mTvWorkTeam;
    @BindView(R.id.tv_work_group)
    TextView mTvWorkGroup;
    @BindView(R.id.et_entd)
    TextView mEtEntd;
    @BindView(R.id.tv_load_time)
    TextView mTvLoadTime;


    private ZhuangCheEntity mEntity;

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

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

        ETTool.setData(mTvInboundCode, mEntity.inbound_code);
        ETTool.setData(mTvTrainCode, mEntity.train_code);
        ETTool.setData(mTvCustomer, mEntity.customer);
        ETTool.setData(mTvGain, mEntity.grain);
        ETTool.setData(mTvPlatform, mEntity.platform_name);
        ETTool.setData(mTvPosition, mEntity.position_name);
        ETTool.setData(mTvCar, mEntity.car);
        ETTool.setData(mEtLoadCount, mEntity.finish_count);
        ETTool.setData(mTvWorkTeam, mEntity.work_team);
        ETTool.setData(mTvWorkGroup, mEntity.work_group);
        ETTool.setData(mEtEntd, mEntity.entd);
        ETTool.setData(mTvLoadTime, mEntity.load_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;
        }
    }

}
