<template src='./cncTestLists.html'>
</template>

<script>
    import HeaderMain from "../../components/HeaderMain";
    import api from "../../api/editor";

    export default {
        components: {
            HeaderMain
        },
        data() {
            return {
                cncTestDetailSingle: null,
                houseList: [],
                value: "",
                isShowDialog: false,
                pageModel: null,
                cncTestCaseList:[],
                loading: true,
                currentProject: "所属项目",
                currentProjectHouseId: null,
                fullscreenLoading: false,
                loadingMore:false,
                count: 0,
                pageNo:1,
            };
        },
        created() {
            this.houseList = [];
            this.fullscreenLoading = true;
            api.houseList().then(res => {
                if (res.success) {
                    this.houseList = res.list;
                    this.houseList.unshift({houseId: null,houseName:"全部"});
                    console.log("户型列表",this.houseList);
                }
            });
            this.cncTestCaseList = [];
            this.getCncTestcaseList();
        },
        filters: {
            transtion(val) {
                return String.fromCharCode(64 + parseInt(val));
            },
            currentOptionNum(section) {
                var chnNumChar = [
                    "零",
                    "一",
                    "二",
                    "三",
                    "四",
                    "五",
                    "六",
                    "七",
                    "八",
                    "九"
                ];
                var chnUnitChar = ["", "十", "百", "千", "万", "亿", "万亿", "亿亿"];
                var strIns = "",
                    chnStr = "";
                var unitPos = 0;
                var zero = true;
                while (section > 0) {
                    var v = section % 10;
                    if (v === 0) {
                        if (!zero) {
                            zero = true;
                            chnStr = chnNumChar[v] + chnStr;
                        }
                    } else {
                        zero = false;
                        strIns = chnNumChar[v];
                        strIns += chnUnitChar[unitPos];
                        chnStr = strIns + chnStr;
                    }
                    unitPos++;
                    section = Math.floor(section / 10);
                }
                if (chnStr.indexOf("一十") == 0) {
                    chnStr = chnStr.substr(1);
                }
                return "题目" + chnStr;
            }
        },
        computed: {
            noMore() {
                return this.count === this.cncTestCaseList.length
            },
            disabled() {
                return this.loadingMore || this.noMore
            }
        },
        methods: {
            getCncTestcaseList() {
                let data = {
                    houseId: this.currentProjectHouseId,
                    pageNo: this.pageNo++,
                    pageSize: 20,
                };
                this.loadingMore = true;
                api.cncTestcaseList(data).then(res => {
                    if (res.success) {
                        this.pageModel = res.pageModel;
                        res.pageModel.resultSet.forEach((item, index) => {
                            this.cncTestCaseList.push(item);
                        });
                        this.count = res.pageModel.rowTotal;
                        this.loadingMore = false;
                    }
                    this.fullscreenLoading = false;
                });
            },
            cncTestDetail(testcaseId) {
                let that = this;
                let data = {
                    testcaseId
                };
                api.cncTestDetail(data).then(res => {
                    if (res.success) {
                        that.cncTestDetailSingle = res.single;
                    }
                    that.loading = false;
                });
            },
            showQRDialog(testcaseId) {
                this.isShowDialog = true;
                this.loading = true;
                this.cncTestDetail(testcaseId);
            },
            close() {
                this.isShowDialog = false;
                this.cncTestDetailSingle = null;
            },
            handleCommand(command) {
                this.currentProjectHouseId = null;
                for (let i = 0; i < this.houseList.length; i++) {
                     let element = this.houseList[i];
                    if (element.houseId === command) {
                        this.currentProject = element.houseName;
                        this.currentProjectHouseId = element.houseId;
                        this.fullscreenLoading = true;
                        this.cncTestCaseList = [];
                        this.pageNo = 1;
                        this.getCncTestcaseList();
                        break;
                    }
                }
            },
            useCurrentTest(item) {
                let data = {
                    creator: "admin",
                    houseNameList: item.houseNameList,
                    questionCount: item.questionCount,
                    title: item.title,
                    testcaseId: item.testcaseId,
                    jsonString:
                        '{"pages":[{"elements":[]}],"createDate":1566357290066,"type":"h5","_id":"5d5cb72addd4fa1c047df1d5","title":"标题","description":"描述","canvasHeight":1334,"canvasWidth":750,"bgc":"#B1C096","btnColor":"#5f8a61","cover":"","animatedNameGlobal":"","bodyBackgroundColor":"rgba(255,255,255,0)","animatedNameBackgroundGlobal":"","backgroundAudio":"","loginId":"caodd@elab-plus.com","__v":0}'
                };
                let that = this;
                api.createTest(data).then(res => {
                    if (res.success) {
                        that.$router.push({path: "/h5editor", query: {itemId: res.id, testcaseId: item.testcaseId}});
                    }
                });
            }
        },
        destroyed() {
            this.pageNo = 1;
            this.cncTestCaseList=[];
        }
    };
</script>
<style lang="scss" scoped>
  @import "./cncTestLists.scss";
</style>
