package com.example.administrator.zhapp.activity.sbxdjgl;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.example.administrator.zhapp.DBModel.QxgdInfo;
import com.example.administrator.zhapp.DBModel.Zy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.adapter.SpinnerAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.StatusInfo;
import com.example.administrator.zhapp.utils.GsonUtils;
import com.example.administrator.zhapp.utils.HttpListener;
import com.example.administrator.zhapp.utils.NohttpUtils;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.utils.UiUtlis;
import com.orhanobut.logger.Logger;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import org.litepal.crud.DataSupport;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SQxgdlrfActivity extends BaseActivity {

    EditText etGd;
    EditText etMs;
    Spinner etZy;
    EditText etBgr;
    EditText etDate;
    Button btnSave;
    Button btnSc;

    private SpinnerAdapter adapter;
    private List<Zy> zys;

    private void findView() {
        etGd = findViewById(R.id.et_gd);
        etMs = findViewById(R.id.et_ms);
        etZy = findViewById(R.id.et_zy);
        etBgr = findViewById(R.id.et_bgr);
        etDate = findViewById(R.id.et_date);
        btnSave = findViewById(R.id.btn_save);
        btnSc = findViewById(R.id.btn_sc);
        btnSc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scData();
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(etBgr.getText().toString())
                        && !TextUtils.isEmpty(etDate.getText().toString())
                        && !TextUtils.isEmpty(etMs.getText().toString())) {
                    if (savaData()) {
                        showToast("保存成功");
                        clearView();
                    } else {
                    }
                } else {
                    showToast("你还有信息未填写");
                }
            }
        });
    }

    @Override
    public int getContentViewID() {
        return R.layout.activity_sqxgdlr3;
    }

    @Override
    protected void initView() {
        findView();
        setBack();
        setTitle("缺陷工单录入");
    }

    @Override
    public void initData() {
        setView();
        zys = DataSupport.findAll(Zy.class);
        initSpinner();
    }

    //给spinner填充数据
    private void initSpinner() {
        adapter = new SpinnerAdapter(zys, context);
        etZy.setAdapter(adapter);
        int item = -1;
        String bzmc = (String) SPUtils.get(context, Contans.BZMC, "");
        for (int i = 0; i < zys.size(); i++) {
            if (zys.get(i).getXXMS().equals(bzmc)) {
                item = i;
                break;
            }
        }
        if (item != -1) {
            etZy.setSelection(item);
        }
    }

    private void scData() {
        //缺陷工单，如果缺陷工单为空，就没有上传
        List<QxgdInfo> infos = DataSupport.findAll(QxgdInfo.class);
        if (infos.size() != 0) {
            Request<String> request1 = NoHttp.createStringRequest(Contans.IP + Contans.DJJHQXGD, RequestMethod.POST);
            String qxgdjson = "{\"Rows\":" + GsonUtils.GsonString(infos) + ",\"Total\": " + infos.size() + "}";
            Logger.json(qxgdjson);
            System.out.println(qxgdjson);
            qxgdjson = new String(qxgdjson.getBytes(), StandardCharsets.UTF_8);
            request1.setDefineRequestBodyForJson(UiUtlis.encoder(qxgdjson));
            NohttpUtils.getInstance().add(this, 1, request1, callback, true, true, "上传中...");
        }
    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {
            try {
                StatusInfo info = GsonUtils.GsonToBean(response.get(), StatusInfo.class);
                if (info != null) {
                    if (info.getTotal() != 0) {
                        if (info.getRows().get(0).getStatus().equals("1")) {
                            showToast("上传成功");
                            DataSupport.deleteAll(QxgdInfo.class);
                        } else {
                            showToast("上传失败");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {

        }
    };

    public boolean savaData() {
        QxgdInfo info = new QxgdInfo();
        info.setQxms(UiUtlis.getText(etMs));
        info.setZyid(zys.get(etZy.getSelectedItemPosition()).getVALUE());
        info.setBgr(UiUtlis.getText(etBgr));
        info.setDate(UiUtlis.getText(etDate));
        return info.save();
    }

    private void setView() {
        etDate.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        etBgr.setText((String) SPUtils.get(context, Contans.USERNAME, ""));
    }

    private void clearView() {
        etDate.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        etBgr.setText((String) SPUtils.get(context, Contans.USERNAME, ""));
    }
}
