/*
 * @Overview 기초 코드 관리
 */
const VueBaseCode = createApp({
  data() {
    return {
      ajax_url: './basecode.ajax.php',
      Bran_Use: false, //지금은 안쓴다
      Bran_filter: '',
      CodeList: [],
      Ncode: {}, //현재선택코드
    }
  },
  mounted() {
    let _this = this;
    _this.getCodeList();
  },
  computed: {
    LCodeList() { //CodeList 정렬
      let _this = this;

      return _this.getSortCodeList();
    },
  },
  methods: {
    getCodeList() {
      let _this = this;
      let postdata = {};
      postdata.mode = 'getAllCodeList';
      ajax_json({
        url: _this.ajax_url,
        type: "POST",
        data: postdata,
        success: function (data) {
          if (data.code) {
            _this.CodeList = data.data;
          }
        },
        error: function (request, status, error) {
          console.log('asdf');
          kuls_alert(error.message);
        }
      });
    },
    getCode(code) {
      let _this = this;

      _this.Ncode = code;
      Tb_basecode.getMainList(code);
    },
    getSortCodeList( Bran_Uid ) {
      let _this = this;

      return _this.CodeList.filter( v => !Bran_Uid || v.Bran_Uid == Bran_Uid )
      .sort((Fir, Sec) => (Fir.Bran_Idx > Sec.Bran_Idx) ? 1 : -1)
      .sort((Fir, Sec) => (Fir.Bran_Idx != Sec.Bran_Idx) ? 0 : ((Fir.Group_Idx > Sec.Group_Idx) ? 1 : -1))
      .sort((Fir, Sec) => (Fir.Bran_Idx != Sec.Bran_Idx || Fir.Group_Idx != Sec.Group_Idx) ? 0 : ((Fir.Code_Idx2 > Sec.Code_Idx2) ? 1 : -1));
    },
    getCodeLevels( Bran_Uid, Group_Uid, fld, ord ) { //부분 레벨 get 정렬포함
      let _this = this;
      let CodePartTemp = _this.CodeList.filter( v => {
        if( typeof(Bran_Uid) == 'undefined' || Bran_Uid === '' ) return v.Bran_Uid == v.Code_Uid;
        if( typeof(Group_Uid) == 'undefined' || Group_Uid === '' ) return v.Bran_Uid == Bran_Uid && v.Group_Uid == v.Code_Uid;
        return  v.Bran_Uid == Bran_Uid && v.Group_uid == Group_Uid;
      });

      let fld_b = 'Bran_Idx';
      let fld_g = 'Group_Idx';
      let fld_c = 'Code_Idx2';
      let sord = 1;

      if( fld ){
        fld_b = fld;
        fld_g = fld;
        fld_c = fld;
      }
      if( ord ){
        sord = ord;
      }

      return CodePartTemp
        .sort((Fir, Sec) => (Fir[fld_b] > Sec[fld_b]) ? 1 : -1)
        .sort((Fir, Sec) => (Fir[fld_b] != Sec[fld_b]) ? 0 : ((Fir[fld_g] > Sec[fld_g]) ? 1 : -1))
        .sort((Fir, Sec) => (Fir[fld_b] != Sec[fld_b] || Fir[fld_g] != Sec[fld_g]) ? 0 : ((Fir[fld_c] > Sec[fld_c]) ? 1 : -1));
    },
  }
}).mount('#frame_contents');


const Tb_basecode = new Tb_basecode_class("#sub_table");
Tb_basecode.init();
