<template>
    <view>
        <form @submit.stop.prevent="beforeFormSubmit">
            <view class="mt10" style="padding-bottom: 120px">
                <view class="weui-cells__group weui-cells__group_form mt10">
                    <view class="weui-cells weui-cells_form">
                        <view class="weui-cells searchbar-result" v-if="searchListShow">
                            <view
                                class="weui-cell weui-cell_active weui-cell_access"
                                :data-name="item.aliasName"
                                :data-id="item.id"
                                @tap="setValue"
                                v-for="(item, idx) in storeArray"
                                :key="idx"
                            >
                                <view class="weui-cell__bd weui-cell_primary">
                                    <view>{{ item.aliasName }}</view>
                                </view>
                            </view>
                        </view>
                        <view class="weui-cell">
                            <view class="weui-cell__hd">
                                <label class="weui-label c6">
                                    <text class="cfred">*</text>
                                    报名岗位
                                </label>
                            </view>
                            <view class="weui-cell__bd">
                                <view
                                    class=""
                                    hover-class="none"
                                    :hover-stop-propagation="false"
                                    style="text-align: right; color: #333"
                                    @tap="toJobAndAgencySelect"
                                    data-type="job"
                                >
                                    <view class="" hover-class="none" :hover-stop-propagation="false" v-if="jobName">
                                        {{ jobName }}
                                        <i class="iconfont icon-gengduo1 f16 ml4 dib pr c9" style="line-height: 1"></i>
                                    </view>
                                    <view class="" hover-class="none" :hover-stop-propagation="false" style="text-align: right; color: #ccc" v-else>
                                        请选择岗位
                                        <i class="iconfont icon-gengduo1 f16 ml4 dib pr c9" style="line-height: 1"></i>
                                    </view>
                                </view>
                            </view>
                        </view>
                        <view class="weui-cell">
                            <view class="weui-cell__hd">
                                <label class="weui-label c6">
                                    <text class="cfred">*</text>
                                    面试时间
                                </label>
                            </view>
                            <view class="weui-cell__bd tr">
                                <picker
                                    mode="multiSelector"
                                    :value="dateTime"
                                    @change="changeDateTime"
                                    @columnchange="changeDateTimeColumn"
                                    :range="dateTimeArray"
                                    style="color: #333"
                                >
                                    {{ !!currentTime ? currentTime : '----' }}
                                    <i class="iconfont icon-rili f16 ml4 dib pr c9" style="line-height: 1"></i>
                                </picker>
                            </view>
                        </view>

                        <view class="weui-cell">
                            <view class="weui-cell__hd">
                                <label class="weui-label c6">接待时间</label>
                            </view>
                            <view class="weui-cell__bd tr">
                                <picker
                                    mode="multiSelector"
                                    :value="dateTime2"
                                    @change="changeDateTime2"
                                    @columnchange="changeDateTimeColumn2"
                                    :range="dateTimeArray"
                                    style="color: #333"
                                >
                                    {{ !!currentTime2 ? currentTime2 : '----' }}
                                    <i class="iconfont icon-rili f16 ml4 dib pr c9" style="line-height: 1"></i>
                                </picker>
                            </view>
                        </view>

                        <view class="weui-cell">
                            <view class="weui-cell__hd">
                                <label class="weui-label c6">政策</label>
                            </view>
                            <view class="weui-cell__bd" style="text-align: right">
                                <view style="display: inline-block; text-align: left; color: #333">{{ policy == '' ? '-' : policy }}</view>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
            <view class="btmFix display-flex" style="justify-content: center">
                <button class="dtBtnD mr12" @click.stop="shrink" data-edit="2">取消</button>
                <button type="primary" class="dtBtn ml12" :loading="loading" hover-class="button-hover" formType="submit">确定</button>
            </view>
        </form>
    </view>
</template>

<script>
// pages/newEnroll/index.js
const app = getApp();
var dateTimePicker = require('../../../utils/dateTimePicker.js');
var dateUtil = require('../../../utils/dateUtil.js');
import { nationArray } from '../../../utils/commonUtil';
const mydate = new Date();
export default {
    data() {
        return {
            showUpload: true,
            name: '',
            loading: false,
            idInfo: {},
            index: -1,
            agentArray: [],

            // jobIndex: -1,
            jobArray: [
                // {
                // 	id: 1,
                // 	name: "GG",
                // },
            ],

            storeIndex: -1,

            storeArray: [
                // {
                // 	id: 1,
                // 	name: "GG",
                // },
            ],

            dateTimeArray1: null,
            dateTime1: null,
            startYear: 2022,
            endYear: 2024,
            currentTime: dateUtil.formatDateYMD(mydate.toLocaleDateString()) + ' ' + '12:00',
            currentTime2: dateUtil.formatDateYMD(mydate.toLocaleDateString()) + ' ' + '12:00',
            searchListShow: false,
            searchKeys: '',
            searchKeysA: '',
            inputShowed: false,
            inputVal: '',
            inputShowedA: false,
            inputValA: '',
            storeId: '',
            agentId: '',
            agentName: '',
            storeJobId: '',
            policy: '',
            currentImg: '',
            nationIndex: -1,

            // 民族的索引
            managerRoleClassify: false,

            // 管理员权限
            nationArray,

            currentUser: {},
            jobName: '',
            switch1Checked: false,
            dateTimeArray: '',
            dateTime: '',
            dateTimeArray2: '',
            dateTime2: '',
            imgSrc: '',
            type: '',

            userInfo: {
                nationality: ''
            }
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        let that = this;
        // console.log(JSON.parse(options.info));
        console.log(options);

        // 获取缓存的报名人信息
        let info = uni.getStorageSync('storeJobInfo');
        console.log(info);
        if (app.globalData.isNotEmptyCheck(info)) {
            // 统一给代理岗位相关的字段赋值
            that.setData({
                storeJobId: info.id,
                jobName: info.jobName,
                agentId: info.agentId,
                agentName: info.agentName,
                currentImg: info.currentImg
            });
            this.getPolicyByStoreJobId(that.storeJobId);
        }
        that.setData({
            managerRoleClassify: app.globalData.loginUserInfo.managerRoleClassify
        });
        if (app.globalData.isNotEmptyCheck(options.info)) {
            // 如果随路由传过来的信息有更新,赋值新的数据
            this.setData({
                storeJobId: JSON.parse(options.info).id,
                jobName: JSON.parse(options.info).jobName,
                inputVal: JSON.parse(options.info).storeName || JSON.parse(options.info).aliasName,
                storeId: JSON.parse(options.info).storeId
            });
            this.getPolicyByStoreJobId(this.storeJobId);
        }
        if (app.globalData.isNotEmptyCheck(options.agencyInfo)) {
            that.setData({
                agentName: JSON.parse(options.agencyInfo).agencyName,
                agentId: JSON.parse(options.agencyInfo).id
            });
        }
        if (app.globalData.isNotEmptyCheck(options.imgUrl)) {
            let imgUrl = JSON.parse(options.imgUrl);
            that.uploadIdcardImg(imgUrl);
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // let src = wx.getStorageSync("src");
        // console.log(src);
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var obj = dateTimePicker.dateTimePicker(this.startYear, this.endYear, this.currentTime);
        console.log(obj.dateTimeArray);
        for (let index = 0; index < obj.dateTimeArray[0].length; index++) {
            obj.dateTimeArray[0][index] = obj.dateTimeArray[0][index] + '年';
        }
        for (let index = 0; index < obj.dateTimeArray[1].length; index++) {
            obj.dateTimeArray[1][index] = obj.dateTimeArray[1][index] + '月';
        }
        for (let index = 0; index < obj.dateTimeArray[2].length; index++) {
            obj.dateTimeArray[2][index] = obj.dateTimeArray[2][index] + '日';
        }
        this.setData({
            dateTimeArray: obj.dateTimeArray,
            dateTime: obj.dateTime,
            dateTimeArray2: obj.dateTimeArray,
            dateTime2: obj.dateTime
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function (e) {
        console.log('isHide');
        console.log(e);
        // this.setData({
        // 	userInfo: {
        // 		name: "",
        // 		address: "",
        // 		age: "",
        // 		nationality: "",
        // 		sex: "",
        // 		num: "",
        // 		idCardImageUrl: "",
        // 	},
        // });
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // wx.removeStorageSync("townsManInfo");
        // wx.switchTab({
        // 	url: "../firstBill/index",
        // });
        // console.log(wx.getStorageSync("townsManInfo"));
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    methods: {
        shrink: function () {
            uni.navigateBack({
                delta: -1
            });
        },

        toJobAndAgencySelect(e) {
            console.log(e);
            console.log(this.userInfo);
            this.setStorage().then(() => {
                console.log('--------', 'isIntonavigation');
                uni.navigateTo({
                    url: `../../recordChoice/index?type=${e.currentTarget.dataset.type}`
                });
            });
        },

        showImg() {
            uni.previewImage({
                current: this.currentImg,
                // 当前显示图片的 http 链接
                urls: [this.currentImg] // 需要预览的图片 http 链接列表
            });
        },

        deleteImg() {
            var that = this;
            uni.showModal({
                title: '提示',
                content: '确定要删除图片吗？',
                confirmColor: '#ff4400',
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            imgSrc: '',
                            showUpload: true
                        });
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            });
        },

        bothwayBind(e) {
            console.log(e);
            let type = `userInfo.${e.currentTarget.dataset.type}`;
            this.setData({
                [type]: e.detail.value
            });
        },

        beforeFormSubmit: function (e) {
            var that = this;
            console.log(e);
            that.setData({
                loading: true
            });
            setTimeout(() => {
                that.formSubmit(e);
            }, 1);

            // if (!that.data.switch1Checked) {
            // 	wx.showToast({
            // 		title: "请先阅读并同意《服务协议》及《隐私政策》",
            // 		icon: "none",
            // 		duration: 2000,
            // 	});
            // } else if (app.isEmptyCheck(e.detail.value.userName)) {
            // 	wx.showToast({
            // 		duration: 2000,
            // 		title: "请输入姓名",
            // 		icon: "none",
            // 		mask: true,
            // 	});
            // 	that.setData({
            // 		loading: false,
            // 	});
            // } else if (app.isEmptyCheck(e.detail.value.sex)) {
            // 	wx.showToast({
            // 		duration: 2000,
            // 		title: "请选择性别",
            // 		icon: "none",
            // 		mask: true,
            // 	});
            // 	that.setData({
            // 		loading: false,
            // 	});
            // } else if (app.isEmptyCheck(e.detail.value.age)) {
            // 	wx.showToast({
            // 		duration: 2000,
            // 		title: "请输入年龄",
            // 		icon: "none",
            // 		mask: true,
            // 	});
            // 	that.setData({
            // 		loading: false,
            // 	});
            // } else {
            // 	setTimeout(() => {
            // 		that.formSubmit(e);
            // 	}, 1);
            // }
        },

        setStorage() {
            let that = this;
            uni.removeStorageSync('townsManInfoJob');
            return new Promise((resolve, reject) => {
                console.log(that.storeJobId);
                let data = {
                    currentTime: that.currentTime,
                    currentTime2: that.currentTime2,
                    storeJobId: that.storeJobId,
                    jobName: that.jobName,
                    agentId: that.agentId,
                    agentName: that.agentName
                };
                console.log(data);
                uni.setStorage({
                    key: 'townsManInfoJob',
                    data
                });
                console.log(uni.getStorageSync('townsManInfoJob'));
                resolve();
            });
        },

        formSubmit: function (e) {
            var that = this;
            console.log(e);
            that.setData({
                loading: true
            });
            uni.showLoading({
                title: '提交中...',
                mask: true
            });
            // if (that.data.index == -1) {
            //   wx.showToast({
            //     duration: 2000,
            //     title: '请选择代理',
            //     icon: 'none',
            //     mask: true
            //   })
            //   that.setData({
            //     loading: false
            //   })
            // } else

            // if (that.data.jobIndex == -1) {
            // wx.showToast({
            // 	duration: 2000,
            // 	title: "请选择岗位",
            // 	icon: "none",
            // 	mask: true,
            // });
            // that.setData({
            // 	loading: false,
            // });
            // } else {
            console.log(that.idInfo);
            new Promise(function (resolve, reject) {
                var paramData = {};
                paramData['storeJobId'] = that.storeJobId;
                paramData['policy'] = that.policy;
                paramData['interviewTime'] = that.currentTime;
                paramData['receptionTime'] = that.currentTime2;
                resolve(paramData);
            }).then(function (paramData) {
                console.log(paramData);
                uni.removeStorageSync('townsManInfoJob');
                new Promise((resolve, reject) => {
                    console.log(that.storeJobId);
                    let data = {
                        currentTime: that.currentTime,
                        currentTime2: that.currentTime2,
                        storeJobId: that.storeJobId,
                        jobName: that.jobName,
                        agentName: that.agentName,
                        policy: that.policy
                    };
                    console.log(data);
                    uni.setStorage({
                        key: 'townsManInfoJob',
                        data
                    });
                    console.log(uni.getStorageSync('townsManInfoJob'));
                    resolve();
                }).then(() => {
                    uni.redirectTo({
                        url: '../../newEnroll/index'
                    });
                });
                uni.hideLoading({
                    success: (res) => {}
                });
            });
            // }
        },

        inputTyping: function (e) {
            this.setData({
                inputVal: e.detail.value
            });
            if (e.detail.value.length > 0) {
                this.setData({
                    searchListShow: true
                });
                this.getStoreList(e.detail.value);
            } else {
                this.setData({
                    searchListShow: false
                });
            }
        },

        getStoreList: function (searchKeys) {
            console.log(searchKeys);
            var that = this;
            that.searchKeys = searchKeys;
            // 60秒后重新获取验证码
            uni.request({
                url: app.globalData.ip + '/store/getAllStoreAliasName',
                data: {
                    keys: searchKeys
                },
                header: app.globalData.headers,
                method: 'GET',
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 200) {
                        that.setData({
                            storeArray: res.data.data
                        });
                    }
                },
                fail: function (res) {}
            });
        },

        setValue: function (e) {
            let str = e.currentTarget.dataset.name;
            let storeId = e.currentTarget.dataset.id;
            console.log(storeId);
            this.setData({
                inputVal: str,
                storeId: storeId,
                searchListShow: false
            });
            this.getJobList(storeId);
        },

        getJobList: function (storeId) {
            var that = this;
            return new Promise((resolve, reject) => {
                uni.request({
                    url: app.globalData.ip + '/store/job/getJobNameByStoreId',
                    data: {
                        storeId: storeId
                    },
                    header: app.globalData.headers,
                    method: 'GET',
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 200) {
                            that.setData({
                                jobArray: res.data.data
                            });
                            resolve();
                        }
                    },
                    fail: function (res) {}
                });
            });
        },

        // bindPickerChangeJob: function (e) {
        // 	var that = this;
        // 	console.log("picker发送选择改变，携带值为", e.detail.value);
        // 	var storeJobId = that.data.jobArray[e.detail.value].id;
        // 	console.log(storeJobId);
        // 	that.getPolicyByStoreJobId(storeJobId);
        // 	this.setData({
        // 		jobIndex: e.detail.value,
        // 	});
        // },
        bindPickerChangeNation(e) {
            let that = this;
            console.log('picker发送选择改变，携带值为', e.detail.value);
            var nationName = that.nationArray[e.detail.value].name;
            this.setData({
                nationIndex: e.detail.value,
                ['userInfo.nationality']: nationName
            });
        },

        getPolicyByStoreJobId: function (storeJobId) {
            var that = this;
            // 60秒后重新获取验证码
            uni.request({
                url: app.globalData.ip + '/store/job/getPolicyByStoreJobId',
                data: {
                    storeJobId: storeJobId
                },
                header: app.globalData.headers,
                method: 'GET',
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 200) {
                        that.setData({
                            policy: res.data.data
                        });
                    }
                },
                fail: function (res) {
                    console.log(res);
                }
            });
        },

        changeDateTime(e) {
            var dateTimeArray = this.dateTimeArray;
            var dateTime = e.detail.value;
            this.setData({
                // dateTime: e.detail.value,
                currentTime:
                    dateTimeArray[0][dateTime[0]].replace('年', '') +
                    '-' +
                    dateTimeArray[1][dateTime[1]].replace('月', '') +
                    '-' +
                    dateTimeArray[2][dateTime[2]].replace('日', '') +
                    ' ' +
                    dateTimeArray[3][dateTime[3]]
            });
            console.log(this.dateTimeArray);
        },

        changeDateTimeColumn(e) {
            var arr = this.dateTime;
            var dateArr = this.dateTimeArray;
            arr[e.detail.column] = e.detail.value;
            dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
            for (let index = 0; index < dateArr[2].length; index++) {
                dateArr[2][index] = dateArr[2][index] + '日';
            }
            // .replace("年",'').replace("月",'').replace("日",'')
            this.setData({
                dateTimeArray: dateArr,
                dateTime: arr
            });
        },

        changeDateTime2(e) {
            var dateTimeArray = this.dateTimeArray;
            var dateTime = e.detail.value;
            this.setData({
                // dateTime: e.detail.value,
                currentTime2:
                    dateTimeArray[0][dateTime[0]].replace('年', '') +
                    '-' +
                    dateTimeArray[1][dateTime[1]].replace('月', '') +
                    '-' +
                    dateTimeArray[2][dateTime[2]].replace('日', '') +
                    ' ' +
                    dateTimeArray[3][dateTime[3]]
            });
            console.log(this.dateTimeArray);
        },

        changeDateTimeColumn2(e) {
            var arr = this.dateTime2;
            var dateArr = this.dateTimeArray;
            arr[e.detail.column] = e.detail.value;
            dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
            for (let index = 0; index < dateArr[2].length; index++) {
                dateArr[2][index] = dateArr[2][index] + '日';
            }
            // .replace("年",'').replace("月",'').replace("日",'')
            this.setData({
                dateTimeArray: dateArr,
                dateTime2: arr
            });
        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage1: function () {}
    }
};
</script>
<style>
@import './index.css';
</style>
