package com.example.administrator.zhapp.fragment.ajhxcgl;

import android.os.Bundle;
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.adapter.SpinnerAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.JsInfo;
import com.example.administrator.zhapp.entity.Qxgdzy;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.utils.UiUtlis;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 安健环巡查管理-缺陷工单
 */
public class QxgdFragment extends BaseFragment {


    EditText et_gd;
    EditText et_ms;
    Spinner et_zy;
    EditText et_bgr;
    EditText et_date;
    Button btn_save;
    Button btn_cancel;




    private boolean isEdit = false;

    private List<Qxgdzy.Qxgd> qxgds;
    private SpinnerAdapter adapter;

    private List<Zy> zys;

    private boolean first = true;

    public QxgdFragment() {
    }

    public static QxgdFragment newInstance(boolean b) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        QxgdFragment fragment = new QxgdFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            setData();
        }
    }

    private void setData() {
        et_date.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            isEdit = bundle.getBoolean("edit");
        }
    }

    /**
     * 设置是否可以编辑
     *
     * @param isEdit
     */
    private void setEnabled(boolean isEdit) {
        et_ms.setEnabled(isEdit);
        et_date.setEnabled(isEdit);
        et_bgr.setEnabled(isEdit);
        et_zy.setEnabled(isEdit);
        et_gd.setEnabled(isEdit);
        btn_cancel.setEnabled(isEdit);
        btn_save.setEnabled(isEdit);
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_qxgd;
    }

    @Override
    protected void initView() {
        et_gd = view.findViewById(R.id.et_gd);
        et_ms = view.findViewById(R.id.et_ms);
        et_zy = view.findViewById(R.id.et_zy);
        et_bgr = view.findViewById(R.id.et_bgr);
        et_date = view.findViewById(R.id.et_date);
        btn_save = view.findViewById(R.id.btn_save);
        btn_cancel = view.findViewById(R.id.btn_cancel);

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearView();
            }
        });
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(et_bgr.getText().toString())
                        && !TextUtils.isEmpty(et_date.getText().toString())
                        && !TextUtils.isEmpty(et_ms.getText().toString())) {
                    if (savaData()) {
                        showToast("保存成功");
                        clearView();
                    } else {
                    }
                } else {
                    showToast("你还有信息未填写");
                }
            }
        });


        setEnabled(isEdit);
    }

    public void initData() {
        qxgds = new ArrayList<>();
        zys = new ArrayList<>();
        if (isEdit) {
            //requestData();
            zys = DataSupport.findAll(Zy.class);
            initSpinner();
            et_bgr.setText((String) SPUtils.get(context, Contans.USERNAME, ""));
        }
    }

    //给spinner填充数据
    private void initSpinner() {
        adapter = new SpinnerAdapter(zys, context);
        et_zy.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) {
            et_zy.setSelection(item);
        }
//        et_zy.getSelectedItem();
    }

    private void clearView() {

        et_gd.setText("");
        et_zy.setSelection(0);
        //et_bgr.setText("");
        et_date.setText("");
        et_ms.setText("");
        setData();
    }

    public boolean savaData() {
        QxgdInfo info = new QxgdInfo();
        info.setQxms(UiUtlis.getText(et_ms));
        info.setZyid(zys.get(et_zy.getSelectedItemPosition()).getVALUE());
        info.setBgr(UiUtlis.getText(et_bgr));
        info.setDate(UiUtlis.getText(et_date));
        return info.save();
    }
}
