<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Kole UI · PlateInput</title>
  <link rel="stylesheet" href="../.design_library/kole-ui/colors_and_type.css" />
  <link rel="stylesheet" href="./PlateInput.css" />
</head>
<body>
  <div class="kole-page">
    <h2 class="kole-h2">PlateInput 车牌号输入</h2>
    <p class="kole-desc">省份简称 + 字母数字组合，自动大写，支持新能源车牌（7 位）。</p>

    <div class="kole-demo">
      <div class="kole-demo__title">基础用法</div>
      <div class="kole-plate" id="plate">
        <div class="kole-plate-field" id="field">
          <select class="kole-plate-prov" id="prov">
            <option>京</option><option>沪</option><option>粤</option>
            <option>津</option><option>冀</option><option>鲁</option><option>苏</option>
          </select>
          <input class="kole-plate-input" role="group" id="input" placeholder="A12345" maxlength="7" />
        </div>
        <div class="kole-plate-tip" id="tip"></div>
      </div>
    </div>
  </div>

  <script>
    var PROV = '京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领';
    var field = document.getElementById('field');
    var prov = document.getElementById('prov');
    var input = document.getElementById('input');
    var tip = document.getElementById('tip');

    function validate(prov, body) {
      if (!body) return { state: 'empty' };
      // 新能源：省+1字母+6位，共 8；标准：省+1字母+5位，共 7
      if (/^[A-Z][A-Z0-9]{6}$/.test(body)) return { state: 'valid', newEnergy: true };
      if (/^[A-Z][A-Z0-9]{5}$/.test(body)) return { state: 'valid', newEnergy: false };
      return { state: 'invalid' };
    }

    function render() {
      var body = input.value.toUpperCase();
      var r = validate(prov.value, body);
      field.classList.remove('is-valid', 'is-invalid');
      tip.className = 'kole-plate-tip';
      if (r.state === 'empty') { tip.textContent = '格式：省份 + 字母 + 5/6 位（新能源 6 位）'; }
      else if (r.state === 'valid') {
        field.classList.add('is-valid'); tip.className = 'kole-plate-tip valid';
        tip.innerHTML = '车牌号合法：' + prov.value + body + '<span class="kole-plate-tag">' + (r.newEnergy ? '新能源' : '普通') + '</span>';
      } else {
        field.classList.add('is-invalid'); tip.className = 'kole-plate-tip invalid';
        tip.textContent = '车牌格式不正确（首位应为字母）';
      }
    }
    input.addEventListener('input', function () { input.value = input.value.toUpperCase().replace(/[^A-Z0-9]/g, '').slice(0, 7); render(); });
    prov.addEventListener('change', render);
    input.addEventListener('focus', function () { field.classList.add('is-focus'); });
    input.addEventListener('blur', function () { field.classList.remove('is-focus'); });
  </script>
</body>
</html>
