<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <include file="Common/head" />
    </head>
    <body>
        <include file="Common/header" />
        <div class="am-cf admin-main">
            <include file="Common/sidebar" nav="sales"/>
            <div class="admin-content">
                <div class="admin-content-body">
                    <div class="am-cf am-padding am-padding-bottom-0">
                        <div class="am-fl am-cf">
                            <strong class="am-text-primary am-text-lg">卖货管理</strong> / <small>{$small}商品修改</small>
                        </div>
                    </div>
                    <hr style="margin-top:0px;">
                    <form class="am-form" action="" method="POST" id="saveForm" enctype="multipart/form-data">                        <fieldset>
                            <input type="hidden" name="id" value="{$data['id']}" id="more">
                            <div class="am-form-group">
                                <div class="am-form-group">
                                     <label>收购方式</label>
                                    <div> 
                                        <select name="purchase_type" id="purchase_type">
                                           
                                            <option value="1">某村收购</option>
                                            <option value="0" >统一收购</option>
                                           
                                        </select>
                                    </div>
                                </div>
                                <script type="text/javascript">
                                    <?php if($data['purchase_type'] !='') echo '$("select[name=purchase_type]").val('.$data['purchase_type'].');';?>
                                </script>
                                <div class="am-form-group" id="positions">
                                    <label>
                                    选择村
                                    </label>
                                <div>
                                    <select id="province" name="province_id" style="display:inline;width:200px" <if condition='$info'>disabled</if>>
                                        <if condition="$pos[id4] eq 0"><option value="0">请选择省份</option></if>
                                        <volist name="province" id="vo">
                                            <option value="{$vo.id}" <if condition='$thisRegion[province_id] eq $vo[id]'>selected</if>>{$vo.name}</option>
                                        </volist>
                                    </select>
                                    <select class="city" name="city_id" style="display:<if condition='!$pos'>none<else/>inline</if>;width:200px" <if condition='$info'>disabled</if>>
                                            <volist name="cityList" id="vo">
                                                <option value="{$vo.id}" <if condition='$thisRegion[city_id] eq $vo[id]'>selected</if>>{$vo.name}</option>
                                            </volist>
                                      
                                    </select>
                                    <select id="county" name="county_id" style="display:<if condition='!$pos'>none<else/>inline</if>;width:200px" <if condition='$info'>disabled</if>>
                                       <!--  <if condition='$pos'>
                                          <option value="{$pos['id2']}">{$pos['name2']}</option>  </if> -->
                                            <volist name="countyList" id="vo">
                                                <option value="{$vo.id}" <if condition='$thisRegion[county_id] eq $vo[id]'>selected</if>>{$vo.name}</option>
                                            </volist>
                                      
                                    </select>
                                    <select id="village_id" name="village_id" style="display:<if condition='!$pos'>none<else/>inline</if>;width:200px" <if condition='$info'>disabled</if>>
                                      <!--   <if condition='$pos'>
                                        <option value="{$pos['id1']}">{$pos['name1']}</option></if> -->
                                            <volist name="villageList" id="vo">
                                                <option value="{$vo.id}" <if condition='$thisRegion[village_id] eq $vo[id]'>selected</if>>{$vo.name}</option>
                                            </volist>
                                        
                                    </select>
                                </div>
                              <script type="text/javascript">
                                    <?php 
                                        if($pos['id4']) echo '$("select[name=province_id]").val("'.$pos['id4'].'");';                                          
                                    ?>
                                    
                            </script>
                                </div>
                                </div>                                                                                           
                            <div class="am-form-group">
                                <label>
                                    商品名称
                                </label>
                                <input type="text" name="goods_name" id="goods_name" value="{$data['goods_name']}"/>
                            </div>   
                           
                            <div class="am-form-group">
                                <if condition="$data.pic_goods eq '0'">
                                 <label>
                                  商品图片<strong style="color:red;font-size: 0.1em">(图片大小：750*350)</strong>
                                  </label>
                                  <input type="file" name="img" class="pic_goods" />

                                
                                    <else />
                                <label>商品图片</label>
                                <a href="javascript::"  class="del_link"><img src="__ROOT__/Uploads/{$data.pic_goods}" style="weight:100px;height:100px;" > </a>
                                </if>
                               
                            </div>

                                	

                                  
                                	

                                
                                   <div style="margin-top:10px;">
                                   		 <button type="button" class="am-btn am-btn-success addBtn" id="btns">提交</button>
			                            <a href="javascript:location.href=history.back();">
			                                <button type="button" class="am-btn am-btn-default">返回</button>
			                            </a>
                                   </div>
                           
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

<script type="text/javascript">

		 $(".del_link").on("click",function(){
            var bool = confirm("确认修改图片？");
            if (bool) {
                var ids = $("#more").val();
                $.ajax({
                    type:'post',
                    url:"{:U('Sales/delImg')}",
                    data:{'id':ids},
                    success :function(data){
                        console.log(data);
                        if(data == 1){
                            alert("图片删除成功");
                             setTimeout(function(){
                                        location.reload();
                                    },1000);   
                        }else{
                            alert("图片删除失败");
                        }
                    }
                });           	              
            };
        });

        var dos={$data['purchase_type']};
        if(dos == 0){
           $("#positions").css({display:'none'}); 
        }    
        $("select[name='purchase_type']").on('change',function(){
            var  type1 = $(this).val();             
            if (type1 == 0) {
                $("#positions").css({display:'none'});
            }else if(type1 == 1) {
                $("#positions").css({display:'inline'});
            } 
         });    
       $(".addBtn").on('click',function(){
            var indextype = $("select[name='purchase_type']").val();
            if(indextype == 1){               
                 var village = $("select[name='village_id']").find("option:selected").text();
                //var village = $("select[name='village_id']").val();
                if(village != '' || village != '请选择村'){
                    var goods = $("#goods_name").val();
                            if (goods == ''){
                                alert('商品名称必填');
                            }else{                           
                                $("#saveForm").submit();                          
                            }  
                }                     
            }
            if(indextype == 0){
                var goods = $("#goods_name").val();
                if (goods == ''){
                  alert('商品名称必填');
                } else {             
                    $("#saveForm").submit(); 
                }
            }
        });                   
    var data = <?php echo json_encode($pos?:'');?>;
    var id = data['id4'];
    var cityid = data['id3'];
    var con_id = data['id2'];
    var vii_id = data['id1'];
    xueshen(id);
    //console.log(data);
    // 选择省份
    $("#province").change(function() {
        var province_id = $(this).val();
        xueshen(province_id);
    });
    function xueshen(province_id){
        if (province_id > 0) {
            // 选择省份
            $.post("{:U('cirtiesFromProvince')}", {'province_id': province_id}, function(data) {
                var cityList = data;
                var str = "<option value=\"0\">请选择市</option>";
                for (var i in data) {                   
                    str += "<option value="+data[i]['id']+">"+data[i]['name']+"</option>";
                };
                $(".city").html(str);
                if(cityid){
                    $("select[name=city_id]").val(cityid);
                    xuexian(cityid);
                    cityid = null;

                }
                $(".city").css({
                    display: 'inline',
                    width: '200px'
                });
            });
        } else if (province_id == 0) {
            var str = "";
            $(".city").html(str);
            $(".city").hide();
            $("#county").html(str);
            $("#county").hide();
            $("#village_id").html(str);
            $("#village_id").hide();
        }
    }

    // 选择城市
    $(".city").change(function() {
        var city_id = $(this).val();
        xuexian(city_id);
    });
    function xuexian(city_id){
        if (city_id > 0) {
            // 选择县
            $.post("{:U('countiesFromCity')}", {'city_id': city_id}, function(data) {
                var cityList = data;
                var str = "<option value=\"0\">请选择县</option>";
                for (var i in data) {
                    str += "\
                        <option value="+data[i]['id']+">"+data[i]['name']+"</option>\
                    ";
                };
                $("#county").html(str);
                if(con_id){
                    $("select[name=county_id]").val(con_id);
                    xuancun(con_id);
                    con_id = null;
                }
                $("#county").css({
                    display: 'inline',
                    width: '200px'
                });
            });
        } else if (city_id == 0) {
            var str = "";
            $("#county").html(str);
            $("#county").hide();
            $("#village_id").html(str);
            $("#village_id").hide();
        }
    }

    // 选择县
    $("#county").change(function() {
        var county_id = $(this).val();
        xuancun(county_id);
    });

    function xuancun(county_id){
        if (county_id > 0) {
            // 选择省份
            $.post("{:U('villagesFromCounty')}", {'county_id': county_id}, function(data) {
                var cityList = data;
                var str = "<option value=\"0\">请选择村</option>";
                for (var i in data) {
                    str += "\
                        <option value="+data[i]['id']+">"+data[i]['name']+"</option>\
                    ";
                };
                $("#village_id").html(str);
                if(vii_id){
                    $("select[name=village_id]").val(vii_id);
                    vii_id = null;
                    //xuexian(cityid);
                }
                $("#village_id").css({
                    display: 'inline',
                    width: '200px'
                });
            });
        } else if (county_id == 0) {
            var str = "";
            $("#village_id").html(str);
            $("#village_id").hide();
        }
    }

   
</script>