
class Tb_Std_class {
    constructor(container,item_cd) {
        this.ajaxUrl = "./reg_prod_result.ajax.php";
        this.container = document.querySelector(container);

        this.item_cd = item_cd;
        this.table = this.container.querySelector(".hy-table");
        this.addBtn = this.container.querySelector(".add-btn") || "";
        this.removeBtn = this.container.querySelector(".remove-btn") || "";
    }

    getMainList(){
        let _this = this;
        let talert = window.setTimeout(()=> { this.tbList.alert("Loading ...") }, 1000);
        ajax_json({
            url: this.ajaxUrl,
            data: { mode: "getRoutingList", ITEM_CD :this.item_cd},
            success: (data) => {
                console.log(data.data);
                this.tbList.setData(data.data);
            },
            error: (req, status, err) => {
                kuls_alert(err.message);
            },
            complete: () => {
                window.clearTimeout(talert);
            },
        });


    }
    downloadIcon = function(cell, formatterParams, onRendered){ //plain text value
        if(!cell.getRow().getData().FILE_NM){
            return "<button class='reverse-btn disabled-btn' disabled>다운</button>";
        }
        return "<button class='reverse-btn'>다운</button>";

    };
    previewIcon = function(cell, formatterParams, onRendered){ //plain text value
        if(!cell.getRow().getData().FILE_NM){
            return "<button class='reverse-btn disabled-btn' diabled>미리보기</button>";
        }
        return "<button class='reverse-btn'>미리보기</button>";

    };
    getFileUrl(ITEM_CD, ROUT_SQNO, FILE_ID,FILE_NM,down){
        let urlStr = '/include/getFile.php'+"?gubun=rout&ITEM_CD="
            + ITEM_CD +
            "&ROUT_SQNO="+ ROUT_SQNO +
            "&FILE_ID="+ FILE_ID +
            "&name="+ FILE_NM 
            
        if(down == true){
            location.href = urlStr;
        }
        return urlStr;
    }
    init(){
        let _this = this;
        const fields = [

            { formatter: "rownum", title: "NO", width: 50 },
            { field: "OP_CD", title: "공정코드`", width: 100 },
            { field: "OP_NM", title: "공정명", width: 150 },
            { title: "작업 표준서", cssClass:"width-230", columns:[
                    { field: "SID",                       visible:false},
                    { field: "FILE_ID",                   visible:false},
                    { field:"FILE_NM", width:115, formatter: this.previewIcon, cellClick:function(e,cell){
                            pop_filedown(
                                _this.getFileUrl(
                                    cell.getRow().getData().ITEM_CD,
                                    cell.getRow().getData().ROUT_SQNO,
                                    cell.getRow().getData().FILE_ID,
                                    cell.getRow().getData().FILE_NM,
                                    false
                                ), cell.getRow().getData().FILE_NM.split(".").pop()
                            );
                        }
                    },
                    { field:"DOWNLOAD", width:115, formatter: this.downloadIcon,
                        cellClick:function(e, cell){

                            _this.getFileUrl(
                                cell.getRow().getData().ITEM_CD,
                                cell.getRow().getData().ROUT_SQNO,
                                cell.getRow().getData().FILE_ID,
                                cell.getRow().getData().FILE_NM,
                                true
                            );
                        }},

                ]
            },
        ];
        const commonFieldData = {
            headerSort: true,
            vertAlign: "middle",
            hozAlign: "center",
            resizable: "header", //Resize 는 헤더에서만 가능
            headerHozAlign: "center", //헤더 정렬
            headerSortTristate: true, //데이터 정렬 => 정/역/초기화
        };
        this.tbList = new Tabulator(this.table, {
            layout: "fitColumns",
            data: [], //기본 데이터
            columns: fields, //컬럼 설정
            columnDefaults: commonFieldData,
            headerSortElement: "<i class='bi'></i>", //정렬 아이콘
        });

        /*
         * @Overview 테이블 만들어지면 데이터 불러오기
         * @Parameter 없음
         */
        this.tbList.on("tableBuilt", () => {
            this.getMainList();
        });
    }
}

class Tb_Draw_class{
    constructor(container,item_cd) {
        this.ajaxUrl = "../../base/item/item.ajax.php";
        this.container = document.querySelector(container);
        this.item_cd = item_cd;
        this.table = this.container.querySelector(".hy-table");
        this.addBtn = this.container.querySelector(".add-btn") || "";
        this.removeBtn = this.container.querySelector(".remove-btn") || "";
    }

    getMainList(){
        let _this = this;
        let talert = window.setTimeout(()=> { this.tbList.alert("Loading ...") }, 1000);
        ajax_json({
            url: this.ajaxUrl,
            data: { mode : "getDrawList", ITEM_CD : this.item_cd},
            success: (data) => {
                console.log(data.data);
                this.tbList.setData(data.data);
            },
            error: (req, status, err) => {
                kuls_alert(err.message);
            },
            complete: () => {
                window.clearTimeout(talert);
            },
        });


    }
    downloadIcon = function(cell, formatterParams, onRendered){ //plain text value
        if(!cell.getRow().getData().FILE_NM){
            return "<button class='reverse-btn disabled-btn' disabled>다운</button>";
        }
        return "<button class='reverse-btn'>다운</button>";

    };
    previewIcon = function(cell, formatterParams, onRendered){ //plain text value
        if(!cell.getRow().getData().FILE_NM){
            return "<button class='reverse-btn disabled-btn' diabled>미리보기</button>";
        }
        return "<button class='reverse-btn'>미리보기</button>";

    };
    previewIcon = function(cell, formatterParams, onRendered){ //plain text value
        if(!cell.getRow().getData().FILE_NM){
            return "<button class='reverse-btn disabled-btn' diabled>미리보기</button>";
        }
        return "<button class='reverse-btn'>미리보기</button>";

    };


    getFileUrl(DRAW_NO, SID, FILE_ID, FILE_NM,down){
        let urlStr = '/include/getFile.php'+"?gubun=draw&nid="
            + DRAW_NO +
            "&sid="+ SID +
            "&file_id="+ FILE_ID+
            "&name="+FILE_NM
        if(down == true){
            location.href = urlStr;
        }
        return urlStr;
    }
    init_draw(){
        let _this = this;
        const fields = [

            { field:"check", title: "선택", width: 50, headerSort: false,
                formatter: tb_common_funtions.formatterCheckboxIcon,
                formatterParams: { checkval: "Y" },
            },
            { formatter: "rownum",  title: "NO", width: 50 },
            { field: "DOWNLOAD",    title: "다운로드", width: 100, formatter: this.downloadIcon,
                cellClick:function(e, cell){
                    _this.getFileUrl(
                        "",
                        cell.getRow().getData().SID,
                        cell.getRow().getData().FILE_ID,
                        cell.getRow().getData().FILE_NM,
                        true
                    );
                }
            },
            { field: "DRAW_VER",    title: "버전",    width: 100 },
            { field: "DRAW_NO",     title: "도면번호", width: 150, editor: "input" ,editable:(cell)=>{ let val = cell.getData(); return val._STATUS_ === 'I' },}, //file의 nid이기도 함
            { field: "FILE_NM",     title: "파일명",  width: 150 },
            { field: "SID",                       visible:false},
            { field: "FILE_ID",                   visible:false},
            { field: "MAKE_TM",     title: "등록일", width: 100,
              formatter: (cell)=>{
                return cell;
              }
            },
            { field: "MAKE_USER_ID",title: "등록자", width: 100 },
            { field: "preview",     title: "미리보기", width: 80,formatter:this.previewIcon,
                cellClick:function(e,cell){

                    pop_filedown(
                        _this.getFileUrl(
                            "",
                            cell.getRow().getData().SID,
                            cell.getRow().getData().FILE_ID,
                            cell.getRow().getData().FILE_NM
                        ), cell.getRow().getData().FILE_NM.split(".").pop()
                    );
                }
            },
        ];
        const commonFieldData = {
            headerSort: true,
            vertAlign: "middle",
            hozAlign: "center",
            resizable: "header", //Resize 는 헤더에서만 가능
            headerHozAlign: "center", //헤더 정렬
            headerSortTristate: true, //데이터 정렬 => 정/역/초기화
        };
        this.tbList = new Tabulator(this.table, {
            layout: "fitColumns",
            data: [], //기본 데이터
            columns: fields, //컬럼 설정
            columnDefaults: commonFieldData,
            headerSortElement: "<i class='bi'></i>", //정렬 아이콘
        });

        /*
         * @Overview 테이블 만들어지면 데이터 불러오기
         * @Parameter 없음
         */
        this.tbList.on("tableBuilt", () => {
            this.getMainList();
        });
    }

}




