$(function () {
    $('body').on('click','.delete_file',function (e) {
        e.stopPropagation();
        e.preventDefault();
        $(this).parent().remove();
    });
    $('body').on('click','.delete_img',function (e) {
        e.stopPropagation();
        e.preventDefault();
        $(this).parent().remove();
    });
    $('body').on('click','.delete_video',function (e) {
        e.stopPropagation();
        e.preventDefault();
        $(this).parent().remove();
        $('.videoFlie').show();
    });
    $('body').on('click','.delete_audio',function (e) {
        e.stopPropagation();
        e.preventDefault();
        $(this).parent().remove();
        $('.Sound_m').show();
    });
//查询巡检记录选项
    var QrcordeID,recordID,temlateID,temlateNames;
    inspectRecord();
    function inspectRecord() {
        var info =[];
        UrlSearch();
        function UrlSearch() {
            var name,value;
            var str=location.href;
            var num=str.indexOf("?");
            str=str.substr(num+1);
            var arr=str.split("&");
            for(var i=0;i < arr.length;i++){
                num=arr[i].indexOf("=");
                if(num>0){
                    name=arr[i].substring(0,num);
                    value=arr[i].substr(num+1);
                    this[name]=value;
                    info.push(value)
                }
            }
        }
        console.log(info,'info')
        recordID = info[0];
        QrcordeID = info[1];
        temlateID = info[2];
        temlateNames = getItem('templateName');
        var obj = {
            pageId:QrcordeID,
            pageRecordId:recordID,
            recordParentId:''
        };
        $.ajax({
            url:Url+'/record/v1/getInspection',
            type:'get',
            data:obj,
            success:function (res) {
                console.log(res,'res')
                if (res.code === '200') {
                    setItem("parentIds",res.data.parentId);

                    var recordArray = res.data.recordTemplate.inputList;
                    if (res.data.recordTemplate.recordStatusList != null) {
                        var status = "";
                        for (var r = 0; r<res.data.recordTemplate.recordStatusList.length; r++) {
                            status =` <div class="text mar_bottom">
                                        <p class="title_style"><span style="color: red;position: relative;top: 3px;">*</span>${res.data.recordTemplate.recordStatusList[r].statusContent}：</p>
                                        <div class="radio_list"> 
                                            <input type="text" class="moreStatusA input_style moreStatus${res.data.recordTemplate.recordStatusList[r].id}" value="选择" statusId="${res.data.recordTemplate.recordStatusList[r].statusId}" readonly>
                                            <i class="ico"></i>
                                        </div>
                                    </div>`;
                            var ids = res.data.recordTemplate.recordStatusList[r].id,statusContents = res.data.recordTemplate.recordStatusList[r].statusContent;
                            var statusItemLists = res.data.recordTemplate.recordStatusList[r].statusItemList;
                            $('.status_l').append(status);
                                var itmes = {title:'',value:''},itmesArray = [];
                                for (var j= 0;j<statusItemLists.length;j++) {
                                    itmes = {};
                                    itmes.title = statusItemLists[j].statusContent;
                                    itmes.value = statusItemLists[j].id;
                                    itmesArray.push(itmes)
                                }
                                $(".moreStatus"+ids).select({
                                    title: statusContents,
                                    multi: true,
                                    items: itmesArray
                                });
                         }
                    }else {
                        var status = "";
                        status = `<div class="text mar_bottom">
                                   <p class="title_style"><span style="color: red;position: relative;top: 3px;">*</span>${res.data.recordTemplate.statusName}：</p>
                                   <input type="text" class="input_style statusContent" placeholder="请输入${res.data.recordTemplate.statusName}">
                               </div>`;
                        $('.status_l').append(status);
                    }
                    if (res.data.recordTemplate.checkItemList != "") {
                        for (var t = 0; t < res.data.recordTemplate.checkItemList.length; t++) {
                            if (res.data.recordTemplate.checkItemList[t].group.checkType === "checkbox") {
                                var inTerm = `<div class="text mar_bottom">
                                            <p class="title_style">${res.data.recordTemplate.checkItemList[t].group.groupName}：</p>
                                            <div class="radio_list">
                                                <input type="text" class="input_style inspectTremA inspectTrem${res.data.recordTemplate.checkItemList[t].group.checkType}" value="选择" readonly>
                                                <i class="ico"></i>
                                             </div>
                                    </div>`;
                                $('.inspectTremCon').append(inTerm);
                                var ids = res.data.recordTemplate.checkItemList[t].group.checkType,Items = res.data.recordTemplate.checkItemList[t].checkItemList;
                                inspectTremConFun(Items,ids)
                            }else if (res.data.recordTemplate.checkItemList[t].group.checkType === "radio") {
                                var inTerm = `<div class="text mar_bottom">
                                            <p class="title_style">${res.data.recordTemplate.checkItemList[t].group.groupName}：</p>
                                            <div class="radio_list">
                                                <input type="text" class="input_style inspectTremA inspectTrem${res.data.recordTemplate.checkItemList[t].group.checkType}" value="选择" readonly>
                                                <i class="ico"></i>
                                             </div>
                                    </div>`;
                                $('.inspectTremCon').append(inTerm);
                                var ids = res.data.recordTemplate.checkItemList[t].group.checkType,Items = res.data.recordTemplate.checkItemList[t].checkItemList;
                                inspectTremConRadioFun(Items,ids)
                            }

                        }
                    }
                    for (var i = 0; i < recordArray.length; i++) {
                        if(recordArray[i].inputType === 'text'){
                            var  str =`<div class="text mar_bottom">
                                   <p class="title_style">${recordArray[i].inputName}：</p>
                                   <input type="text" class="input_style recordTrems" placeholder="请输入${recordArray[i].inputName}" recordTrem-id="${recordArray[i].id}">
                               </div>`;
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "dateTime") {
                            var str =` <div class="text mar_bottom">
                                <p class="title_style">${recordArray[i].inputName}：</p>
                                <input type="datetime-local" class="input_style recordTrems" recordTrem-id="${recordArray[i].id}">
                            </div>`
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "date") {
                            var str =`<div class="text mar_bottom">
                                <p class="title_style">${recordArray[i].inputName}：</p>
                                <input type="date" class="input_style recordTrems" recordTrem-id="${recordArray[i].id}">
                            </div>`
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "file") {
                            var str =`<div class="text mar_bottom listsItem01 fileList_warp" recordTrem-id="${recordArray[i].id}">
                                <p class="title_style">${recordArray[i].inputName}：</p>
                               <div class="upload_warp">
                                   <ul class="fileList flex-wrap">
                                       <li class="lastUpload">
                                           <div class="upload_files camera-area">
                                               <form enctype="multipart/form-data" method="post">
                                                   <div class="weui-uploader__input-box">
                                                       <input id="fileToUploads" class="weui-uploader__input fileToUpload fileToUpload1" type="file"  accept=".xls,.doc,.ppt,.pdf,.zip,.rar"">
                                                   </div>
                                               </form>
                                           </div>
                                       </li>
                                   </ul>
                               </div>
                            </div>`
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "mp3") {
                            var  str =`<div class="text mar_bottom mp3_text" recordTrem-id="${recordArray[i].id}">
                                    <p class="title_style">${recordArray[i].inputName}：</p>
                                    <div class="upload_mp3 Sound_m">
                                        <form enctype="multipart/form-data" method="post">
                                            <input type="file" name="fileToUpload" id="audioUpload" accept="audio/*"  capture="microphone" class="mp3File fileToUpload"/>
                                        </form>
                                        <span class="mp3"></span>
                                    </div>
                                </div>`
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "number") {
                            var str =` <div class="text mar_bottom">
                                    <p class="title_style">${recordArray[i].inputName}：</p>
                                    <input type="number" class="input_style recordTrems" recordTrem-id="${recordArray[i].id}">
                                </div>`
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "photo") {
                            var str =`<div class="text mar_bottom listsItem01 photoFile" recordTrem-id="${recordArray[i].id}">
                                    <p class="title_style">${recordArray[i].inputName}：</p>
                                    <div class="weui-cell">
                                        <div class="weui-cell__bd">
                                            <div class="weui-uploader">
                                                <div class="weui-uploader__bd">
                                                    <ul class="weui-uploader__files" id="uploaderFiles">
                    
                                                        <li class="upload_imgs">
                                                            <div class="weui-uploader__input-box">
                                                                <input id="uploaderInput" class="weui-uploader__input" type="file"  accept="image/jpg,image/jpeg,image/png,image/gif"  multiple="">
                                                            </div>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>`;
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "radio") {
                            var str =`<div class="text mar_bottom">
                                    <p class="title_style">${recordArray[i].inputName}：</p>
                                    <div class="radio_list">
                                        <input type="text" class="input_style changeInput input_radio Singleelection${recordArray[i].id}" recordTrem-id="${recordArray[i].id}" placeholder="请选择" readonly>
                                        <i class="ico"></i>
                                    </div>
                                </div>`;
                            $('.warp_form').append(str);
                            var item = recordArray[i].inputItemList;
                            radioRecord(item,recordArray[i].id);
                        }else if (recordArray[i].inputType === "video") {
                            var str =` <div class="text mar_bottom vedio_text" recordTrem-id="${recordArray[i].id}">
                                    <p class="title_style">${recordArray[i].inputName}：</p>
                                    <div class="upload_vedio videoFlie" style="position: relative;">
                                        <form enctype="multipart/form-data" method="post">
                                            <input type="file" name="fileToUpload" id="vedioUpload" accept="video/*" class="mp3File fileToUpload"/>
                                        </form>
                                        <span class="upload_vedioBtn"></span>
                                    </div>
                                </div>`
                            $('.warp_form').append(str)
                        }else if (recordArray[i].inputType === "checkbox") {
                            var str =` <div class="text mar_bottom">
                                    <p class="title_style">${recordArray[i].inputName}：</p>
                                    <div class="radio_list">
                                        <input type="text" class="input_style changeInput  moreChange moreChange${recordArray[i].id}" placeholder="请选择" recordTrem-id="${recordArray[i].id}" readonly>
                                        <i class="ico"></i>
                                    </div>
                                </div>`;
                            $('.warp_form').append(str);
                            var itemCheckbox = recordArray[i].inputItemList;
                            checkboxRecord(itemCheckbox,recordArray[i].id);
                        }
                    }
                }
            },
            error:function () {

            }
        })
    }
    // 提交信息
    $('.submitBtn').click(function () {
         // 检查项
        var inspectTremA = $('.inspectTremA'),checkItemArray = [];
        inspectTremA.each(function () {
            $(this).attr('inspectTrem-info');
            if ($(this).attr('inspectTrem-info') !=undefined) {
                var arr = JSON.parse($(this).attr('inspectTrem-info'));
                console.log('00aaa0')
                for (var i = 0; i < arr.length; i++) {
                    checkItemArray.push(arr[i])
                }
            }
        });
        console.log('checkItemArray',checkItemArray)
        // 记录项
        var inutItme =[],recordTrems = $('.recordTrems'),inputObj = {inputId:'',inputValue:''},changeInput = $('.changeInput');
        recordTrems.each(function () {
            inputObj = {};
            var inputValue = $(this).val(),inputId = $(this).attr('recordTrem-id');
            inputObj.inputId = inputId;
            inputObj.inputValue = inputValue;
            inutItme.push(inputObj);
        });
        changeInput.each(function () {
            inputObj = {};
            inputObj.inputId = $(this).attr('recordTrem-id');
            inputObj.inputValue = $(this).attr('data-values');
            inutItme.push(inputObj);
        });
        // 录音
        var mp3_text = $('.mp3_text');
        mp3_text.each(function () {
            inputObj = {};
            inputObj.inputId = $(this).attr('recordTrem-id');
            inputObj.inputValue = $(this).find('.audio_a').attr('fullURL');
            inutItme.push(inputObj);
        });
        // 视频
        var vedio_text = $('.vedio_text');
        vedio_text.each(function () {
            inputObj = {};
            inputObj.inputId = $(this).attr('recordTrem-id');
            inputObj.inputValue = $(this).find('.video_a').attr('file-Url');
            inutItme.push(inputObj);
        });
        // 文件
        var fileList_warp = $('.fileList_warp'),fileArray = [],fileName = [];
        fileList_warp.each(function () {
            inputObj = {};
            inputObj.inputId = $(this).attr('recordTrem-id');
            var fileList = $(this).find('.fileList .flex-con');
            fileList.each(function () {
                fileArray.push($(this).attr('file-Url')+"-"+$(this).attr('file-Name'));
            });
            inputObj.inputValue = fileArray.join(',');
            inutItme.push(inputObj);
        });
        // 图片
        var photoFile = $('.photoFile'),photoArray = [];
        photoFile.each(function () {
            inputObj = {};
            inputObj.inputId = $(this).attr('recordTrem-id');
            var photoList = $(this).find('.weui-uploader__files .weui-uploader__file');
            photoList.each(function () {
                photoArray.push($(this).attr('img-Url'));
            });
            inputObj.inputValue = photoArray.join(',');
            inutItme.push(inputObj);
        });
        // 状态
        var moreStatusA = $('.moreStatusA'),statusA = [],statusObj = {statusId:"",statusValue:''};
        moreStatusA.each(function () {
            statusObj = {};
            statusObj.statusId = $(this).attr('statusId');
            statusObj.statusValue = $(this).attr('data-values');
            statusA.push(statusObj);
        });
        // 状态
        var Infos = {
            checkItems:checkItemArray,
            createBy:JSON.parse(getItem('UserQrcode')).userId,
            createName:JSON.parse(getItem('UserQrcode')).userName,
            inputs:inutItme,
            pageId:QrcordeID,
            pageRecordParentId:getItem('parentIds'),
            recordParentId:'',
            status:statusA.length !=0? statusA : [],
            statusContent:$('.statusContent').val(),
            templateId:temlateID,
            templateName:temlateNames

        };
        axios({
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/json;charset=UTF-8',
                'Access-Control-Allow-Origin': '*'
            },
            method: 'post',
            url: Url+'/record/v1/addRecordInstance',
            data:Infos
        }).then(function(res){
            if (res.data.code === '200'){
                $.toast("提交成功");
                location.href = 'static/note-detail.html?recodeID='+res.data.data;
                // console.log(res.data.data)
                // location.href = 'static/note-detail.html?recodeID=dacea08a0e2544e3be6ec01f4833fcf9'
            }
        }).catch(function(e){

        });
    });
// 记录项多选-单选
    function checkboxRecord(list,id) {
        var itmes = {title:'',value:''},itmesArray = [];
        for (var j= 0;j<list.length;j++) {
            itmes = {};
            itmes.title = list[j].itemValue;
            itmes.value = list[j].id;
            itmesArray.push(itmes)
        }
        $(".moreChange"+id).select({
            // title: statusContents,
            multi: true,
            items: itmesArray
        });
    }
    function radioRecord(list,id) {
        var itmes = {title:'',value:''},itmesArray = [];
        for (var j= 0;j<list.length;j++) {
            itmes = {};
            itmes.title = list[j].itemValue;
            itmes.value = list[j].id;
            itmesArray.push(itmes)
        }
        $(".Singleelection"+id).select({
            multi: false,
            items: itmesArray
        });
    }
// 检查项
//     checkbox
    function inspectTremConFun(list,id) {
        var itmes = {title:'',value:'',instanceValue:'true'},itmesArray = [];
        for (var j= 0;j<list.length;j++) {
            itmes = {};
            itmes.title = list[j].itemValue;
            itmes.value = list[j].id;
            itmes.instanceValue = 'true';
            itmesArray.push(itmes)
        }
        $(".inspectTrem"+id).select({
            // title: statusContents,
            multi: true,
            items: itmesArray,
            onChange:function (res) {
                var obj = {checkItemId:'',instanceValue:''},TremArray=[];
                for (var i = 0; i < res.origins.length; i++) {
                    obj = {};
                    obj.checkItemId = res.origins[i].value;
                    obj.instanceValue = res.origins[i].instanceValue
                    TremArray.push(obj)
                }
                $('.inspectTrem'+id).attr('inspectTrem-info',JSON.stringify(TremArray))
            }
        });
    }
    // radio
    function inspectTremConRadioFun(list,id) {
        var itmes = {title:'',value:'',instanceValue:'true'},itmesArray = [];
        for (var j= 0;j<list.length;j++) {
            itmes = {};
            itmes.title = list[j].itemValue;
            itmes.value = list[j].id;
            itmes.instanceValue = 'true';
            itmesArray.push(itmes)
        }
        $(".inspectTrem"+id).select({
            multi: false,
            items: itmesArray,
            onChange:function (res) {
                var obj = {checkItemId:'',instanceValue:'true'},TremArray=[];
                for (var i = 0; i < res.origins.length; i++) {
                    obj = {};
                    obj.checkItemId = res.origins[i].value;
                    obj.instanceValue = res.origins[i].instanceValue;
                    TremArray.push(obj)
                }
                $('.inspectTrem'+id).attr('inspectTrem-info',JSON.stringify(TremArray))
            }
        });
    }
});