<template>
    <lj-win-index-common-list :title="title" api-name="GetL1Cpcheck"
                        :data-reset-callback="handleDataReset"
                        :data-loaded-callback="handleDataLoaded"
                        ref="baseWin"
                        hint="质检单号/采购订单号/订单相关号/送检单号"
                        ifShowSearchbar
                        :searchText="keyword"
                        v-on:update:searchText="emitChangeKeyword"
                        :query-quick="queryQuick"
                        :search-actions="searchActions"
                        :query-hard-parms="queryHardParms"
                        :addable="true"
                        @scan-click="handleSearchScan"
                        @add-click="handleAddClick"
                        :query-fields="queryFields">
        <div slot="body_list" class="body_list_normal">
            <!-- <div style="margin-left: 10px;">待审核:{{firstAuditCount}},待二审:{{secondAuditCount}} </div> -->
            <template v-for="(item, index) in allData">
                <cpcheck-item :item="item" @click.native="handleCellClick(item)" :key="index"/>
            </template>
            <l1-mtrltype-choose ref="mtrltypeChooseDialog" @mtrltype-chosen="handleMtrltypeChosen"/>
        </div>
    </lj-win-index-common-list>
</template>

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

    export default {
        components: {
            CpcheckItem,
            LjSearchbar,
            LjWinIndexCommonList,
            L1MtrltypeChoose
        },
        data() {
            return {
                firstAuditCount: 0,
                secondAuditCount: 0,
                winMode: "",
                keyword: "",
                allData: [],
                queryHardParms: { billtype: 0 },
                queryQuick: [
                    {
                        title: "分部",
                        property: "scid",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            optionsApi: "GetL1Scdef",
                            resultName: "scdefList",
                            valueMap: { text: 'scname', value: 'scid'},
                            extraOptions: [
                                {scid: -1, scname: "全部"}
                            ]
                        }
                    },
                    {
                        title: "状态",
                        property: "status",
                        type: 'radio3',
                        initValue: -1,
                        selectOptions: {
                            options: [
                                {status: -1, dscrp: "全部"},
                                {status: 0, dscrp: "待审核"},
                                {status: 1, dscrp: "待二审"},
                                {status: 3, dscrp: "已完成"}
                            ], 
                            valueMap: { text: 'dscrp', value: 'status'},
                        }
                    },
                    {
                        title: "全部类别",
                        property: "mtrltypeid",
                        type: 'cascader',
                        initValue: -1,
                        selectOptions: {
                            optionsApi: "GetL1MtrltypeTree",
                            requestbody: {RecurionType: 1},
                            resultName: "RecurionList",
                            valueMap: { text: 'text', value: 'value', children: 'children' }
                        }
                    },
                ],
                searchActions: [
                    { 
                        title: '全部',
                        property: 'keyword'
                    },
                    { 
                        title: '质检单号',
                        property: 'billcode'
                    },
                    { 
                        title: '采购订单号',
                        property: 'sptcheckRelcode'
                    },
                    { 
                        title: '订单相关号',
                        property: 'saletaskRelcode'
                    },
                    { 
                        title: '销售订单号',
                        property: 'saletaskcode'
                    },
                    { 
                        title: '指令单号',
                        property: 'ordercode'
                    }
                ],
                queryFields: [
                    {
                        title: "建单人",
                        property: "opemp",
                        initValue: "",
                        group: "建单人"
                    },
                    {
                        title: "建单开始日期",
                        property: "begindate",
                        type: "date",
                        group: "日期"
                    },
                    {
                        title: "建单结束日期",
                        property: "enddate",
                        type: "date",
                        group: "日期"
                    },
                ],
                queryFields: [
                    {
						title: '车间', property: 'wrkGrpid',
                        type: 'radio3',
                        initValue: -1,
						selectOptions: {
                            optionsApi: "GetScWkps",
                            resultName: "wkps",
                            valueMap: { text: 'wrkGrpName', value: 'wrkGrpid'},
                            extraOptions: [
                                {wrkGrpid: -1, wrkGrpName: "全部"}
                            ]
						}
					},
                    {title: "建单开始日期", property: "begindate", type: "date"},
                    {title: "建单结束日期", property: "enddate", type: "date"}
                ],
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
            }
            var self = this;
            $lj.addEventListener("cpcheck_refresh", function () {
                self.$refs.baseWin.refresh();
            });
        },
        methods: {
            /**回调绑定值 */
            emitChangeKeyword(val) {
                this.keyword = val;
                console.log('库存表 emitChangeKeyword this.keyword :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(this.keyword));
            },
            /**搜索栏-扫描 */
            handleSearchScan(searchState) {
                $lj.openScanner((ret) => {
                    setTimeout(() => {
                        let obj = {};
                        obj[searchState] = ret.content.trim();
                        this.$refs.baseWin.handleSearchbar(obj, true);
                    }, 200);
                })
            },
            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-zz-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);
                }
            },
            handleAddClick(){
                console.log(1)
                $lj.openWin("cpcheck-zz-mx", {ifadd: true, winmode: 1});
            }
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select";
            },
            title() {
                if (this.winMode === 'single-select') {
                    return "自制质检单-选择";
                } else {
                    return "自制质检单";
                }
            },
        }
    }
</script>