<?php defined('BASEPATH') or exit('No direct script access allowed');?>
<?php echo form_open(current_url(), 'id="addressForm" class="form-horizontal"');?>
  <div class="form-group">
    <label class="col-sm-2 control-label">Nama</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="name" value="<?php echo set_value('name', $name);?>" maxlength="32" placeholder="Alamat 1" required="required"/>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label">No. Telepon</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="phone" value="<?php echo set_value('phone', $phone);?>" maxlength="32" placeholder="081234567890" required="required"/>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label">Provinsi</label>
    <div class="col-sm-10">
      <select class="form-control" name="province">
        <option value="0">-- Pilih --</option>
        <?php foreach ($regional as $prov):?>
        <option value="<?php echo $prov['id'];?>" required="required"><?php echo $prov['name'];?></option>
        <?php endforeach;?>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label">Kota</label>
    <div class="col-sm-10">
      <select class="form-control" name="city" required="required"></select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label">Kode POS</label>
    <div class="col-sm-10">
      <input type="text" pattern="([0-9]{5})"class="form-control" name="postal_code" value="<?php echo set_value('postal_code', $postal_code);?>" maxlength="5" required="required"/>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label">Alamat</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="address" value="<?php echo set_value('address', $address);?>" maxlength="255"  required="required"/>
    </div>
  </div>
  <div class="form-group" style="margin-bottom:0">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-primary"><i class="fa fa-check"></i> Simpan</button>
      &nbsp;
      <a href="javascript:history.go(-1);" class="btn btn-default" data-dismiss="modal"><i class="fa fa-remove"></i> Batal</a>
    </div>
  </div>
<?php echo form_close();?>
<script type="text/javascript">
var regional = <?php echo json_encode($regional);?>;
$(document).on("change", '[name="province"]', function() {
    var s = $('[name="city"]');
        s.empty();
    var prov = $('[name="province"] option:selected').val();
    var i;
    for (i = 0; i < regional.length; i++) {
        if (regional[i]['id'] == prov) {
            var options = '<option value="">-- Pilih --</option>';
            var e;
            for (e = 0; e < regional[i]['cities'].length; e++) {
                options += '<option value="'+regional[i]['cities'][e]['id']+'">'+regional[i]['cities'][e]['name']+'</option>';
            }
            s.html(options);
        }
    }
});
</script>
