package com.zswc.creativity.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.qmuiteam.qmui.kotlin.onClick
import com.zswc.creativity.R
import com.zswc.creativity.adapter.AreaSelectAdapter
import com.zswc.creativity.base.MBRActivity
import com.zswc.creativity.databinding.ActivityAreaselectBinding
import com.zswc.creativity.model.AreaBean
import com.zswc.creativity.vmodel.AreaSelectVModel
import com.ysnows.base.net.IResp
import java.lang.Exception

//城市选择
class AreaSelectActivity : MBRActivity<AreaSelectVModel, AreaSelectAdapter, ActivityAreaselectBinding>() {

    var type = 1
    var name = ""
    var province_code = ""//省的编码
    var city_code = ""//市的编码
    var area_code = ""//区的编码
    var cityname=""

    var chose=0//1 搜索


    override fun __before() {
        super.__before()

        try {
            chose= intent?.extras?.getInt("chose",0)!!

        }catch (e:Exception){
            chose=0
        }


    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if(chose==1){
            titleBar()!!.tvRight!!.text="确定"
            titleBar()!!.tvRight!!.onClick {
                //筛选
               if(TextUtils.isEmpty(name)){
                   toast("请选择一个省市")
                   return@onClick
               }

                val intent = Intent()
                val bundle = Bundle()
                bundle.putString("name", name)
                bundle.putString("province_code", province_code)
                bundle.putString("city_code", city_code)
                bundle.putString("area_code", area_code)
                bundle.putString("cityname", cityname)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()

            }

        }

        adapter().setOnItemClickListener { adapter, view, position ->
            var bean = adapter.getItem(position) as AreaBean

            if (type == 1) {
                vm.code = bean.code
                province_code = bean.code
                name = bean.name
                type++
                onRefresh()
                return@setOnItemClickListener
            }
            if (type == 2) {
                vm.code = bean.code
                city_code = bean.code
                cityname= bean.name
                name += bean.name
                type++

                onRefresh()
                return@setOnItemClickListener
            }
            if (type == 3) {
                area_code = bean.code
                name += bean.name

                val intent = Intent()
                val bundle = Bundle()
                bundle.putString("name", name)
                bundle.putString("province_code", province_code)
                bundle.putString("city_code", city_code)
                bundle.putString("area_code", area_code)
                bundle.putString("cityname", cityname)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()

                return@setOnItemClickListener
            }

        }
    }


    override fun title(): String {
        return getStr(R.string.chose_city)!!
    }

    override fun initAdapter(): AreaSelectAdapter = AreaSelectAdapter(vm)


    override fun binding(): ActivityAreaselectBinding {
        return ActivityAreaselectBinding.inflate(layoutInflater)
    }

    override fun vmClass(): Class<AreaSelectVModel> {
        return AreaSelectVModel::class.java
    }

    override fun needLoadMore(): Boolean {
        return false
    }

    override fun onListReceive(res: IResp<*>) {
        super.onListReceive(res)
        if (res.empty() && type > 1) {
            val intent = Intent()
            val bundle = Bundle()
            bundle.putString("name", name)
            bundle.putString("province_code", province_code)
            bundle.putString("city_code", city_code)
            bundle.putString("area_code", area_code)
            bundle.putString("cityname", cityname)
            intent.putExtras(bundle)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }


}
