/**
 * 标段相关js
 *
 * @author CaiAoLin
 * @date 2018/2/5
 * @version
 */

// 整理数据
let tenderData = [];
try {
    if (tenderList.length <= 0) {
        throw '数据为空';
    }
    for(const tmp of tenderList) {
        const tmpData = {
            value: 7814964.00,
            name: tmp.name,
        };
        tenderData.push(tmpData);
    }
} catch (error) {

}
// 属性
function loadCommonProperty () {
    // 合同信息
    $('#build-name').val(property.deal_info.buildName);
    $('#deal-code').val(property.deal_info.dealCode);
    $('#deal-name').val(property.deal_info.dealName);

    // 参建单位
    // 建设单位
    $('#build-company').val(property.construction_unit.build.company);
    $('#build-corporation').val(property.construction_unit.build.corporation);
    $('#build-date').val(property.construction_unit.build.date);
    // 承包单位1
    $('#contract1-company').val(property.construction_unit.contract1.company);
    $('#contract1-corporation').val(property.construction_unit.contract1.corporation);
    $('#contract1-date').val(property.construction_unit.contract1.date);
    // 承包单位2
    $('#contract2-company').val(property.construction_unit.contract2.company);
    $('#contract2-corporation').val(property.construction_unit.contract2.corporation);
    $('#contract2-date').val(property.construction_unit.contract2.date);
    // 监理单位1
    $('#supervision1-company').val(property.construction_unit.supervision1.company);
    $('#supervision1-corporation').val(property.construction_unit.supervision1.corporation);
    $('#supervision1-date').val(property.construction_unit.supervision1.date);
    // 监理单位2
    $('#supervision2-company').val(property.construction_unit.supervision2.company);
    $('#supervision2-corporation').val(property.construction_unit.supervision2.corporation);
    $('#supervision2-date').val(property.construction_unit.supervision2.date);

    // 技术参数
    $('#loadLevel').val(property.tech_param.loadLevel);
    $('#length').val(property.tech_param.loadLength);
    $('#start-peg').val(property.tech_param.startPeg);
    $('#end-peg').val(property.tech_param.endPeg);
    $('#lane-count').val(property.tech_param.laneCount);
    $('#deal-period').val(property.tech_param.dealPeriod);
    $('#start-date').val(property.tech_param.startDate);
    $('#plan-end-date').val(property.tech_param.planEndDate);
}
// 小数位数
function loadCalculateProperty () {
    $('#decimal-up').val(property.decimal.up);
    $('#decimal-tp').val(property.decimal.tp);
    $('#decimal-pay')[0].checked = property.decimal.pay;
    $('#decimal-pay-tp').val(property.decimal.payTp);
}
// 清单精度
function loadPrecisionProperty () {
    $('#unit-t').val(property.precision.t.value);
    $('#unit-km').val(property.precision.km.value);
    $('#unit-m').val(property.precision.m.value);
    $('#unit-m2').val(property.precision.m2.value);
    $('#unit-m3').val(property.precision.m3.value);
    $('#unit-kg').val(property.precision.kg.value);
    $('#unit-ge').val(property.precision.ge.value);
    $('#unit-tai').val(property.precision.tai.value);
    $('#unit-tao').val(property.precision.tao.value);
    $('#unit-ke').val(property.precision.ke.value);
    $('#unit-zu').val(property.precision.zu.value);
    $('#unit-zonge').val(property.precision.zonge.value);
    $('#unit-xitong').val(property.precision.xitong.value);
    $('#unit-other').val(property.precision.other.value);
}
// 合同参数
function loadDealProperty () {
    $('#contract-price').val(property.deal_param.contractPrice);
    $('#zan-lie-price').val(property.deal_param.zanLiePrice);
    $('#c-zl').val(property.deal_param.contractPrice - property.deal_param.zanLiePrice);
    $('#start-advance').val(property.deal_param.startAdvance);
    $('#material-advance').val(property.deal_param.materialAdvance);
}
// 显示设置
function loadDisplayProperty () {
    $('#ledger-dgn-qty')[0].checked = property.display.ledger.dgnQty;
}
// 设置某个div下全部的input、select是否只读
function setReadOnly(obj, readOnly) {
    if (readOnly) {
        $('input', obj).attr('readonly', '');
        $('select', obj).attr('disabled', '');
        $('input[type=checkbox]', obj).attr('disabled', '');
    } else {
        $('input', obj).removeAttr('readonly');
        $('select', obj).removeAttr('disabled');
        $('input[type=checkbox]', obj).removeAttr('disabled');
    }
}
// 标段属性
function loadTenderProperty() {
    // 加载属性
    loadCommonProperty();
    loadCalculateProperty();
    loadPrecisionProperty();
    loadDealProperty();
    loadDisplayProperty();
    // 设置只读
    setReadOnly('#shuxing', true);
}
// 获取当前合同支付应该使用的小数位数
function getDealTpDecimal() {
    const spec = $('#decimal-pay')[0].checked;
    return spec ? _.toNumber($('#decimal-pay-tp').val()) : _.toNumber($('#decimal-tp').val());
}
// 四舍五入
function roundPrice(obj) {
    const iDecimal = getDealTpDecimal();
    obj.val(_.round(_.toNumber(obj.val()), iDecimal));
}
// 计算签约合同价（不含暂列金额）
function calculateC2() {
    const constract = _.toNumber($('#contract-price').val());
    const zanLie = _.toNumber($('#zan-lie-price').val());
    const iDecimal = getDealTpDecimal();
    $('#c-zl').val(_.round(constract - zanLie, iDecimal));
}
// 根据小数位数，计算全部的合同参数
function CalculateAllDealParam() {
    roundPrice($('#contract-price'));
    roundPrice($('#zan-lie-price'));
    roundPrice($('#start-advance'));
    roundPrice($('#material-advance'));
    calculateC2();
}

$(document).ready(function() {
    loadTenderProperty();

    /**
     * 属性
     */
    // 编辑
    $('#edit-1').click(() => {
        setReadOnly('#v-pills-1', false);
        $('#post-1').parent().show();
        $('#edit-1').parent().hide();
    });
    // 取消
    $('#cancel-1').click(() => {
        setReadOnly('#v-pills-1', true);
        loadCommonProperty();
        $('#post-1').parent().hide();
        $('#edit-1').parent().show();
    });
    // 提交
    $('#post-1').click(() => {
        const prop = {
            deal_info: {
                buildName: $('#build-name').val(),
                dealCode: $('#deal-code').val(),
                dealName: $('#deal-name').val(),
            },
            construction_unit: {
                build: {
                    company: $('#build-company').val(),
                    corporation: $('#build-corporation').val(),
                    date: $('#build-date').val(),
                },
                contract1: {
                    company: $('#contract1-company').val(),
                    corporation: $('#contract1-corporation').val(),
                    date: $('#contract1-date').val(),
                },
                contract2: {
                    company: $('#contract2-company').val(),
                    corporation: $('#contract2-corporation').val(),
                    date: $('#contract2-date').val(),
                },
                supervision1: {
                    company: $('#supervision1-company').val(),
                    corporation: $('#supervision1-corporation').val(),
                    date: $('#supervision1-date').val(),
                },
                supervision2: {
                    company: $('#supervision2-company').val(),
                    corporation: $('#supervision2-corporation').val(),
                    date: $('#supervision2-date').val(),
                },
            },
            tech_param: {
                loadLevel: _.toNumber($('#loadLevel').val()),
                loadLength: _.toNumber($('#length').val()),
                startPeg: $('#start-peg').val(),
                endPeg: $('#end-peg').val(),
                laneCount: _.toNumber($('#lane-count').val()),
                dealPeriod: $('#deal-period').val(),
                startDate: $('#start-date').val(),
                planEndDate: $('#plan-end-date').val(),
            }
        };
        const tenderId = window.location.pathname.split('/')[2];
        console.log(prop);
        postData('/tender/' + tenderId + '/save', prop, function (data) {
            setReadOnly('#v-pills-1', true);
            property.deal_info = data.deal_info;
            property.construction_unit = data.construction_unit;
            property.tech_param = data.tech_param;
            $('#post-1').parent().hide();
            $('#edit-1').parent().show();
        });
    });

    /**
     * 计算参数
      */
    // 编辑
    $('#edit-2').click(() => {
        setReadOnly('#v-pills-2', false);
        $('#post-2').parent().show();
        $('#edit-2').parent().hide();
    });
    // 取消
    $('#cancel-2').click(() => {
        setReadOnly('#v-pills-2', true);
        loadCalculateProperty();
        $('#post-2').parent().hide();
        $('#edit-2').parent().show();
    });
    // 提交
    $('#post-2').click(() => {
        const prop = {
            decimal: {
                up: _.toNumber($('#decimal-up').val()),
                tp: _.toNumber($('#decimal-tp').val()),
                pay: $('#decimal-pay')[0].checked,
                payTp: _.toNumber($('#decimal-pay-tp').val()),
            }
        };
        const tenderId = window.location.pathname.split('/')[2];
        console.log(prop);
        postData('/tender/' + tenderId + '/save', prop, function (data) {
            setReadOnly('#v-pills-2', true);
            property.decimal = data.decimal;
            $('#post-2').parent().hide();
            $('#edit-2').parent().show();
        });
    });

    /**
     * 清单精度
     */
    // 编辑
    $('#edit-3').click(() => {
        setReadOnly('#v-pills-3', false);
        $('#post-3').parent().show();
        $('#edit-3').parent().hide();
    });
    // 取消
    $('#cancel-3').click(() => {
        setReadOnly('#v-pills-3', true);
        loadPrecisionProperty();
        $('#post-3').parent().hide();
        $('#edit-3').parent().show();
    });
    // 提交
    $('#post-3').click(() => {
        const prop = { precision: JSON.parse(JSON.stringify(property.precision)) };
        prop.precision.t.value = _.toNumber($('#unit-t').val());
        prop.precision.km.value = _.toNumber($('#unit-km').val());
        prop.precision.m.value = _.toNumber($('#unit-m').val());
        prop.precision.m2.value = _.toNumber($('#unit-m2').val());
        prop.precision.m3.value = _.toNumber($('#unit-m3').val());
        prop.precision.kg.value = _.toNumber($('#unit-kg').val());
        prop.precision.ge.value = _.toNumber($('#unit-ge').val());
        prop.precision.tai.value = _.toNumber($('#unit-tai').val());
        prop.precision.tao.value = _.toNumber($('#unit-tao').val());
        prop.precision.ke.value = _.toNumber($('#unit-ke').val());
        prop.precision.zu.value = _.toNumber($('#unit-zu').val());
        prop.precision.zonge.value = _.toNumber($('#unit-zonge').val());
        prop.precision.xitong.value = _.toNumber($('#unit-xitong').val());
        prop.precision.other.value = _.toNumber($('#unit-other').val());
        const tenderId = window.location.pathname.split('/')[2];
        postData('/tender/' + tenderId + '/save', prop, function (data) {
            setReadOnly('#v-pills-count', true);
            property.precision = data.precision;
            $('#post-3').parent().hide();
            $('#edit-3').parent().show();
        });
    });

    /**
     * 合同参数
     */
    // 编辑
    $('#edit-4').click(() => {
        setReadOnly('#v-pills-4', false);
        $('#post-4').parent().show();
        $('#edit-4').parent().hide();
    });
    // 取消
    $('#cancel-4').click(() => {
        setReadOnly('#v-pills-4', true);
        loadDealProperty();
        $('#post-4').parent().hide();
        $('#edit-4').parent().show();
    });
    // 提交
    $('#post-4').click(() => {
        const prop = {
            deal_param: {
                contractPrice: _.toNumber($('#contract-price').val()),
                zanLiePrice: _.toNumber($('#zan-lie-price').val()),
                startAdvance: _.toNumber($('#start-advance').val()),
                materialAdvance: _.toNumber($('#material-advance').val()),
            }
        };
        const tenderId = window.location.pathname.split('/')[2];
        postData('/tender/' + tenderId + '/save', prop, function (data) {
            setReadOnly('#v-pills-4', true);
            property.deal_param = data.deal_param;
            $('#post-4').parent().hide();
            $('#edit-4').parent().show();
        });
    });

    /**
     * 显示设置
     */
    // 编辑
    $('#edit-5').click(() => {
        setReadOnly('#v-pills-5', false);
        $('#post-5').parent().show();
        $('#edit-5').parent().hide();
    });
    // 取消
    $('#cancel-5').click(() => {
        setReadOnly('#v-pills-5', true);
        loadDisplayProperty();
        $('#post-5').parent().hide();
        $('#edit-5').parent().show();
    });
    // 提交
    $('#post-5').click(() => {
        const prop = {
            display: {
                ledger: { dgnQty: $('#ledger-dgn-qty')[0].checked, },
            },
        };
        const tenderId = window.location.pathname.split('/')[2];
        console.log(prop);
        postData('/tender/' + tenderId + '/save', prop, function (data) {
            setReadOnly('#v-pills-5', true);
            property.display = data.display;
            $('#post-5').parent().hide();
            $('#edit-5').parent().show();
        });
    });

    //标段类型选择
    $('#tender_type_select').change(function () {
        const type = $(this).val() != 0 ? '/?type='+$(this).val() : '';
        window.location.href = '/tender' + type;
    });
    //编辑标段 选择
    $('.save-btn').on('click',function () {
        $('#saveid').val($(this).attr('tender-id'));
        $('#savename').val($(this).attr('tender-name'));
        $('#savetype').val($(this).attr('tender-type'));
    });
    //删除标段 选择
    $('.del-btn').on('click',function () {
        $('#delid').val($(this).attr('tender-id'));
    });
});