{% extends 'obj_form.html' %}
{% load i18n %}
{% load tj_filters %}



{% block js %}
    {{ block.super }}
	
	var editor = null;
	
	
	{% if cfg.product_photo %}
	
		var content = $("#id_product_content").val();
        if (content!="None"){
            $("#htmlEdit").html(content);
        }

        var E = window.wangEditor
        editor = new E('#htmlEdit')
        editor.config.uploadImgServer = '/mall/points_product/upload/'
        editor.config.uploadImgMaxLength = 20
        editor.config.uploadImgMaxSize = 3 * 1024 * 1024 // 512kb
        editor.config.uploadFileName = 'file'
        editor.create()
 
	
    {% endif %}


    $('#data-content div.product_photo br').remove();
    $('#photo-clear_id,#id_photo').removeClass('form-control');


    
    var html='<tr><th>{% trans "属性名" %}</th>'+
        '<th>{% trans "属性值" %}</th>'+
        '<th>{% trans "单位" %}</th>'+
        '<th>{% trans "备注" %}</th></tr>';
    var s='',sel='', sSel='';
    {% for p in props %}
        s='';
        sel='<select class="form-control prd_props" name="pro_id_{{forloop.counter}}">';
        {% for o in p.ops %}
            sSel='';
            {% ifequal p.pro_id o.0 %}
                sSel='selected="selected"';
            {% endifequal %}
             s = s + '<option value="{{o.0}}" '+sSel+'>{{o.1}}</option>';
        {% endfor %};
        sSel='';
        {% ifequal p.pro_id '' %}
            sSel=' selected="selected"';
        {% endifequal %}
        s=s+'<option value=""  '+sSel+'>-------------</option>';
        sel=sel+s+'</select>';
        html = html + '<tr><input type="hidden" name="rec_id_{{forloop.counter}}" value="{{p.rec_id}}">'+
                '<td>{{ p.label }}</td>'+
                '<td>'+sel+'</td>'+
                '<td>{{ p.unit }}</td>'+
                '<td><input class="form-control" value="{{ p.remark|default_if_none:''}}" name="remark_{{forloop.counter}}" > </td>'+
            '</tr>';
    {% endfor %}
    
    {% if props %}
        $('#tb_props tbody').html(html);
    {% endif %}

    $('#tb_props').append('<input type="hidden" name="rec_cnt" value="{{ props|length }}">');

    $('.btn-save').click(function(){
	
		if(editor){ 
			var editorData = editor.txt.html();
			$("#id_product_content").val(editorData); 
			console.log(editorData);
		}
        $(this).hide();

        //2022-06-12
        if( !$.isEmpty($('#shelf_life_value').val()) ){
            var period=$('#shelf_life_period').val();
            var value=$('#shelf_life_value').val();          
            $('#id_shelf_life').val('{"period":"'+period+'","value":'+value+'}');         

        }
        $.SubmitForm('#obj_form form','{{request.path}}');
    });  
    //2022-06-12
    {% if form.instance.shelf_life %}
        $('#shelf_life_period').val('{{form.instance.shelf_life.period}}');
        $('#shelf_life_value').val({{form.instance.shelf_life.value}});
    {% endif %}


    //2020-11-24
    $('#data-content .btn-delete').click(function(){
        $.ajax({
            url:'/obj/delete/product/{{form.instance.id}}/',                             
            success: function(data) {
                if(data.cnt>0){  
                    toastr.success('当前产品已删除!', '操作成功：',
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );
                    $.RefreshContent("/obj/list/product/");                   
                };
            },

        });       
    
    });


    $('#obj_form .product_photo a').click(function(event){      
        event.preventDefault();
        var src=$(this).attr('href');
        var i="<div style='text-align:center;'><img src='"+src+"' style='height:400px;'></img></div>";
        $.OpenCfmDlg($('#id_name').val(),i);                   
    });
	
	
	
	
	
	
{% endblock %}

{% block subtitle-1 %}
        {% trans "产品管理" %}
{% endblock %}
{% block subtitle-2 %}
        {% trans "产品名称" %}
{% endblock %}


{% block form_content %}
    {{ block.super }}
    

    {% if has_series %}
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'series' %} </label>
            <div class="col-sm-10">{{ form.series }}</div>
        </div>        
    {% endif %}

    {% if form.instance.company.has_external_code %}
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'external_code' %}</label>
            <div class="col-sm-10">{{ form.external_code }}</div>
        </div>
    {% endif %}


    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'name' %} </label>
        <div class="col-sm-10">{{ form.name }}</div>
    </div>
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'barcode' %} </label>
        <div class="col-sm-10">{{ form.barcode }}</div>
    </div>
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'pl' %} </label>
        <div class="col-sm-10">{{ form.pl }}</div>
    </div>
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'standard' %} </label>
        <div class="col-sm-10">{{ form.standard }}</div>
    </div>
    <div class="form-group "> 
        <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'processflow' %} </label>
        <div class="col-sm-10">{{ form.processflow }}</div>
    </div>
    <div class="form-group "> 
        <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'desc' %} </label>
        
		<div class="col-sm-10">{{ form.desc }}</div>
    </div>

    {% ifequal user.company.id 26560 %}
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'specs' %} </label>
            <div class="col-sm-10">{{ form.specs }}</div>
        </div>
    {% endifequal %}

    {% if cfg.product_price %}
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'price' %} </label>
            <div class="col-sm-10">{{ form.price }}</div>
        </div>
    {% endif %}   

    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'remark' %} </label>
        <div class="col-sm-10">{{ form.remark|default_if_none:'' }}</div>
    </div>

    {% if cfg.shelf_life %}
        <div class="form-group">
            <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'shelf_life' %} </label>
            <div class="col-sm-10 input-group">  
                <div class='col-sm-10'>
                    <input type='number' class="form-control" id="shelf_life_value" />
                </div>
                <span class='col-sm-2'>
                    <select id='shelf_life_period'>
                        <option value='day' selected="selected">天</option>                        
                        <option value='month'>个月</option>
                        <option value='year'>年</option>                        
                    </select>
                </span>
            </div>
        </div>
    {% endif %}

    {% if cfg.product_photo %}
        <div class="form-group product_photo">
            <label class="col-sm-2 control-label ">{% get_verbose_field_name form.instance 'photo' %} </label>
            <div class="col-sm-10">{{ form.photo }}</div>
        </div>
    {% endif %}

    

    
	
	
    
    <div {% ifequal user.company.id 26560 %} style="display:none"  {% endifequal %}>
            <table id='tb_props' class="table table-condensed table-responsive table-bordered table-hover"><tbody></tbody></table>
    </div>
   
   
    {% if cfg.product_photo %}
		<div style="margin-bottom:30px"> 
			<h5>产品介绍：</h5>
			<div id="htmlEdit"></div>
		</div>
    {% endif %}

    <div>
        {% if form.instance.id %}
            {% if is_show %}
                <a class="btn btn-danger btn-delete" type="button">{% trans '删除' %}</a>
            {% endif %}
        {% endif %}
        <a class="btn btn-primary btn-save" type="submit" style="float:right;">{% trans '保存' %}</a>
    </div>
	
	<input type="hidden" id="id_product_content" name="contents" value="{{ form.instance.contents }}">
{% endblock %}
