<template>
    <lj-win-index-common-list
        :title="title"
        dwname="web_bednet_define"
        :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
    >
        <div slot="body_list">
            <!-- <div class="marginTop-5px"></div> -->
            <item-widget v-for="(item, idx) in allData" :item="item" :key="idx" @callFuntion="handleCellClick(item)" :deptEnum="deptEnum" :bednettypeEnum="bednettypeEnum"></item-widget>
            <!-- <div class="lj-card-item marginTop-8px" v-for="(item, idx) in allData" @click="handleCellClick(item)"> -->
                <!-- <div>
                    <mu-row>
                        <mu-col span="3" class="lj-card-secondary-title">订单号码：</mu-col>
                        <mu-col span="5" class="lj-card-secondary-content">{{item.taskcode}}</mu-col>
                        <mu-col span="2">
                            <div v-if="item.consignedqty > 0" class="lj-alert-text">已接单</div>
                        </mu-col>
                        <mu-col span="2">
                            <lj-status-badge :value="item.statusstr"/>
                        </mu-col>
                        <mu-col span="3" class="lj-card-secondary-title">客户：</mu-col>
                        <mu-col span="9" class="lj-card-secondary-content">{{ isShowCusname(item) }}</mu-col>
                        <mu-col span="3" class="lj-card-secondary-title">业务员：</mu-col>
                        <mu-col span="9" class="lj-card-secondary-content">{{item.assign_emp}}</mu-col>
                        <mu-col span="3" class="lj-card-secondary-title">订货日期：</mu-col>
                        <mu-col span="3" class="lj-card-secondary-content">{{item.taskdate | dateFilter}}</mu-col>
                        <mu-col span="3" class="lj-card-secondary-title">交货日期：</mu-col>
                        <mu-col span="3" class="lj-card-secondary-content">{{item.requiredate | dateFilter}}</mu-col>
                        <mu-col span="3" class="lj-card-secondary-title" v-if="userFunids.indexOf(122) < 0">金额：</mu-col>
                        <mu-col span="3" class="lj-card-secondary-content lj-sharp-red-text" v-if="userFunids.indexOf(122) < 0">
                            {{item.moneysign}}{{item.msttakeamt}}
                        </mu-col>
                        <mu-col span="3" class="lj-card-secondary-title" v-if="userFunids.indexOf(122) < 0">订金：</mu-col>
                        <mu-col span="3" class="lj-card-secondary-content lj-sharp-red-text" v-if="userFunids.indexOf(122) < 0">
                            {{item.moneysign}}{{item.damt}}
                        </mu-col>
                    </mu-row>
                    <div class="lj-card-summary">
                        <div class="lj-card-summary-light-text">
                            <i class="iconfont icon-cart lj-card-summary-icon"></i>
                            <span class="lj-card-secondary-content">共{{item.sumqty}}件商品，其中包括</span>
                        </div>
                        <template v-if="item.mtrlstr_1">
                            <div class="lj-divider"></div>
                            <mu-row>
                                <mu-col span="1" style="text-align: right;">
                                    <li></li>
                                </mu-col>
                                <mu-col>
                                    <div class="lj-card-secondary-content"> 
                                        {{item.mtrlstr_1}}
                                    </div>
                                    <div class="lj-card-secondary-content lj-card-summary-light-text" v-if="userFunids.indexOf(122) < 0">
                                        {{item.qtystr_1}}
                                    </div>
                                </mu-col>
                            </mu-row>
                        </template>
                        <template v-if="item.mtrlstr_2">
                            <mu-row>
                                <mu-col span="1" style="text-align: right;">
                                    <li></li>
                                </mu-col>
                                <mu-col>
                                    <div class="lj-card-secondary-content">
                                        {{item.mtrlstr_2}}
                                    </div>
                                    <div class="lj-card-secondary-content lj-card-summary-light-text" v-if="userFunids.indexOf(122) < 0">
                                        {{item.qtystr_2}}
                                    </div>
                                </mu-col>
                            </mu-row>
                        </template>

                    </div>
                    <div class="lj-card-tag-text lj-card-summary-light-text flex">
                        <div class="lj-card-tag">
                            <i class="iconfont icon-grid lj-card-summary-icon"></i>
                            {{item.typename}}
                        </div>
                        <div class="lj-card-tag">
                            <i class="iconfont icon-cash lj-card-summary-icon"></i>
                            {{item.banktypestr}}
                        </div>
                    </div>
                    <van-steps
                        class="steps-list"
                        v-if="item.workplanCheckList && item.workplanCheckList.length"
                        :active="isActiveWorkplan(item.workplanCheckList)"
                        active-color="#4caf50"
                        inactive-color="#D1D1D6"
                    >
                        <van-step
                            v-for="(itm, idx) in item.workplanCheckList" :key="idx"
                        >
                            {{ itm.content }}
                        </van-step>
                    </van-steps>
                </div> -->
            <!-- </div> -->
        </div>
    <!-- </lj-win-index-common> -->
    </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: 'bednetcode',
                        initValue: "",
                        group: "单据"
                    },
                    {
                        title: '床网名称',
                        property: 'bednetname',
                        initValue: "",
                        group: "单据"
                    },
                ],
                queryQuick: [
                    {
                        title: "部门",
                        property: "arg_deptid",
                        type: 'radio3',
                        selectOptions: {
                            valueMap: { text: 'label', value: 'value'},
                        }
                    },
                    {
                        title: "类型",
                        property: "arg_bednettypeid",
                        type: 'radio1',
                        selectOptions: {
                            valueMap: { text: 'label', value: 'value'},
                        }
                    },
                ],
                searchActions: [
                    { 
                        title: '全部',
                        property: 'arg_search'
                    },
                    { 
                        title: '床网编码',
                        property: 'bednetcode' 
                    },
                    { 
                        title: '床网名称',
                        property: 'bednetname'
                    },
                ],
                keyword: '',
                deptEnum: [],
                bednettypeEnum: []
            }
        },
        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("bednet_quote_refresh", function () {
                self.$refs.baseWin.refresh();
            });
        },
        methods: {
            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 _bednettypeItem = data.tableinfo.columns.find(item => item.field == 'bednettypeid');
                if (_bednettypeItem && _bednettypeItem.enum) {
                    for (let i = 0; i < this.queryQuick.length; i++) {
                        if (this.queryQuick[i].property == 'arg_bednettypeid') {
                            this.$set(this.queryQuick[i].selectOptions, 'options', _bednettypeItem.enum);
                            this.bednettypeEnum = _bednettypeItem.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") {
                    let item = this.bednettypeEnum.find(item => item.value == mainObject.bednettypeid);
                    mainObject.typename = item.label;
                    $lj.sendEvent("bednet-selection", mainObject);
                    $lj.closeWin();
                } else {
                    $lj.openWin("bednet-quote-mx", {mainObject: mainObject});
                }
            },
            handleAddClick() {
                if (!$lj.checkPower(62)) {
                    alert("您没有床网报价建单权限，请检查");
                    return;
                }
                $lj.openWin("bednet-quote-new", {mainObject: {}, 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;
    }
    .steps-list {
        padding-top: 4px;
        .van-steps__items {
            margin-bottom: 0;
        }
    }
    .marginTop-5px {
        margin-top: 5px;
    }
    .marginTop-8px {
        margin-top: 8px;
    }
</style>