package com.rv.rvmine.traveler;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.base.utils.ui.datetime.selector.util.TextUtil;
import com.frame.rv.config.RvFrameConfig;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.base.BaseStatusActivity;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.rv.home.rv.module.ApiConfig;
import com.rv.home.rv.module.basic.presenter.CommonPresenter;
import com.rv.rvmine.R;
import com.rv.rvmine.R2;
import com.rv.rvmine.bean.TravelerListBean;
import com.yuyife.okgo.OkGoUtil;

import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加出游人
 */
public class AddAVisitorActivity extends BaseStatusActivity<CommonPresenter> {


    @BindView(R2.id.cb_adult)
    CheckBox cbAdult;
    @BindView(R2.id.cb_child)
    CheckBox cbChild;
    @BindView(R2.id.et_traveler_name)
    EditText etTravelerName;
    @BindView(R2.id.cb_man)
    CheckBox cbMan;
    @BindView(R2.id.cb_girl)
    CheckBox cbGirl;
    @BindView(R2.id.et_id_card)
    EditText etIdCard;
    @BindView(R2.id.et_phone)
    EditText etPhone;

    private int identityType = 0;
    private int sexType = 1;
    private int avisitorId;

    public static Intent getIntent(Context context, TravelerListBean.DataBean bean) {
        return new Intent(context, AddAVisitorActivity.class).putExtra("bean", bean);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_add_avisitor;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        TravelerListBean.DataBean dataBean = (TravelerListBean.DataBean) intent.getSerializableExtra("bean");
        if (dataBean !=null){
            titleView.setTitle("编辑出游人");
            avisitorId = dataBean.getId();
            identityType = dataBean.getIsChild();
            sexType = dataBean.getSex();
            if (0== dataBean.getIsChild()){
                cbAdult.setChecked(true);
                cbChild.setChecked(false);
            }else{
                cbAdult.setChecked(false);
                cbChild.setChecked(true);
            }

            if (0 == dataBean.getSex()){
                cbMan.setChecked(false);
                cbGirl.setChecked(true);
            }else {
                cbMan.setChecked(true);
                cbGirl.setChecked(false);
            }
            etTravelerName.setText(dataBean.getName());
            etIdCard.setText(dataBean.getIdCard());
            etPhone.setText(dataBean.getPhone());

        }else {
            titleView.setTitle(getString(R.string.add_a_visitor_title));
        }


        titleView.setText(R.id.tv_title_right, getString(R.string.rv_save));
        titleView.setChildClickListener(R.id.tv_title_right, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etTravelerName.getText().toString().trim();
                if (TextUtil.isEmpty(name)){
                    showToast("姓名不能为空！");
                    return;
                }

                String idCard = etIdCard.getText().toString().trim();
                if (TextUtil.isEmpty(idCard)){
                    showToast("身份证号不能为空！");
                    return;
                }

                String phone = etPhone.getText().toString().trim();
                if (TextUtil.isEmpty(phone)){
                    showToast("手机号码不能为空！");
                    return;
                }
                addAVisitor(name,idCard,phone);
            }
        });
    }

    @Override
    public void onShowResult(int requestType, BaseBean result) {
        setResult(RESULT_OK);
        finish();
    }

    @OnClick({R2.id.cb_adult, R2.id.cb_child, R2.id.cb_man, R2.id.cb_girl})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.cb_adult) {
            //成人
            cbAdult.setChecked(true);
            cbChild.setChecked(false);
            identityType = 0;
        } else if (id == R.id.cb_child) {
            //儿童
            cbAdult.setChecked(false);
            cbChild.setChecked(true);
            identityType = 1;
        } else if (id == R.id.cb_man) {
            //男
            cbMan.setChecked(true);
            cbGirl.setChecked(false);
            sexType = 1;
        } else if (id == R.id.cb_girl) {
            //女
            cbMan.setChecked(false);
            cbGirl.setChecked(true);
            sexType = 0;
        }
    }


    /**
     * 编辑或修改出游人信息
     * @param name
     * @param idCard
     * @param phone
     */
    private void addAVisitor(String name, String idCard, String phone) {

        mPresenter.postBodyData(RvFrameConfig.VEHICLE_TOURUSER, 0, ApiConfig.HTTP_URL_ADDANDUPDATE, BaseBean.class, new addTraveler(avisitorId == 0 ? null : avisitorId, name, sexType, phone, idCard, identityType), getHeadMap(), true);
    }


    private Map<String, Object> getHeadMap() {
        Map<String, Object> headMap = new LinkedHashMap<>();
        if (OkGoUtil.getToken() != null)
            headMap.put("Authorization", OkGoUtil.getToken());
        return headMap;
    }


    private class addTraveler {
        private Integer id;
        private String name;
        private Integer sex;  //1男  0女
        private String phone;
        private String idCard;
        private Integer isChild;

        public addTraveler(Integer id, String name, Integer sex, String phone, String idCard, Integer isChild) {
            this.id = id;
            this.name = name;
            this.sex = sex;
            this.phone = phone;
            this.idCard = idCard;
            this.isChild = isChild;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getSex() {
            return sex;
        }

        public void setSex(Integer sex) {
            this.sex = sex;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getIdCard() {
            return idCard;
        }

        public void setIdCard(String idCard) {
            this.idCard = idCard;
        }

        public Integer getIsChild() {
            return isChild;
        }

        public void setIsChild(Integer isChild) {
            this.isChild = isChild;
        }
    }

}
