import {
    findAllByCategory
} from '../../../api/publics'
import {
    addApplyDetail
} from '../../../api/projectApply'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        options: {},
        dataList: [],
        detailDataList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            options: options,
        })
        this.findMyDeptAssetsList();
        this.newApplyDetail();
    },

    /**
     * 栏目触发事件
     */
    onClickNav(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        list[index].activeId = null;
        list[index].mainActiveIndex = e.detail.index;
        this.setData({
            detailDataList: list
        })
        console.log(e.detail.index, this.data.dataList);
    },

    /**
     * 选项触发事件
     */
    onClickItem(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        list[index].detailId = e.detail.id;
        list[index].detailName = e.detail.text;
        list[index].showDetailsName = e.detail.detailName;
        if (e.detail.units) {
            let itemList = [];
            e.detail.units.split(',').forEach((item, idx) => {
                if (idx == 0) {
                    list[index].useUnitIndex = idx;
                    list[index].useUnit = item;
                }
                itemList.push({
                    id: idx,
                    text: item
                });
            });
            list[index].assetsUnits = itemList;
        }
        list[index].activeId = e.detail.id;
        list[index].showDetailsPopup = false;
        this.setData({
            detailDataList: list
        })
    },

    //新增问题
    newApplyDetail() {
        var data = this.data.detailDataList
        data.push({
            detailId: '',
            detailName: '',
            showDetailsName: '',
            showDetailsPopup: false,
            mainActiveIndex: 0,
            activeId: [],
            useUnit: "",
            useUnitIndex: "",
            assetsUnits: [],
            applyNumber: "",
            assetsVersion: "",
            useReason: ""
        })
        this.setData({
            detailDataList: data
        })
    },

    /**
     * 删除申请详情
     * @param {*} e 
     */
    delApplyDetail(e) {
        var index = e.currentTarget.dataset.index
        var data = this.data.detailDataList
        data.splice(index, 1);
        this.setData({
            detailDataList: data
        })
    },

    //保存
    onSave() {
        let {
            detailDataList,
        } = this.data;
        //数据效验
        if (detailDataList.length > 0) {
            for (let i = 0; i < detailDataList.length; i++) {
                if (!detailDataList[i].detailId || !detailDataList[i].detailName || detailDataList[i].activeId.length == 0) {
                    app.toast("请选择申请明细!")
                    return false;
                }
                if (!detailDataList[i].applyNumber) {
                    app.toast("请填写申请数量!")
                    return false;
                }
                if (!detailDataList[i].useUnit) {
                    app.toast("请选择单位名称!")
                    return false;
                }
            }
        } else {
            app.toast("请添加申请明细!")
        }
        let that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认添加申请内容？',
            success: function (sm) {
                if (sm.confirm) {
                    that.submit()
                } else if (sm.cancel) {
                    console.log('用户点击取消');
                }
            }
        })
    },

    submit() {
        let {
            options,
            detailDataList,
            dataList
        } = this.data;
        let applyDetailList = [];
        detailDataList.forEach(detail => {
            applyDetailList.push({
                superTypeKey: options.applyType,
                typeId: dataList[detail.mainActiveIndex].id,
                typeName: dataList[detail.mainActiveIndex].name,
                assetsId: detail.detailId,
                assetsName: detail.detailName,
                number: detail.applyNumber,
                assetsUnit: detail.useUnit,
                assetsVersion: detail.assetsVersion,
                useReason: detail.useReason,
                pushType:3,
            });
        });
        let params = {
            id: options.id,
            proProjectApplyDetailList: applyDetailList
        }
        addApplyDetail(params).then(res => {
            if (res.code == '200') {
                app.toast("添加申请内容成功！", 'success');
                //跳转页面
                wx.redirectTo({
                    url: '../info/index?businessKey='+this.data.options.id,
                })
            }
        });
    },

    //查询部门资产列表信息
    findMyDeptAssetsList() {
        findAllByCategory(this.data.options.applyType).then(res => {
            if (res.code == '200') {
                let list = [];
                res.data.forEach(item => {
                    let children = [];
                    item.childrenAssetsTypeList.forEach(child => {
                        children.push({
                            id: child.id,
                            text: child.name,
                            units: child.unit,
                            detailName: item.name + ' > ' + child.name
                        });
                    })
                    list.push({
                        id: item.id,
                        text: item.name,
                        children: children
                    });
                });
                this.setData({
                    dataList: list
                })
            }
        });
    },

    //输入申请数量
    onInputNumber(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        let value = e.detail.value;
        list[index].applyNumber = value;
        this.setData({
            detailDataList: list
        })
    },

    //选择资产单位
    onSelectUnit(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        list[index].useUnit = e.detail.text;
        list[index].useUnitIndex = e.detail.id;
        this.setData({
            detailDataList: list
        })
    },

    //输入规格型号
    onInputVersion(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        list[index].assetsVersion = e.detail.value;
        this.setData({
            detailDataList: list
        })
    },

    //输入使用说明
    onInputUseReason(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        list[index].useReason = e.detail.value;
        this.setData({
            detailDataList: list
        })
    },

    //关闭申请明细选择
    onShowPopup(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        list[index].showDetailsPopup = true;
        this.setData({
            detailDataList: list
        })
    },

    //关闭申请明细选择
    onClosePopup(e) {
        var index = e.currentTarget.dataset.index
        let list = this.data.detailDataList;
        list[index].showDetailsPopup = false;
        this.setData({
            detailDataList: list
        })
    },

    returnToPage: function () {
        /*关闭当前页面，跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面*/
        wx.redirectTo({
            url: '../info/index?businessKey='+this.data.options.id,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})