<template>
    <div>
        <lj-win-index-common :title="title" api-name="GetL1Cpcheck"
                             :data-reset-callback="handleDataReset"
                             :data-loaded-callback="handleDataLoaded"
                             ref="baseWin"
                             :addable="false"
                             :query-hard-parms="queryHardParms"
                             :query-fields="queryFields"
                             @query-field-click="handleQueryFieldClick">
            <mu-paper :z-depth="3" slot="header_sub" style="background-color: white;">
                <lj-searchbar hint="质检单号/委外订单号/订单相关号/送检单号" @search="handleSearchbar" style="width: 100%"/>
                <div style="margin-left: 10px;">待审核:{{firstAuditCount}},待二审:{{secondAuditCount}} </div>
            </mu-paper>
            <div slot="body_list">
                <div>
                    <template v-for="(item,index) in allData">
                        <cpcheck-item :item="item" @click.native="handleCellClick(item)" :key="index"/>
                    </template>
                </div>
            </div>
        </lj-win-index-common>
        <l1-mtrltype-choose ref="mtrltypeChooseDialog" @mtrltype-chosen="handleMtrltypeChosen"/>
    </div>
</template>

<script>
    import LjWinIndexCommon from "components/lj-win-index-common";
    import LjSearchbar from "components/lj-searchbar";
    import L1MtrltypeChoose from "components/L1APP/public/l1-mtrltype-choose";
    import CpcheckItem from "./cpcheck-ww-item";

    export default {
        components: {
            CpcheckItem,
            LjSearchbar,
            LjWinIndexCommon,
            L1MtrltypeChoose
        },
        data() {
            return {
                firstAuditCount: 0,
                secondAuditCount: 0,
                winMode: "",
                allData: [],
                queryHardParms: {billtype: 5},
                queryFields: [
                    {
                        title: "分部",
                        property: "scid",
                        selectOptions: {
                            optionsApi: "GetL1Scdef",
                            valueMap: ["scid", "scname"],
                            extraOptions: [{scid: -1, scname: "全部"}]
                        }
                    },
                    {
                        title: "状态",
                        property: "status",
                        selectOptions: {
                            options: [{status: -1, dscrp: "全部"},
                                {status: 0, dscrp: "待审核"},
                                {status: 1, dscrp: "待二审"},
                                {status: 2, dscrp: "已完成"}
                            ],
                            valueMap: ["status", "dscrp"]
                        }
                    },
                    {
                        title: "物料类别",
                        property: "mtrltypeid",
                        selectOptions: {
                            optionsApi: "GetL1Mtrltype",
                            valueMap: ["mtrltypeid", "mtrltype"],
                            extraOptions: [{mtrltypeid: -1, mtrltype: "全部"}]
                        },
                        disabled: true
                    },
                    {title: "建单开始日期", property: "begindate", type: "date"},
                    {title: "建单结束日期", property: "enddate", type: "date"},
                    {
                        title: "质检单号/委外订单号/订单相关号/送检单号",
                        property: "keyword"
                    }
                ],
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
            }
            var self = this;
            $lj.addEventListener("cpcheck_refresh", function () {
                self.$refs.baseWin.refresh();
            });
        },
        methods: {
            handleDataReset(response) {
                this.allData.length = 0;
                this.addList(response.cpcheckList);
                this.firstAuditCount = 0;
                this.secondAuditCount = 0;
                for (var i in this.allData){
                    if (this.allData[i].flag == 0){
                        this.firstAuditCount++;
                    }
                    if (this.allData[i].flag == 1 && this.allData[i].SecFlag == 0){
                        this.secondAuditCount++;
                    }
                };
            },
            handleDataLoaded(response) {
                let moreItems = response.cpcheckList;
                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) {
                    //选择界面
                } else {
                    $lj.openWin("cpcheck-ww-mx", {mainObject: mainObject, winmode: 1});
                }
            },
            handleQueryFieldClick({property}) {
                if (property === 'mtrltypeid') {
                    this.$refs.mtrltypeChooseDialog.open();
                }
            },
            handleMtrltypeChosen(item) {
                if (item && item.mtrltypeid) {
                    this.$refs.baseWin.setQueryParam("mtrltypeid", item.mtrltypeid);
                }
            }
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select";
            },
            title() {
                if (this.winMode === 'single-select') {
                    return "委外质检单-选择";
                } else {
                    return "委外质检单";
                }
            },
        }
    }
</script>