var pers = []
var table,form,layer;
layui.use(['form','layer','table'], function () {
    table = layui.table;
    form = layui.form;
    layer = layui.layer;

    table.on('tool(table)', function (obj) {
        switch (obj.event) {
            case 'detail': 
                console.log('详情',obj) 
                checkView(obj.data)
                break;
            case 'edit': 
                console.log('修改',obj) 
                updateView(obj.data)
                break;
            case 'del': 
                console.log('删除',obj) 
                delView(obj.data)

                break;
        }
    });
    init();
});

//查询
function queryClick(){
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId"); 
	let fgsName = $('#fgsName').val();
	let proName = $('#proName').val();
	table.reload('layui_table', {
		where: {
			fgsName: fgsName,
			proName: proName,
			yjId: yjId
		}
	})
}
//重置
function resetClick(){
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId"); 
	$('#fgsName').val("");
	$('#proName').val("");
	table.reload('layui_table', {
		where: {
			fgsName: "",
			proName: "",
			yjId: yjId
		}
	})
}
    
/**
 * 初始化数据
 */
function init(){
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId"); 
      // 渲染表格
  table.render({
	id: 'layui_table',
    elem: '#table',
    url: PATH_URL + '/lineProject/getLineProjectList',
	where: {
		'yjId': yjId
	},
	page: true, //开启分页
	method:'post',
    skin: 'line', // 表格样式
    cols: [[
        // {
        //     type: 'checkbox'
        // },
        {
            title: '序号',
            field: 'zizeng',
            align: 'center',
            type: 'numbers'
        },
        {field:'fgsName', title: '分公司',align: 'center',},
        {field:'yjName', title: '运检站',align: 'center',},
        {field:'proName', title: '线路工程名称',align: 'center',},
        {field:'volLevel', title: '电压等级',align: 'center',},
        {
			title: '单/双回路',
			field: '',
			align: 'center',
			templet: function(d){
				let isTwo = d.isTwo;
				let html;
				if(isTwo == '0' || isTwo == 0){
					html = '<span>单回路</span>'
				}else{
					html = '<span>双回路</span>'
				}
				// 返回模板内容
				return html;
			}
		},
        {field:'powerNum', title: '杆塔数量',align: 'center',},
        {field:'lineNum', title: '群众护线员数量',align: 'center',width:170},
        {field:'inLinePowerNum', title: '已绑定护线员杆塔数量',align: 'center',width:170},
        {field:'unLinePowerNum', title: '未绑定护线员杆塔数量',align: 'center', },
        {
            title: '操作',
            toolbar: '#opeator-bar',
            align: 'center',
            width: 200
        }
    ]],
	done: function(res,curr,count){
		//checkPermission();
	}
  });
     
}

/**
 * 新增页面
 * */
function add() {
    var height = '90%';
    var width = '55%';
    var index = layer.open({
        title: ['新增', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
        type: 2,
        content: 'projectAdd.html',
        area: [width, height],
        maxmin: false,
        success: function (layero, index) {
            // //打开页面成功时执行
            var myIframe = window[layero.find('iframe')[0]['name']];
            //getTaskSource()为子页面的方法
            // myIframe.getTaskSource("taskSourceId","");

        },
        yes: function (index, layero) {
            // //提交子页面时执行
            // // 获取弹出层中的form表单元素
            // var formSubmit = layer.getChildFrame('form', index);
            // var submited = formSubmit.find('button')[0];
            // // 触发点击事件，会对表单进行验证，验证成功则提交表单，失败则返回错误信息
            // // var myIframe = window[layero.find('iframe')[0]['name']];
            // // myIframe.selectCheck(); //aaa()为子页面的方法
            // submited.click();
            // example.ajax.reload(); // 刷新页面
        },
		end: function(){
		    queryClick();
		}
		
    });
}

/**
 * 详情
 * */
function checkView(vo) {
    let height = '98%';
    let width = '95%';
    $.ajax({
        type: 'POST',
        url: PATH_URL + '/lineProject/getProById',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({"proId": vo.proId}),
        success: function (data) {
			console.log("ssadsad:", data)
			
			
            var index = layer.open({
                title: ['详情'],
                type: 2,
                content: './projectDetail.html',
                area: [width, height],
                maxmin: false,
                success: function (layero, index) {
                    // console.log(data);
                    var myIframe = window[layero.find('iframe')[0]['name']];
                    // myIframe.hideSubmit();
                    var fnc = myIframe.setData(data.data); //aaa()为子页面的方法

                },
            });
        }
    })
}

 
/**
 * 修改
 * */
function updateView(value) { 
	if(value.lineNum > 0){
		layer.msg("该工程已绑定人员，无法修改", { icon: 0 })
		return;
	}
	var height = '90%';
	var width = '55%';
	$.ajax({
		type: 'POST',
		contentType: "application/json; charset=utf-8",
		url: PATH_URL + '/lineProject/getProById',
		data: JSON.stringify({"proId": value.proId}),
		success: function (data) {
			var index = layer.open({
				title: ['修改', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
				type: 2,
				content: 'projectAdd.html',
				area: [width, height],
				maxmin: false,
				success: function (layero, index) {
					//打开页面成功时执行
					var myIframe = window[layero.find('iframe')[0]['name']];
					myIframe.setData(data); //aaa()为子页面的方法
				},
				yes: function (index, layero) {
					// //提交子页面时执行
					// // 获取弹出层中的form表单元素
					// var formSubmit = layer.getChildFrame('form', index);
					// var submited = formSubmit.find('button')[0];
					// // 触发点击事件，会对表单进行验证，验证成功则提交表单，失败则返回错误信息
					// submited.click();
				},
				end: function(){
				    queryClick();
				}
			});
		}
	})
 
}


/**
 * 删除
 * */
function delView(value) {
	if(value.lineNum > 0){
		layer.msg("该工程已绑定人员，无法删除", { icon: 0 })
		return;
	}
	let index = layer.confirm("是否确定删除数据？", function () {
		$.ajax({
			type: 'POST',
			url: PATH_URL + '/lineProject/delProId',
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify({ "proIds": value.proId }),
			success: function (data) {
				layer.close(index);
				if (data.code == 200) {
					parent.layer.alert('删除成功', {icon: 1});
					init()
				} else {
					layer.msg("删除失败", {icon: 2}, function () {
						// example.ajax.reload(); // 刷新页面
					});
				}
			}
		})
	})
}

function importData(){
    var height = '99%';
    var width = '50%';
    var index = layer.open({
        title: [`导入`, 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
        type: 2,
        // content: 'child/orgInformation.html',
        content: 'projectImport.html',
        area: [width, height],
        maxmin: false,
        success: function (layero, index) {
            var myIframe = window[layero.find('iframe')[0]['name']];
			var json = {
				type: '1',
				proId: ""
			}
            myIframe.setParam(json)
        },
        yes: function (index, layero) {
        },
        end: function () {
			queryClick();
        }
    });
}



//导出
function exportData() {
    var fgsName = $("#fgsName").val();
	var proName = $("#proName").val();
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId"); 
    window.location.href = PATH_URL + `/lineProject/exportProData?token=` + token
        + "&fgsName=" + fgsName
		+ "&proName=" + proName
		+ "&yjId=" + yjId
}