/**
 * 询报价-成本构成模板详情页面JS
 *
 * Created by likui on 2020/2/28.
 */
'use strict';
var el = {
    id: '#id',
    form: '#form',
    templateCode: '#templateCode',
    templateName: '#templateName',
    pgId: '#pgId',
    costTypeCode: '#costTypeCode',
    purchaseGroupCode: '#purchaseGroupCode',
    remark: '#remark'
};


$(function () {
    $(el.costTypeCode).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=cost_type',
        panelHeight: 'auto',
        limitToList: 'true',
        prompt: '-- 请选择 --',
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        editable: false,
        multiple: true
    });
    $(el.purchaseGroupCode).combobox({
        valueField: 'purchaseGroupCode',
        textField: 'codeWithDesc',
        method: 'POST',
        prompt: '--- 未选择则为全部采购组 ---',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/listAll'
    });
});


/**
 * 数据保存
 *
 * @param call 回调函数
 */
function saveOrUpdate(call) {
    if($(el.form).form('validate')){
        var templateCode = $(el.templateCode).textbox('getValue'),templateName = $(el.templateName).textbox('getValue'), purchaseGroupCode = $(el.purchaseGroupCode).combobox('getValue'),
            costTypeCode = $(el.costTypeCode).combobox('getValues'), costTypeName = $(el.costTypeCode).combobox('getText'),
            remark = $(el.remark).val(), id = $(el.id).val();

        var item = {};
        item.id = id;
        item.templateCode = templateCode;
        item.templateName = templateName;
        item.purchaseGroupCode = purchaseGroupCode;
        item.remark = remark;
        item.costTypeCode = costTypeCode.join(',');
        item.costTypeName = costTypeName;

        $.ajax({
            type: 'POST',
            url: g.ctx + 'pc/inquiry/cost/template/rest/saveOrUpdate',
            data: JSON.stringify(item),
            contentType: 'application/json',
            success: function () {
                call();
            },
            error: function (data) {
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    }
}