<template>
    <NavBar v-if="showTab" :showIcon="true" :showTitle="false" title="" backgroundBox="#FFFFFF"></NavBar>
    <div class="page">
        <view class="title">
            <view class="t1">3/5工作地点</view>
            <view class="t2">添加准确的就职工作地址</view>
        </view>
        <!-- <address-select></address-select> -->
        <view class="content">
            <view class="form-item">
                <view class="form-item-title">工作地址</view>
                <view :class="`inner flex-between ${address ? '' : 'placeholder'}`">
                    <!-- <view class="flex-align-center">
                        <image src="@/static/image/icon/position.png" />
                        {{ address || pinias.formData.address || "请选择工作地址" }}
                    </view> -->
                    <wd-col-picker
                        v-model="cityValue"
                        :columns="cityOpstion"
                        value-key="code"
                        label-key="name"
                        :column-change="columnChangeCity"
                        @confirm="handleConfirmCity"
                        use-default-slot
                    >
                        <p :class="{ placeholder: !cityText }" class="text-single">{{ cityText || "请选择公司所在地" }}</p>
                    </wd-col-picker>
                    <view class="arrow"></view>
                </view>
            </view>
        </view>

        <wd-tabbar
            @tap="next"
            custom-style="display: flex;justify-content: center !important;z-index:9;"
            fixed
            placeholder
            safeAreaInsetBottom
        >
            <view class="foot-btn">下一步</view>
        </wd-tabbar>
    </div>
</template>

<script setup>
import { ref } from "vue";
import AddressSelect from "@/components/form/address-select.vue";
import { getTreeListApi } from "@/api/common";
import { useReleasePostionStore } from "./store";
import town from "@/assets/town.json";
/* tab */
const pinias = useReleasePostionStore();
const showTab = ref(true);
const address = ref("");
import { validateForm } from "@/utils/utils";

// 地区选择
/* 获取省市区 */
const cityOpstion = ref([]);
const cityValue = ref([]);

const cityText = ref("");
let citySelectedItems = [];
getTreeListApi({ provinceName: "贵州省" }).then(res => {
    cityOpstion.value.push(res.data[0].children);
});
const columnChangeCity = ({ selectedItem, resolve, finish }) => {
    if (selectedItem.children) {
        console.log(selectedItem.children);
        resolve(selectedItem.children);
    } else {
        // 选择街道
        if (selectedItem.children === null && town[selectedItem.code]) {
            resolve(town[selectedItem.code].map((v, i) => ({ ...v, code: i })));
        } else {
            finish();
        }
    }
};
const handleConfirmCity = ({ selectedItems }) => {
    cityText.value = selectedItems.map(v => v.name).join("-");
    citySelectedItems = selectedItems;
    const [city, district, street] = selectedItems;
    pinias.formData.address = selectedItems.map(v => v.name).join("");
    pinias.formData.provinceCode = 520000;
    pinias.formData.cityCode = city.code;
    pinias.formData.districtCode = district.code;
    pinias.formData.street = street.name;

    /*params.provinceCode = province.code;
    params.provinceName = province.name;
    params.cityCode = city.code;
    params.cityName = city.name;
    params.districtCode = district.code;
    params.districtName = district.name;
    console.log(cityValue); */
};

const next = () => {
    if (validateForm(pinias.formData, ["address"])) {
        uni.navigateTo({
            url: "/pages/recommend/releasePostion/step4"
        });
    }
};
</script>

<style lang="scss" scoped>
@import "./common.scss";
image {
    width: 12px;
    height: 13px;
}
:deep(#u-a-c) {
    .uni-system-choose-location .nav {
        top: 20px !important;
        background-image: none;
    }
    .uni-system-choose-location .nav-btn {
        background-color: #007aff;
        border-radius: 10px;
    }
    .uni-system-choose-location .nav-btn.back {
        top: 50px;
        left: 10px;
        background-color: #007aff;
        border-radius: 10px;
    }
    .uni-system-choose-location .nav-btn.confirm {
        top: 50px;
        right: 10px;
        background-color: #007aff;
        border-radius: 10px;
    }
}
.foot-btn {
    position: fixed;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 622rpx;
    height: 80rpx;
    color: #ffffff;
    background: #1f86ff;
    border-radius: 12rpx;
}
</style>
