<template>
    <div>
        <lj-win-index-common :title="title" api-name="GetL1BuytaskCheckch"
                             :data-reset-callback="handleDataReset"
                             :data-loaded-callback="handleDataLoaded"
                             ref="baseWin"
                             :addable="!isSelectMode"
                             @add-click="addcus"
                             :query-hard-parms="queryHardParms"
                             :query-fields="queryFields"
                             @query-field-click="handleQueryFieldClick">
            <lj-searchbar slot="header_sub" hint="输入供应商收货单号进行查找" @search="handleSearchbar" style="width: 100%"/>
            <div slot="body_list">
                <mu-list class="lj-group">
                    <template v-for="item in allData">
                        <spttask-item :item="item" @click.native="handleCellClick(item)" :key="item"></spttask-item>
                    </template>
                </mu-list>
            </div>
        </lj-win-index-common>
    </div>
</template>

<script>
    import LjWinIndexCommon from "components/lj-win-index-common";
    import LjSearchbar from "components/lj-searchbar";
    import spttaskItem from './spttask-item';

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommon,
            spttaskItem
        },
        data() {
            return {
                winMode: "",
                allData: [],
                queryHardParms: { scid: 0},
                queryFields: [
                    {
                        title: "状态",
                        property: "flag",
                        selectOptions: {
                            options: [{flag: -1, dscrp: "全部"},
                                {flag: 0, dscrp: "待审核"},
                                {flag: 1, dscrp: "在进行"},
                                {flag: 2, dscrp: "已暂停"},
                                {flag: 3, dscrp: "已终止"},
                                {flag: 5, dscrp: "自动完成"},
                                {flag: 6, dscrp: "手动完成"}],
                            valueMap: ["flag", "dscrp"]
                        }
                    },
                    {title: "开始日期", property: "begindate", type: "date"},
                    {title: "结束日期", property: "enddate", type: "date"},
                    {title: "关键字", property: "keyword"}
                ]
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
            }
        },
        methods: {
            handleDataReset(response) {
                this.allData.length = 0;
                this.addList(response.buytaskList);
            },
            handleDataLoaded(response) {
                let moreItems = response.buytaskList;
                if (moreItems.length > 0) {
                    this.addList(moreItems);
                    return true;
                }
                return false;
            },
            addList(list) {
                this.allData = this.allData.concat(list);
            },
            handleSearchbar(text) {
                this.$refs.baseWin.refresh({keyword: text});
            },
            handleCellClick(mainObject) {
                if (this.isSelectMode) {
                    $lj.sendEvent("spttask_select", mainObject);
                    $lj.closeWin();
                } else {
                    $lj.openWin("spttask-detail", {mainObject: mainObject});
                }
            },
            addcus() {
                $lj.openWin("buytask-detail");
            },
            handleQueryFieldClick({property}) {
                if (property === 'cusareaid') {
                    this.$refs.areaChooseDialog.open();
                }
            },
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select";
            },
            title() {
                if (this.winMode === 'single-select') {
                    return "供应商收货单选择";
                } else {
                    return "供应商收货单";
                }
            }
        }
    }
</script>