<template>
	<view class="p-root-setoption g_bg_f_5 g_p_10 g_pb_120" style="min-height: 100vh;padding-top: 0;">
    <div v-if="type == 'homeCity'">
      <div style="height: 10px;"></div>
      <div style="background-color: #fff;border-radius: 8px;padding: 0 10px;">
        <view class="g_flex_row_between flex_center itea"
              v-for="(item,index) in cityGroup" :key="index"
              @click="handleChooseCity(item, index)"
        >
          <view class="g_flex_column_center g_fw_500 g_c_0 g_fs_17" hover-class="none" hover-stop-propagation="false"> {{ item.name }} </view>
          <view class="g_flex_column_center" hover-class="none" hover-stop-propagation="false">
            <i class="iconfont icon-duihao icon" style="font-size: 18px;"
               :style="{
                'color':item.checked ? themecolor : 'transparent'
               }"
            ></i>
          </view>
        </view>
      </div>
    </div>
    <div v-if="type == 'homeTab'">
      <div style="height: 10px;"></div>
      <div style="background-color: #fff;border-radius: 8px;padding: 0 10px;">
        <view class="g_flex_row_between flex_center itea"
              v-for="(item,index) in tabGroup" :key="index"
              @click="handleChooseTab(item, index)"
        >
          <view class="g_flex_column_center g_fw_500 g_c_0 g_fs_17" hover-class="none" hover-stop-propagation="false"> {{ item.name }} </view>
          <view class="g_flex_column_center" hover-class="none" hover-stop-propagation="false">
            <i class="iconfont icon-duihao icon" style="font-size: 18px;"
               :style="{
                'color':item.checked ? themecolor : 'transparent'
               }"
            ></i>
          </view>
        </view>
      </div>
    </div>
    <view class="g_mt_32" hover-class="none" hover-stop-propagation="false" style="margin-top: 120px;">
      <rh-button :primaryColor='themecolor' type="primary" btnText="确定" @clickBtn="subNameInfo"
        :loading="isSubmit" size='new'
      />
    </view>
  </view>
</template>

<script>
export default {
	onLoad(options) {
		let that = this;
		that.themecolor = getApp().globalData.themeColor;
		that.type = options.type;
    that.value = Number(options.value);
    uni.setNavigationBarTitle({
      title: that.type == "homeCity" ? "职位默认位置" : that.type == "homeTab" ? "职位默认tab" : "",
    })
    if (that.type == "homeCity") {
      that.cityGroup.forEach(c => {
        c.checked = c.value === that.value;
      });
    } else if (that.type == "homeTab") {
      that.tabGroup.forEach(c => {
        c.checked = c.value === that.value;
      });
    }
	},
	data() {
			return {
        isSubmit:false,
        themecolor:getApp().globalData.themeColor,
				applySwitch: false,
				jobSwitch: false,
				fdbHomeCity: false,
				fdbHomeTab: false,
				appInfo: uni.getStorageSync("miniApp-info"),
        type: "",
        value: "",
        cityGroup:[
          {
            name: "全国",
            value: 1,
            checked: false,
          },
          {
            name: "当前城市",
            value: 0,
            checked: false,
          }
        ],
        tabGroup:[
          {
            name: "全部",
            value: 1,
            checked: false,
          },
          {
            name: "推荐",
            value: 0,
            checked: false,
          }
        ],
			};
		},
	methods: {
    handleChooseCity(item, index) {
      this.cityGroup.forEach((c, i) => {
        c.checked = i === index;
      });
    },
    handleChooseTab(item, index) {
      this.tabGroup.forEach((c, i) => {
        c.checked = i === index;
      });
    },
    subNameInfo(){
      let that = this;
      that.isSubmit = true;
      if (that.type == "homeCity") {
        let newValue = that.cityGroup.find(c => c.checked).value;
        this.G.Put(this.api.merchantManagement_updateField, { fieldName: "fdbHomeCity", fieldValue: newValue }, (res) => {
          that.updateAgencyInfoCache("fdbHomeCity", newValue);
          that.isSubmit = false;
          uni.setStorageSync("LOCATION_SETTING_UPDATED", true);
          uni.removeStorageSync("selectedCity");
          uni.navigateBack();
        });
      } else if (that.type == "homeTab") {
        let newValue = that.tabGroup.find(c => c.checked).value;
        this.G.Put(this.api.merchantManagement_updateField, { fieldName: "fdbHomeTab", fieldValue: newValue }, (res) => {
          that.updateAgencyInfoCache("fdbHomeTab", newValue);
          that.isSubmit = false;
          uni.setStorageSync("LOCATION_SETTING_UPDATED", true);
          uni.navigateBack();
        });
      }
    },
    updateAgencyInfoCache(fieldName, fieldValue) {
      let agencyInfo = uni.getStorageSync("agencyInfo");
      if (agencyInfo) {
        agencyInfo[fieldName] = fieldValue;
        uni.setStorageSync("agencyInfo", agencyInfo);
      }
    },
	},
};
</script>

<style lang="less">
.p-root-setoption{
  .itea{
    border-bottom: 1px solid #ccc;
    height: 60px;
    &:last-child{
      border-bottom: none;
    }
  }
  .g-components-button .bocai_btn_size_new{
		width: 240px !important;
		height: 40px !important;
		line-height: 38px !important;
	}
}
</style>