package com.wd.workoffice.ui.activity.bat.work;

import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.DepBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.WorkUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 工作台 - 部门列表 -详情
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkDepDetailActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_manager)
    TextView tvManager;
    @BindView(R.id.rl_person)
    RelativeLayout rlPerson;
    @BindView(R.id.tv_label)
    TextView tvLabel;
    @BindView(R.id.tv_role)
    TextView tvRole;
    @BindView(R.id.switch_open)
    Switch switchOpen;
    private DepBean dataBean;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        RtfUtils.getRtf().getDepDetail(getIntent().getStringExtra("id")).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {

            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                dataBean = JSON.parseObject(data.getData().toString(), DepBean.class);
                tvName.setText(dataBean.getName());
                tvType.setText(WorkUtils.getDepType(dataBean.getType()));
                List<DepBean.DeptRulerListBean> deptRulerList = dataBean.getDeptRulerList();
                String returnData = "";
                if (deptRulerList != null && deptRulerList.size() != 0) {
                    for (DepBean.DeptRulerListBean s : deptRulerList) {
                        returnData = returnData + s.getNickName() + ",";
                    }
                    returnData = returnData.substring(0, returnData.length() - 1);
                } else {
                    returnData = "无";
                }
                tvManager.setText("管理员：" + returnData);

                tvLabel.setText("标签：" + coverList(dataBean.getTag()));
                tvRole.setText("权限：" + coverList(dataBean.getPermission()));
                if (TextUtils.equals(dataBean.getStatus(), "1")) {
                    switchOpen.setChecked(true);
                } else {
                    switchOpen.setChecked(false);
                }
            }
        });
    }

    private String coverList(List<String> data) {
        if (data != null && data.size() != 0) {
            String returnData = "";
            for (String s : data) {
                returnData = returnData.concat(s).concat(",");
            }
            return returnData.substring(0, returnData.length() - 1);
        } else {
            return "无";
        }
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_work_dep_details;
    }


    @OnClick(R.id.rl_person)
    public void onViewClicked() {
        if (dataBean == null) {
            toast("正在加载中，请稍后");
            return;
        }
        startActivity(WorkDepPersonActivity.class,
                "simple", JSON.toJSONString(dataBean.getDeptUserList()),
                "manage", JSON.toJSONString(dataBean.getDeptRulerList()));
    }
}
