<template>
    <lj-win-index-common-list
        :title="title"
        dwname="web_mattress"
        :addable="true"
        :data-reset-callback="handleDataReset"
        :data-loaded-callback="handleDataLoaded"
        :query-hard-parms="queryHardParms"
        :query-fields="queryFields"
        @add-click="handleAddClick"
        ref="baseWin"
        hint="床垫编码/床垫名称/相关号"
        :searchText="keyword"
        v-on:update:searchText="emitChangeKeyword"
        :query-quick="queryQuick"
        :search-actions="searchActions"
        @scan-click="handleSearchScan"
        ifShowSearchbar
        :before-post="beforePost"
    >
        <div slot="body_list">
            <item-widget v-for="(item, idx) in allData" :item="item" :key="idx" @callFuntion="handleCellClick(item)" :deptEnum="deptEnum" :mattresstypeEnum="mattresstypeEnum"></item-widget>
        </div>
    </lj-win-index-common-list>
</template>

<script>
    import LjSearchbar from "components/lj-searchbar";
    import LjStatusBadge from "components/lj-status-badge";
    import LjWinIndexCommonList from "components/lj-win-index-common-list";
    import ItemWidget from "./item";
    // import LjText from "./lj-text"

    export default {
        name: "mattress-quote",
        components: {
            LjStatusBadge,
            LjSearchbar,
            LjWinIndexCommonList,
            ItemWidget,
            // LjText
        },
        data() {
            return {
                bindingParms: {keyword: ""},
                allData: [],
                userFunids: [],
                queryHardParms: {},
                // queryFields: [
                //     {title: "客户", property: "keyword"},
                //     {title: "下单日期/要求交期开始", property: "begindate", type: "date"},
                //     {title: "下单日期/要求交期结束", property: "enddate", type: "date"}
                // ],
                queryFields: [
                    {
                        title: '床垫编码',
                        property: 'mattresscode',
                        initValue: "",
                        group: "单据"
                    },
                    {
                        title: '床垫名称',
                        property: 'mattressname',
                        initValue: "",
                        group: "单据"
                    },
                ],
                queryQuick: [
                    {
                        title: "部门",
                        property: "arg_deptid",
                        type: 'radio3',
                        selectOptions: {
                            valueMap: { text: 'label', value: 'value'},
                        }
                    },
                    {
                        title: "类型",
                        property: "arg_mattresstypeid",
                        type: 'radio2',
                        selectOptions: {
                            valueMap: { text: 'label', value: 'value'},
                        }
                    },
                    {
                        title: "状态",
                        property: "status_flag",
                        type: 'radio1',
                        selectOptions: {
                            valueMap: { text: 'text', value: 'value'},
                            options: [
                                {
                                    value: 1,
                                    text: "待下单"
                                },
                                {
                                    value: 2,
                                    text: "已下单"
                                },
                                {
                                    value: 3,
                                    text: "待财务审核"
                                },
                                {
                                    value: 4,
                                    text: "已财务审核"
                                }
                            ]
                        }
                    },
                ],
                searchActions: [
                    { 
                        title: '全部',
                        property: 'arg_search'
                    },
                    { 
                        title: '床垫编码',
                        property: 'mattresscode' 
                    },
                    { 
                        title: '床垫名称',
                        property: 'mattressname'
                    },
                ],
                keyword: '',
                deptEnum: [],
                mattresstypeEnum: []
            }
        },
        created() {
            this.userFunids = $lj.getStorage('user').rsltFunids;
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
                this.queryHardParms = pageParam.queryParams || {};
            }
            var self = this;
            $lj.addEventListener("mattress_quote_refresh", function () {
                self.$refs.baseWin.refresh();
            });
        },
        methods: {
            beforePost(params) {
                console.log("beforePost params ::>>> ", JSON.stringify(params));
                switch (params.status_flag) {
                    case 1: // 待下单
                        params.arg_xd_flag = 0;
                        break;
                    case 2: // 已下单
                        params.arg_xd_flag = 1;
                        break;
                    case 3: // 待财务审核
                        params.arg_cp_flag = 1;
                        params.arg_flag = 0;
                        break;
                    case 4: // 已财务审核
                        params.arg_flag = 1;
                        break;
                    default:
                        break;
                }
                console.log("beforePost params afff::>>> ", JSON.stringify(params));
                return params
            },
            getType(data) {
                let _deptidItem = data.tableinfo.columns.find(item => item.field == 'deptid');
                console.log("_deptidItem.enum: ", _deptidItem.enum)
                if (_deptidItem && _deptidItem.enum) {
                    for (let i = 0; i < this.queryQuick.length; i++) {
                        if (this.queryQuick[i].property == 'arg_deptid') {
                            this.$set(this.queryQuick[i].selectOptions, 'options', _deptidItem.enum);
                            this.deptEnum = _deptidItem.enum;
                            break;
                        }
                    }
                }

                let _mattresstypeItem = data.tableinfo.columns.find(item => item.field == 'mattresstypeid');
                if (_mattresstypeItem && _mattresstypeItem.enum) {
                    for (let i = 0; i < this.queryQuick.length; i++) {
                        if (this.queryQuick[i].property == 'arg_mattresstypeid') {
                            this.$set(this.queryQuick[i].selectOptions, 'options', _mattresstypeItem.enum);
                            this.mattresstypeEnum = _mattresstypeItem.enum;
                            break;
                        }
                    }
                }
            },
            /**搜索栏-扫描 */
            handleSearchScan(searchState) {
                $lj.openScanner((ret) => {
                    setTimeout(() => {
                        let obj = {};
                        obj[searchState] = ret.content.trim();
                        this.$refs.baseWin.handleSearchbar(obj, true);
                    }, 200);
                })
            },
            /**回调绑定值 */
            emitChangeKeyword(val) {
                this.keyword = val;
            },
            handleDataReset(response) {
                this.allData = [];
                this.getType(response);
                if (response.datatable && response.datatable.length > 0) {
                    this.addList(response.datatable);
                }
            },
            handleDataLoaded(response) {
                console.log("handleDataLoaded response:: " , response)
                this.getType(response);
                let moreSaletasks = response.datatable;
                if (moreSaletasks.length > 0) {
                    this.addList(moreSaletasks);
                    return true;
                }
                return false;
            },
            addList(list) {
                this.allData = this.allData.concat(list);
            },
            handleSearchbar(text) {
                this.$refs.baseWin.refresh(Object.assign({keyword: text}, this.queryHardParms));
            },
            handleCellClick(mainObject) {
                // if (this.winMode === "single-select") {
                //     $lj.sendEvent("saletask_select", mainObject);
                //     $lj.closeWin();
                // } else {
                    $lj.openWin("mattress-quote-mx", {mainObject: mainObject});
                // }
            },
            handleAddClick() {
                if (!$lj.checkPower(72)) {
                    alert("您没有床垫报价建单权限，请检查");
                    return;
                }
                $lj.openWin("mattress-quote-new", {mainObject: {mattressid: 0}, winType: 0});
            }
        },
        computed: {
            title() {
                if (this.winMode === 'single-select') {
                    return "床垫报价选择";
                } else {
                    return "床垫报价";
                }
            },
            isActiveWorkplan(list) {
                return function(list) {
                    let active = -1;
                    for (const item of list) {
                        if (item.sql_rslt) {
                            active++;
                        } else {
                            break;
                        }
                    }
                    return active
                }
            },
            ifSummary(){
                 if ($lj.checkPower(122) && $lj.getStorage('user').empid != 0) {
                        return false;  
                 }
                 else{
                     return true;
                 }
            },
            isShowCusname(data) {
                let empid = $lj.getStorage("user").empid;
                return function (data) {
                    let msg = "";
                    if (empid != 0 && $lj.checkPower(1210)) {
                        msg = data.cuscode;
                    } else {
                        msg = data.cusname;
                    }
                    return msg;
                }
            },
        }
    }
</script>
<style lang="less">
    .lj-alert-text {
        color: @colorAlert;
    }
    .lj-primary-text {
        color: @colorPass;
    }
    .steps-list {
        padding-top: 4px;
        .van-steps__items {
            margin-bottom: 0;
        }
    }
    .marginTop-5px {
        margin-top: 5px;
    }
    .marginTop-8px {
        margin-top: 8px;
    }
</style>