<template>
    <div style="background: #fff">
        <div ref="headarea">
            <Affix>
                <slot name="toolbar"></slot>
                <Anchor
                    v-if="showAnchorLink"
                    ref="anchorobject"
                    style="
                        position: absolute;
                        z-index: 99;
                        right: 0px;
                        top: 64px;
                        width: 200px;
                    "
                    :affix="false"
                    :show-ink="false"
                    @on-select="anchorChange"
                    @on-change="anchorChange"
                    offset-top="5"
                    :container="'div#' + headid"
                    :bounds="2"
                >
                    <AnchorLink
                        v-for="temp in curlAnchorData"
                        :key="'anchor_' + temp.id"
                        :href="temp.id"
                        :class="
                            anchorid == temp.id ? 'ivu-anchor-link-active' : ''
                        "
                        v-show="temp.visible"
                        :title="temp.name"
                        style="margin-bottom: 5px; margin-top: 5px"
                    />
                </Anchor>
            </Affix>
        </div>
        <Card
            :padding="0"
            :bordered="false"
            style="
                padding-top: 0px;
                padding-left: 16px;
                padding-right: 16px;
                padding-bottom: 14px;
            "
            dis-hover
        >
            <div :id="headid" :style="style2" @scroll="handleScroll">
                <div
                    v-for="(item, index) in myConfig.formsConfig.items"
                    :id="item.divId"
                    :key="item.divId"
                    v-show="headVisible[index].visible"
                >
                    <Formcard
                        :ref="item.name"
                        :config="item"
                        :dictData="dictData"
                        @doAction="doAction"
                    >
                    </Formcard>
                </div>
                <div
                    v-for="(temp2, index) in detailConfig.items"
                    :key="temp2.divId"
                    :id="temp2.divId"
                    v-show="detailVisible[index].visible"
                >
                    <BillInfoPanelEditTable
                        v-if="temp2.type === 'EditGridCard'"
                        :ref="temp2.name"
                        :config="temp2"
                        :dictData="dictData"
                        @doAction="doAction"
                    />
                    <BillInfoPanelSlotPanel
                        v-if="temp2.type === 'SlotPanelCard'"
                        :ref="temp2.name"
                        :config="temp2"
                        :dictData="dictData"
                        @doAction="doAction"
                    >
                        <!-- 组件原始slot透传 -->
                        <template
                            v-for="(_, name) in $slots"
                            #[name]="slotData"
                        >
                            <slot :name="name" v-bind="slotData || {}"></slot>
                        </template>
                        <!-- ============================ -->
                    </BillInfoPanelSlotPanel>
                    <DetailsBoxs
                        v-if="temp2.type === 'DetailsBox'"
                        :ref="temp2.name"
                        :config="temp2"
                        :dictData="dictData"
                        @doAction="doAction"
                    />
                    <FormCardBox
                        v-if="temp2.type === 'FormCardBox'"
                        :ref="temp2.name"
                        :config="temp2"
                        :dictData="dictData"
                    />
                    <AppendixBoxs
                        v-if="temp2.type === 'AppendixBox'"
                        :ref="temp2.name"
                        :config="temp2"
                        :modulecode="modulecode"
                        @doAction="doAction"
                    />
                </div>
            </div>
        </Card>
    </div>
</template>
<script>
import billdetailform from "../billdetailform.vue";
import BillInfoPanelEditTable from "./BillInfoPanelEditTable.vue";
import BillInfoPanelSlotPanel from "./BillInfoPanelSlotPanel.vue";

export default {
    name: "BillInfoPanel",
    extends: billdetailform,
    components: { BillInfoPanelEditTable, BillInfoPanelSlotPanel },
    data() {
        return {};
    },
    computed: {},
    methods: {
        async checkValidate() {
            let flag = false;
            // 单头校验
            for (
                let i = 0, len = this.myConfig.formsConfig.items.length;
                i < len;
                i++
            ) {
                const el = this.myConfig.formsConfig.items[i];
                if (this.headVisible[i].visible) {
                    flag = await this.checkCommonValidate(el);
                    // console.log("单头校验:", flag);
                }
            }
            // 明细校验
            if (!flag) {
                if (this.detailConfig.items) {
                    for (
                        let i = 0, len = this.detailConfig.items.length;
                        i < len;
                        i++
                    ) {
                        const el = this.detailConfig.items[i];
                        if (!flag) {
                            if (this.detailVisible[i].visible) {
                                flag = await this.checkCommonValidate(el);
                                // console.log("明细校验:", flag);
                            }
                        }
                    }
                }
            }
            // console.log("flag结束校验:::", flag);
            // return true;
            return flag;
        },

        /**
         * 覆写详情校验
         */
        async checkCommonValidate(el) {
            let flag = false;
            if (
                this.$refs[el.name] &&
                this.$refs[el.name][0] &&
                this.$refs[el.name][0].checkValidate
            ) {
                if (el.type === "FormCardBox") {
                    this.$refs[el.name][0].checkValidate();
                    flag = this.$refs[el.name][0].isError();
                } else {
                    if (el.type === "EditGridCard") {
                        flag = await this.$refs[
                            el.name
                        ][0].tableCheckValidate();
                    } else {
                        flag = this.$refs[el.name][0].checkValidate();
                    }
                }
            }
            return flag;
        },
        /**
         * 覆写设置明细数据
         */
        setData(data) {
            this.myConfig.formsConfig.items.forEach((el) => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].setData(data);
                }
            });
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach((el) => {
                    if (data[el.name]) {
                        if (this.$refs[el.name] && this.$refs[el.name][0]) {
                            if (el.type === "DetailsBox") {
                                let tempDetailData = this.setDetailsBox(
                                    el,
                                    data
                                );
                                this.$refs[el.name][0].setData(tempDetailData);
                            } else {
                                if (
                                    el.type === "FormCardBox" ||
                                    el.type === "FormCardBox2"
                                ) {
                                    if (data[el.name].length > 0) {
                                        this.$refs[el.name][0].setData(
                                            data[el.name][0]
                                        );
                                    } else {
                                        this.$refs[el.name][0].clearData();
                                    }
                                } else {
                                    this.$refs[el.name][0].setData(
                                        data[el.name]
                                    );
                                }
                            }
                        }
                    } else {
                        if (this.config.response === el.name) {
                            if (
                                el.type === "FormCardBox" ||
                                el.type === "FormCardBox2"
                            ) {
                                let temp = {};
                                if (el.items) {
                                    el.items.forEach((el2) => {
                                        if (el2.name) {
                                            temp[el2.name] = data[el2.name];
                                        }
                                        if (el2.sname) {
                                            temp[el2.sname] = data[el2.sname];
                                        }
                                        if (el2.ename) {
                                            temp[el2.ename] = data[el2.ename];
                                        }
                                        if (el2.textName) {
                                            temp[el2.textName] =
                                                data[el2.textName];
                                        }
                                    });
                                }
                                this.$refs[el.name][0].setData(temp);
                            }
                        } else {
                            if (
                                el.type !== "AppendixBox" ||
                                el.type !== "AppendixBox2"
                            ) {
                                if (
                                    this.$refs[el.name] &&
                                    this.$refs[el.name][0]
                                ) {
                                    // ==============================================
                                    // SlotPanelCard 插槽数据赋值 覆写
                                    if (el.type === "SlotPanelCard") {
                                        this.$refs[el.name][0].setData({...data});
                                    } else {
                                        this.$refs[el.name][0].setData([]);
                                    }
                                    // ==============================================
                                }
                            }
                        }
                    }
                });
            }
            if (this.billAppendixConfig.showAppendix) {
                if (
                    this.$refs.detail_appenndix &&
                    this.$refs.detail_appenndix[0]
                ) {
                    let billno = data[this.billAppendixConfig.appendixKey];
                    let billmoduleid = data.billmoduleid;
                    this.$refs.detail_appenndix[0].setData(
                        billno,
                        billmoduleid
                    );
                }
            } else if (this.billAppendixConfig.showAppendix2) {
                if (
                    this.$refs.detail_appenndix2 &&
                    this.$refs.detail_appenndix2[0]
                ) {
                    let billno = data[this.billAppendixConfig.appendixKey];
                    let billmoduleid = data.billmoduleid;
                    this.$refs.detail_appenndix2[0].setData(
                        billno,
                        billmoduleid
                    );
                }
            }
            this.$nextTick(() => {
                this.resetAnchor();
                this.curlEditObject = "";
            });
        },
    },
    mounted() {
        // console.log("我继承覆写了8888");
    },
};
</script>

<style lang="scss" scoped></style>
