package com.jqyd.son;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import com.jqyd.app.MyApp;
import com.jqyd.model.MyPlanModule;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.UpdataToServer;
import com.jqyd.manager.R;
import com.jqyd.manager.TabTitle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class Jhap extends Activity implements OnClickListener {
private Spinner jhlb;
private EditText ksrq;
private EditText jsrq;
private EditText jhsl;
private EditText customers;
private EditText bz;
private Button up;
private Button back;
private Button add_cust;
private LinearLayout jhsl_layout;
private LinearLayout mbkh_layout;
private Calendar c;
private ArrayAdapter<String> adapter_jhlb=null;
private Optsharepre_interface share_obj = null;
private Optdb_interfce db = null;
private int flag = 0;//0、开始日期1、结束日期
private MyApp myApp;
private ArrayList<String> custIds = new ArrayList<String>();
private ArrayList<String> custNames = new ArrayList<String>();
private String cids = "";
private String sjzg = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.jhap);
jhlb = (Spinner) this.findViewById(R.id.jhlb);
ksrq = (EditText) this.findViewById(R.id.ksrq);
jsrq = (EditText) this.findViewById(R.id.jsrq);
jhsl = (EditText) this.findViewById(R.id.jhsl);
customers = (EditText) this.findViewById(R.id.customers);
bz = (EditText) this.findViewById(R.id.bz);
up = (Button) this.findViewById(R.id.up);
back = (Button) this.findViewById(R.id.back);
add_cust = (Button) this.findViewById(R.id.add_cust);
jhsl_layout = (LinearLayout) this.findViewById(R.id.jhsl_layout);
mbkh_layout = (LinearLayout) this.findViewById(R.id.mbkh_layout);
ksrq.setOnClickListener(this);
jsrq.setOnClickListener(this);
up.setOnClickListener(this);
back.setOnClickListener(this);
add_cust.setOnClickListener(this);
myApp = (MyApp) this.getApplication();
init();
initEnter();
}
//赋默认值
public void init(){
c = Calendar.getInstance();
//业务时间默认当天
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String mDateTime1 = formatter.format(date);
ksrq.setText(mDateTime1);
jsrq.setText(mDateTime1);
jhsl.setText("0");
ArrayList<String> jhlbList = new ArrayList<String>();
jhlbList.add("客户走访");
jhlbList.add("陌生拜访");
jhlbList.add("二次回访");
jhlbList.add("市场检查");
jhlbList.add("促销执行");
adapter_jhlb = new ArrayAdapter<String>(Jhap.this,
android.R.layout.simple_spinner_item, jhlbList);
adapter_jhlb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 绑定适配器
jhlb.setAdapter(adapter_jhlb);
jhlb.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(jhlb.getSelectedItem().equals("陌生拜访")){
jhsl_layout.setVisibility(LinearLayout.VISIBLE);
mbkh_layout.setVisibility(LinearLayout.GONE);
}else{
jhsl_layout.setVisibility(LinearLayout.GONE);
mbkh_layout.setVisibility(LinearLayout.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
}
/**
* 判断结束日期是否大于开始日期
* @param time1
* @param time2
* @return
*/
public boolean flagDate(String time1, String time2){
boolean flag = false;//true,结束日期大于开始日期
long quot = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = ft.parse(time1);
Date date2 = ft.parse(time2);
quot = date2.getTime() - date1.getTime();
if(quot<0){
flag = false;
}else{
flag = true;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == ksrq){
flag = 0;
getDate();
}else if(v == jsrq){
flag = 1;
getDate();
}else if(v == up){
if(jhlb.getSelectedItem().equals("陌生拜访") && jhsl.getText().toString().equals("")){
warnTell("请输入计划数量!");
}else if(!jhlb.getSelectedItem().equals("陌生拜访") && customers.getText().toString().equals("")){
warnTell("请选择目标客户!");
}else{
share_obj = new Optsharepre_interface(Jhap.this);
sjzg = share_obj.getDataFromPres("SJZG");
String ks = ksrq.getText().toString();
String js = jsrq.getText().toString();
boolean flag = flagDate(ks,js);
if(sjzg.equals("请选择") || sjzg.equals("-1")){
warnTell("对不起,请先分配上级主管,然后再使用此功能!");
}else if(!flag){
warnTell("结束日期不能小于开始日期!");
}else{
//获得登录者姓名
db = new Optdb_interfce(Jhap.this);
String userName = db.searchEmpName(share_obj.getDataFromPres("GUID"));
db.close_SqlDb();
MyPlanModule plan = new MyPlanModule();
plan.setCosim(share_obj.getDataFromPres("COSIM"));
plan.setRegsim(share_obj.getDataFromPres("REGSIM"));
plan.setGuid(share_obj.getDataFromPres("GUID"));
plan.setGguid(share_obj.getDataFromPres("GGUID"));
plan.setZdmc(userName);
plan.setBz(bz.getText().toString());
plan.setJhlb(jhlb.getSelectedItemPosition()+1);
plan.setJhsl(Integer.parseInt(jhsl.getText().toString()));
plan.setKsrq(ksrq.getText().toString());
plan.setJsrq(jsrq.getText().toString());
plan.setCustnames(customers.getText().toString());
new JhapThread(plan).start();
}
}
}else if(v == back){
finish();
}else if(v == add_cust){
//保存当前页面的数据至空间中转站myApp
HashMap<String, String> map = new HashMap<String, String>();
map.put("jhlb", jhlb.getSelectedItemPosition()+"");
map.put("ksrq", ksrq.getText().toString());
map.put("jsrq", jsrq.getText().toString());
map.put("bz", bz.getText().toString());
myApp.setJhap_map(map);
myApp.setModuleName("jhap");
startActivity(new Intent().setClass(Jhap.this, TabTitle.class));
finish();
}
}
/**
* 判断是否二次进入当前界面
*/
public void initEnter(){
if(myApp.getIsInit() == 1){//二次进入
HashMap<String, String> map = myApp.getJhap_map();
if(map != null && map.get("jhlb") !=null && !map.get("jhlb").equals("")){//赋值
String jhlbId = map.get("jhlb");
jhlb.setSelection(Integer.parseInt(jhlbId));
ksrq.setText(map.get("ksrq"));
jsrq.setText(map.get("jsrq"));
bz.setText(map.get("bz"));
custIds = myApp.getLxrTel();
custNames = myApp.getLxrTelAndName();
if(myApp.getLxrTel()==myApp.getLxrTelAndName()){
Log.e("istrue","true");
}else{
Log.e("istrue","flase");
}
Log.e("二次进入", custNames.toString());
String showLxrs = "";
for(int i=0;i<custNames.size();i++){
showLxrs+=custNames.get(i)+",";
}
for(int i=0;i<custIds.size();i++){
cids+=custIds.get(i)+",";
}
if(!showLxrs.equals("") && showLxrs.length()>1){
showLxrs = showLxrs.substring(0, showLxrs.length()-1);
}
if(!cids.equals("") && cids.length()>1){
cids = cids.substring(0, cids.length()-1);
}
if(!customers.getText().toString().equals("")){
showLxrs = customers.getText().toString()+","+showLxrs;
}
customers.setText(showLxrs);
}
}else{
customers.setText("");
}
}
//弹出日期框
public void getDate() {
new DatePickerDialog(Jhap.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker arg0, int year, int month,
int date) {
// TODO Auto-generated method stub
String m = "";
String d = "";
if ((month + 1) < 10) {
m = "0" + (month + 1);
} else {
m = (month + 1) + "";
}
if (date < 10) {
d = "0" + date;
} else {
d = date + "";
}
String time = year + "-" + m + "-" + d;
if(flag == 0){
ksrq.setText(time);
}else{
jsrq.setText(time);
}
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)).show();
}
/**
* 清空历史数据
*/
public void cheanHisdatas(){
bz.setText("");
customers.setText("");
init();
}
/**
* 非空警告提示
*/
public void warnTell(String message){
new AlertDialog.Builder(Jhap.this).setTitle("警告").setMessage(message)
.setPositiveButton("确定", null).show();
}
/**
* 计划安排线程
* @author Administrator
*
*/
class JhapThread extends Thread {
private MyPlanModule plan;
public JhapThread(MyPlanModule plan) {
this.plan = plan;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
Message message = new Message();
message.what = 1;
myHander.sendMessage(message);
Message message2 = new Message();
message2.what = 2;
Bundle bundle = new Bundle();
JSONObject object = new JSONObject();
try {
object.put("cosim", plan.getCosim());
object.put("regsim", plan.getRegsim());
object.put("guid", plan.getGuid());
object.put("gguid", plan.getGguid());
object.put("zdmc", plan.getZdmc());
object.put("bz", plan.getBz());
object.put("jhlb", plan.getJhlb());
object.put("jhsl", plan.getJhsl());
object.put("ksrq", plan.getKsrq());
object.put("jsrq", plan.getJsrq());
object.put("custnames", plan.getCustnames());
object.put("custids", cids);
object.put("sjzg", sjzg);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 请求服务器
String result = new UpdataToServer(Jhap.this).dataToServer("JHAP", object);
if (result != null && !result.equals("-1")) {
try {
JSONObject obj = new JSONObject(result);
String res = obj.getString("result");
if (res.equals("0")) {
bundle.putString("msg","上传成功");
} else {
String errMsg = obj.getString("detail");
bundle.putString("msg", errMsg);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
bundle.putString("msg","对不起,网络连接失败!");
}
message2.setData(bundle);
myHander.sendMessage(message2);
Looper.loop();
}
}
Handler myHander = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
showDialog(1);
break;
case 2:
removeDialog(1);
Bundle data = msg.getData();
String message = data.getString("msg");
showToast(message);
break;
}
}
};
/**
* 结果显示
*
* @param message
*/
public void showToast(final String message) {
new AlertDialog.Builder(Jhap.this).setTitle("提示").setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(message.contains("成功")){
cheanHisdatas();
}
}
}).show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(Jhap.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch (id) {
case 1:
dialog.setMessage("正在上传,请稍候……");
break;
}
return dialog;
}
}