package com.android.chmo.ui.activity.order;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.app.ChmoApplication;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.OrderRes;
import com.android.chmo.http.service.OrderService;
import com.android.chmo.model.LoginUser;
import com.android.chmo.model.ModelOrder;
import com.android.chmo.utils.CommonUtils;
import com.android.chmo.utils.DateUtils;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;

import static com.android.chmo.R.id.price;

/**
 * Created by Administrator on 2018/8/3.
 */

public class SubmitOrderActivity extends BaseActivity {
    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.name)
    TextView nameView;
    @BindView(R.id.work)
    TextView workView;
    @BindView(R.id.time)
    TextView timeView;
    @BindView(price)
    TextView priceView;

    @BindView(R.id.workName)
    EditText workNameEdit;

    @BindView(R.id.contactName)
    EditText contactNameEdit;
    @BindView(R.id.contactPhone)
    EditText contactPhoneEdit;

    @BindView(R.id.workAddress)
    EditText workAddrEdit;

    @BindView(R.id.remark)
    EditText remarkEdit;

    @BindView(R.id.totalPrice)
    TextView totalPriceViw;

    private ModelOrder order = null;


    @Override
    protected int getContentResId() {
        return R.layout.activity_submit_order;
    }

    @Override
    public void initView() {
        order = (ModelOrder) getIntent().getSerializableExtra("order");

        int width = PixelUtils.getWindowWidth()/3;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        lp.width = width;
        lp.height = width;
        imageView.setLayoutParams(lp);
        fillView();
    }


    private  void  fillView() {
        if(order.model.modelphoto != null && order.model.modelphoto.size()> 0) {
            XUtilsImage.display(imageView, HttpApi.getImgUrl(order.model.modelphoto.get(0).photo), R.mipmap.def_img2);
        }
        nameView.setText(order.model.name);
        workView.setText(order.skill.name);
        timeView.setText(DateUtils.formatDateStr(order.startTime) + "~" + DateUtils.formatDateStr(order.endTime));
        priceView.setText("￥"+order.skill.price + "/h");
        totalPriceViw.setText("共计：" + order.hour + "h/￥" + order.totalPrice + "元");

        LoginUser user = ChmoApplication.getApp().getLoginUser();
        contactPhoneEdit.setText(user.phone);
    }

    @OnClick(R.id.submit)
    public void submit() {
        String workName = workNameEdit.getText().toString();
        String contactName = contactNameEdit.getText().toString();
        String contactPhone = contactPhoneEdit.getText().toString();
        String workAddr = workAddrEdit.getText().toString();
        String remark = remarkEdit.getText().toString();

        if(TextUtils.isEmpty(workName)){
            showToast("请输入工作名称");
            return;
        }
        if(TextUtils.isEmpty(contactName)){
            showToast("请输入联系人");
            return;
        }
        if(TextUtils.isEmpty(contactPhone)){
            showToast("请输入联系方式");
            return;
        }
        if(TextUtils.isEmpty(workAddr)){
            showToast("请输入工作地址");
            return;
        }

        showLoading();
        OrderService.submitOrder(order, workName, contactName,contactPhone, workAddr, remark, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                OrderRes res = new Gson().fromJson(result,OrderRes.class);
                if (res.orderpk != null) {
                    Intent intent = new Intent(getContext(), PayOrderActivity.class);
                    intent.putExtra("orderPk", res.orderpk);
                    openPageForResult(intent, 1);
                }else {
                    showToast("订单提交失败");
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("订单提交失败");
            }
        });
    }

    @OnClick(R.id.container)
    public void containerClick() {
        CommonUtils.closeKeybord(workNameEdit,this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK && requestCode == 1) {
            setResult(RESULT_OK);
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
