package com.mq.zhicang;

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.zhicang.db.ZhiCangEntity;

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

/**
 * 创建人 mQ
 * 创建时间 2019/8/15 0015 18:54
 * 文件名称 DetailActivity
 * 说明:   值仓详情页
 **/
public class DetailActivity extends BaseNfcActivity {

    @BindView(R.id.tv_code)
    TextView mTvCode;
    @BindView(R.id.tv_customer)
    TextView mTvCustomer;
    @BindView(R.id.tv_car)
    TextView mTvCar;
    @BindView(R.id.tv_grain_type)
    TextView mTvGrainType;
    @BindView(R.id.tv_weight)
    TextView mTvWeight;
    @BindView(R.id.tv_house)
    TextView mTvHouse;
    @BindView(R.id.tv_position)
    TextView mTvPosition;
    @BindView(R.id.tv_quality)
    TextView mTvQuality;
    @BindView(R.id.tv_real_house)
    TextView mTvRealHouse;
    @BindView(R.id.tv_real_position)
    TextView mTvRealPosition;
    @BindView(R.id.tv_pack_type)
    TextView mTvPackType;
    @BindView(R.id.et_finish_count)
    TextView mEtFinishCount;
    @BindView(R.id.et_deduct_water)
    TextView mEtDeductWater;
    @BindView(R.id.et_deduct_sundries)
    TextView mEtDeductSundries;
    @BindView(R.id.et_deduct_other)
    TextView mEtDeductOther;
    @BindView(R.id.et_deduct_pack)
    TextView mEtDeductPack;
    @BindView(R.id.et_deduct_percent)
    TextView mEtDeductPercent;
    @BindView(R.id.tv_work_team)
    TextView mTvWorkTeam;
    @BindView(R.id.tv_work_group)
    TextView mTvWorkGroup;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R.id.et_keeper)
    TextView mEtKeeper;
    @BindView(R.id.et_remark)
    TextView mEtRemark;

    private ZhiCangEntity mEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ButterKnife.bind(this);
        //取出读卡数据
        mEntity = (ZhiCangEntity) getIntent().getSerializableExtra("bean");

        //设置控件
        ETTool.setData(mTvCode, mEntity.work_number);
        ETTool.setData(mTvCustomer, mEntity.customer);
        ETTool.setData(mTvCar, mEntity.car);
        ETTool.setData(mTvGrainType, mEntity.grain);
        ETTool.setData(mTvWeight, mEntity.weight);
        ETTool.setData(mTvHouse, mEntity.plan_house);
        ETTool.setData(mTvPosition, mEntity.plan_position);
        ETTool.setData(mTvQuality, mEntity.quality);
        ETTool.setData(mTvRealHouse, mEntity.real_house);
        ETTool.setData(mTvRealPosition, mEntity.real_position);
        ETTool.setData(mEtDeductWater, mEntity.deduct_water);
        ETTool.setData(mEtDeductSundries, mEntity.deduct_sundries);
        ETTool.setData(mEtDeductOther, mEntity.deduct_other);
        ETTool.setData(mEtDeductPack, mEntity.deduct_pack);
        ETTool.setData(mEtDeductPercent, mEntity.deduct_percent);
        ETTool.setData(mTvStartTime, mEntity.start_time);
        ETTool.setData(mTvEndTime, mEntity.end_time);
        ETTool.setData(mEtRemark, mEntity.remark);
        ETTool.setData(mEtKeeper, mEntity.keeper);
        ETTool.setData(mTvPackType, mEntity.pack_type);
        ETTool.setData(mEtFinishCount, mEntity.finish_count);
        ETTool.setData(mTvWorkTeam, mEntity.work_team);
        ETTool.setData(mTvWorkGroup, mEntity.work_group);
    }

    @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;
        }
    }
}
