<template>
    <lj-win-common :title="title" :show-back="true" :closeWinHandler="handleCloseWin">
        <template slot="header_right">
            <div @click="handleRefreshSaleTaskMx">
                <mu-button icon>
                    <i class="iconfont icon-ic_refresh" />
                </mu-button>
            </div>
        </template>
        <div slot="body">
            <div class="cust">
                <van-cell-group inset title="客户信息">
                    <van-cell center is-link @click="handleSingleSelectCust">
                        <template #title>
                            <span class="cusName">{{custName}}</span>
                        </template>
                        <template #icon>
                            <div class="icon-box">
                                <i class="iconfont icon-Profile" />
                            </div>
                        </template>
                        <template #label v-if="saletask.cuscode">
                            <!-- <span class="repName" style="color:#000;">{{saletask.rel_rep}}</span> -->
                            <!-- <span class="tele">{{saletask.tele}}</span> -->
                            <span class="tele">{{saletask.cuscode}}</span>
                        </template>
                    </van-cell>
                    <van-field 
                        v-for="(item,index) in clientinfo" :key="index" 
                        v-model="saletask[item.key]" 
                        :class="item.className" 
                        :placeholder="item.placeholder" 
                        :label="item.label" 
                        @focus="item.isLink ? null : handleSelectValue($event)" 
                        :is-link="item.isLink" 
                        :readonly="item.readonly"
                        :type="item.key == 'cus_address' || item.key == 'station_address' ? 'textarea' : 'text'"
                        :autosize="item.key == 'cus_address' || item.key == 'station_address' ? { maxHeight: 132, minHeight: 44 } : false"
                        @click="item.isLink ? item.clickEvent() : null"
                    >
                        <template #right-icon>
                            <span v-if="item.isHistory" @click="item.clickEvent() || null">
                                <i class="iconfont icon-xiangqing field-icon" />
                            </span>
                        </template>
                    </van-field>
                </van-cell-group>
            </div>
            <!-- <van-divider>订单明细</van-divider> -->
            <div class="saletaskmx-info">
                <div class="addgoods">
                    <van-button block @click="handleAddGoodsToCart">
                        <template #icon>
                            <i class="iconfont icon-add"/>
                        </template>
                        <template>
                            <span class="add-goods-text">添加产品</span>
                        </template>
                    </van-button>
                </div>
            </div>
            <div class="saletask-mx">
                <!-- <saletask-mx-item 
                    :saletask-mx="saletask.mxList" 
                    @plus-goods="handleSaletaskMxPlus" 
                    @minus-goods="handleSaletaskMxMinus" 
                    @delete-goods="handleSaletaskMxDelete"
                    @modify-goods="handleSaletaskMxModify"
                ></saletask-mx-item> -->

                <div class="saletask-mx-container">
                    <div class="container-header" v-if="saletask.mxList.length">
                        <van-cell-group title="销售订单明细">
                            <!-- <van-cell title="销售订单明细">
                                <template #icon>
                                    <i class="iconfont icon-huowu icon-16" />
                                </template>
                            </van-cell> -->
                            <van-swipe-cell v-for="(mx,index) in saletask.mxList" :key="index">
                                <saletask-mx-item 
                                    :mx="mx"
                                    :index="index"
                                    @plus-goods="handleSaletaskMxPlus" 
                                    @minus-goods="handleSaletaskMxMinus" 
                                    @select-date="handleSaletaskSelDate" 
                                    @change="fnCalculatePrice" 
                                ></saletask-mx-item>
                                <template #right>
                                    <div class="flex" style="height: 100%;">
                                        <van-button block text="修改" type="primary"  style="height: 100%;" @click="handleSaletaskMxModify(mx,index)"/>
                                        <van-button block text="删除" type="danger" style="height: 100%;" @click="handleSaletaskMxDelete(mx,index)"/>
                                    </div>
                                </template>
                            </van-swipe-cell>
                        </van-cell-group>
                    </div>
                </div>
            </div>
            <!-- <van-divider>订单信息</van-divider> -->
            <div class="saletask-info">
                <van-cell-group inset>
                    <van-field 
                        v-for="(item,index) in saletaskInfoList" :key="index" 
                        v-model="saletask[item.key]" 
                        :class="item.className" 
                        :placeholder="item.placeholder" 
                        :label="item.label" 
                        @focus="item.isLink ? null : handleSelectValue($event)" 
                        :is-link="item.isLink" 
                        :readonly="item.readonly" 
                        @click="item.isLink ? item.clickEvent() : null"
                    >
                        <template #right-icon>
                            <span v-if="item.isHistory" @click="item.clickEvent() || null">
                                <i class="iconfont icon-xiangqing field-icon" />
                            </span>
                        </template>
                    </van-field>
                    <!-- <van-field v-model="saletask.taskdate" label="订货日期" placeholder="请输入交货日期" class="lj-field-required" is-link input-align="right" @click="datePickerShow = true" readonly/>
                    <van-field v-model="saletask.assign_emp" label="交货方式" placeholder="请输入业务员" class="lj-field-required" input-align="right">
                        <template #right-icon>
                            <span><i class="iconfont icon-xiangqing field-icon" /></span>
                        </template>
                    </van-field>
                    <van-field v-model="saletask.assign_emp" label="业务员" placeholder="请输入业务员" class="lj-field-required" input-align="right"/>
                    <van-field v-model="saletask.cus_address" label="收货地址" placeholder="请输入收货地址" input-align="right"/>
                    <van-field v-model="saletask.rel_rep" label="联系人" placeholder="请输入联系人" input-align="right"/>
                    <van-field v-model="saletask.cus_tele" label="联系电话" placeholder="请输入联系电话" input-align="right"/>
                    <van-field v-model="saletask.dscrp" label="备注" placeholder="请输入备注" input-align="right"/> -->
                </van-cell-group>
                <div class="tag-main">
                    <van-row type="flex" justify="end">
                        <van-col v-for="(item,index) in tagColumn" :key="index" :style="{'padding-right':item.offset}">
                            <van-popover v-model="item.popoverVisiable" trigger="click" placement="bottom-end" :actions="item.action" @select="handleTagPopoverSelect($event,item)">
                                <template #reference>
                                <van-tag round text-color="#000" color="#fff" size="large"><i :class="['iconfont',item.icon]" />
                                    {{ ActionVal(item.key) }}
                                </van-tag>
                                </template>
                            </van-popover>
                        </van-col>
                    </van-row>
                </div>
            </div>
            <div class="saletask-info">
                <van-cell-group inset title="单据相关信息">
                    <van-field 
                        v-for="(item,index) in salesmanInfo" :key="index" 
                        v-model="saletask[item.key]" 
                        :class="item.className" 
                        :placeholder="item.placeholder" 
                        :label="item.label" 
                        @focus="item.isLink ? null : handleSelectValue($event)" 
                        :is-link="item.isLink" 
                        :readonly="item.readonly" 
                        @click="item.isLink ? item.clickEvent() : null"
                    >
                        <template #right-icon>
                            <span v-if="item.isHistory" @click="item.clickEvent() || null">
                                <i class="iconfont icon-xiangqing field-icon" />
                            </span>
                        </template>
                    </van-field>
                </van-cell-group>
            </div>

            <!-- <van-popup v-model="datePickerShow"  round position="bottom"> -->
                <!-- <van-datetime-picker
                    v-model="currentDate"
                    type="date"
                    title="选择年月日"
                    @confirm="handleDateTimePicker"
                    @cancel="datePickerShow = false"
                /> -->
                <van-calendar
                    v-model="datePickerShow"
                    title="日期选择"
                    :show-confirm="false"
                    :min-date="new Date(new Date().setMonth(new Date().getMonth() - 6))"
                    @confirm="handleDateTimePicker"
                    @close="datePickerShow = false"
                />
            <!-- </van-popup> -->
            <van-popup v-model="infopickerShow"  round position="bottom">
                <van-picker
                    :title="pickerTitle"
                    show-toolbar
                    :columns="pickerDataList"
                    @confirm="handlePickerDataConfirm"
                    @cancel="infopickerShow = false"
                />
            </van-popup>
            <lj-mtrldef-shopping
                ref="ljMtrldefShopping"
                :cusid="saletask.cusid"
                :moneyid="saletask.moneyid"
                :typeid="saletask.typeid"
                :targetName="editItem.mtrlname"
                :targetCode="editItem.mtrlcode"
                confirm-text="确认修改"
                @confirm="handleConfirmToEditMx"
            >
            </lj-mtrldef-shopping>
        </div>
        <template slot="footer">
            <div class="saletask-footer">
                <!-- <div>合计:<span class="price-sum">¥{{totalPrice|moneyFilter}}</span></div> -->
                <div class="total flex-1 flex-column">
                    <div class="price">
                        合计:
                        <span class="num">
                            ￥
                            <span class="p">{{ totalPrice | moneyFilter }}</span>
                        </span>
                    </div>
                    <div class="other" v-if="discountPrice">
                        <span v-if="saletask.mxList.length">共{{saletask.mxList.length}}件，</span>
                        <span class="subtitle">共减{{ discountPrice }}元</span>
                    </div>
                </div>
                <div class="goods-button">
                    <mu-button v-no-more-click large color="primary" @click="handleSaveSaleTask">
                        <svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-tongyi"></use>
                        </svg>保存订单
                    </mu-button>
                </div>
            </div>
            <!-- <div class="saletask-footer-wrapper">
                <div class="total flex-1 flex-column">
                    <div class="price">
                        <span v-if="saletask.mxList.length">共{{saletask.mxList.length}}件，</span>
                        合计:
                        <span class="num">
                            ￥
                            <span class="p">{{ totalPrice | moneyFilter }}</span>
                        </span>
                    </div>
                    <div class="other" v-if="discountPrice">
                        <span class="subtitle">共减{{ discountPrice }}元</span>
                    </div>
                </div>
                <van-button class="confirm-btn" round block @click="handleSaveSaleTask">
                    保存订单
                </van-button>
            </div> -->
        </template>
    </lj-win-common>
</template>

<script>
    import LjWinCommon from "components/lj-win-common";
    import SaletaskMxItem from "./saletask-edit-mx-item";
    import LjMtrldefShopping from '../../product-index-sale/lj-mtrldef-shopping.vue';

    export default {
        components: {
            LjWinCommon,
            SaletaskMxItem,
            LjMtrldefShopping
        },
        data() {
            return {
                cust: {},
                datePickerShow: false,
                currentDate: new Date(),
                saletask: {
                    mxList: []
                },
                outrepList: [],
                paytypeList: [],
                banktypeList: [],
                tagColumn: this.handleTagColumn(),
                moneyAction: [],
                typeAction: [],
                saleatskDraft:"saletask-edit-product-draft",
                winMode: 0,
                saletaskInfoList: [
                    {label:"交货方式",key:'paytype',placeholder:"请输入交货方式",className:"lj-field-required",readonly:false,isHistory:true,clickEvent:this.handleClickPayType},
                    {label:"交货日期",key:'requiredate',placeholder:"可选，选择交货日期",className:"lj-field-required",readonly:true,isLink:true,clickEvent:() => { this.datePickerShow = true; this.dateTag = 'requiredate' }},
                    {label:"结算方式",key:'banktypename',placeholder:"请输入结算方式",className:"lj-field-required",readonly:true,isLink:true,clickEvent:this.handleClickBankType},
                    {label:"备注",key:'dscrp',placeholder:"请输入备注",className:"lj-field-required",readonly:false},
                ],
                clientinfo: [
                    {label:"联系人",key:'rel_rep',placeholder:"请输入联系人",className:"lj-field-required",readonly:false},
                    {label:"联系电话",key:'cus_tele',placeholder:"请输入联系电话",className:"lj-field-required",readonly:false},
                    {label:"收货地址",key:'cus_address',placeholder:"请输入收货地址",className:"lj-field-required",readonly:false},
                    {label:"货运部地址",key:'station_address',placeholder:"请输入货运部地址",className:"lj-field-required",readonly:false},
                ],
                salesmanInfo: [
                    {label:"订货日期",key:'taskdate',placeholder:"请输入订货日期",className:"lj-field-required",readonly:true,isLink:true,clickEvent:() => { this.datePickerShow = true; this.dateTag = 'taskdate' }},
                    {label:"业务员",key:'assign_emp',placeholder:"请输入业务员",className:"lj-field-required",readonly:true,isLink:true,clickEvent:this.handleClickOutRep},
                ],
                pickerDataList: [],
                pickerTitle: "",
                infopickerShow: false,
                pickerType: '',
                habit:['cusname','cus_address','station_address','cus_tele','cusid','rel_rep','assign_emp','banktypeid','banktypename','moneyid','typeid','dscrp','paytype'],
                editItem: {},
                editItemIndex: 0,
                dateTag: '', // 日期选择目标值
                dateMxIndex: -1, // 选择明细的交货日期时的索引
                totalPrice: 0, // 合计总数
                discountPrice: 0, // 合计优惠
            }
        },
        created() {
            let self = this;

            let pageParam = $lj.getPageParam();
            if (pageParam.mainObject) {
                this.winMode = pageParam.winMode;
                this.saletask = pageParam.mainObject;
            }

            if(this.isModify) {
                this.handleSaveProductsDraft();
            }

            if (!this.saletask.hasOwnProperty('requiredate') || !this.saletask.requiredate) {
                // 赋值交货日期
                this.saletask.requiredate = $lj.dateformat(new Date(),'yyyy-MM-dd')
            }

            this.handleRefreshSaleTaskMx();

            $lj.removeEventListener("saletask_edit_refresh");
            $lj.addEventListener("saletask_edit_refresh",function(rsp){
                self.handleRefreshSaleTaskMx()
            })
;
            $lj.removeEventListener("saletask_edit_add");
            $lj.addEventListener("saletask_edit_add",function(rsp){
                self.handleAddSaleTaskMx(rsp);
            })

            // 固定返回，
            api.addEventListener({
                name: 'keyback'
            }, function(ret, err) {
                $lj.confirm('提示', '是否放弃保存？', function () {
                    self.handleClearSaleTaskMx();
                    $lj.closeWin("product-index-sale");
                    $lj.closeWin();
                });
            });
        },
        methods: {
            /**计算价格 */
            fnCalculatePrice() {
                if(!this.saletask.mxList) {
                    this.totalPrice = 0;
                    this.discountPrice = 0;
                };
                let totalSumAll = 0;
                let totalSumDis = 0;
                this.saletask.mxList.map((item) => {
                    if(!isNaN(item.enprice)) {
                        // totalSumAll += (item.enprice * item.rebate * item.saleqty)
                        let mul = $lj.floatMulMore([item.enprice, item.rebate, item.saleqty]);
                        totalSumAll = $lj.floatAdd(totalSumAll, mul);

                        let disPrice = $lj.floatMul(item.enprice, item.rebate);
                        let allDis = $lj.floatSub(item.enprice, disPrice);
                        if (allDis < 0) {
                            return;
                        }
                        totalSumDis = $lj.floatAdd(totalSumDis, ($lj.floatMul(allDis , item.saleqty) *100/100).toFixed(2))
                    }
                });
                this.totalPrice = isNaN(totalSumAll) ? 0 : totalSumAll;
                this.discountPrice = isNaN(totalSumDis) ? 0 : totalSumDis;
            },
            /**选择明细的交货日期 */
            handleSaletaskSelDate(mx, index) {
                this.datePickerShow = true;
                this.dateMxIndex = index;
            },
            handleConfirmToEditMx(item) {
                console.log('bf this.saletask :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(this.saletask));
                let mxList = this.saletask.mxList;
                mxList[this.editItemIndex] = item;
                this.$set(this.saletask.mxList, mxList);
                this.$nextTick(() => {
                    // this.$forceUpdate();
                    this.fnCalculatePrice();
                    console.log('af this.saletask :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(this.saletask));
                    setTimeout(() => {
                        this.editItem = {};
                        this.editItemIndex = -1;
                    }, 200);
                })
            },
            handleClickOutRep(){
                this.pickerTitle = "选择业务员"
                this.pickerType = 'outrep';
                this.pickerDataList = this.outrepList;
                this.infopickerShow = true;
            },
            handleClickPayType() {
                this.pickerTitle = "选择交货方式"
                this.pickerType = 'paytype'
                this.pickerDataList = this.paytypeList;
                this.infopickerShow = true;
            },
            handleClickBankType() {
                this.pickerTitle = "选择结算方式"
                this.pickerType = 'banktype'
                this.pickerDataList = this.banktypeList;
                this.infopickerShow = true;
            },
            handlePickerDataConfirm(value) {
                if(this.pickerType == '') return;
                this.infopickerShow = false;
                switch(this.pickerType){
                    case 'outrep':
                        this.$set(this.saletask, "assign_emp",value);
                        break;
                    case 'paytype':
                        this.$set(this.saletask, "paytype",value);
                        break;
                    case 'banktype':
                        this.$set(this.saletask, "banktypeid",value.banktypeid);
                        this.$set(this.saletask, "banktypename",value.text);
                        break;
                }
            },
            handleSelectValue(e) {
                e.currentTarget.select();
            },
            handleSaveProductsDraft(){
                $lj.setStorage(this.saleatskDraft,this.saletask);
            },
            handleGetProductsDraft(){
                let task = $lj.getStorage(this.saleatskDraft);
                if(!task) {
                    this.handleSaveProductsDraft();
                    this.handleGetProductsDraft();
                    return;
                }

                let self = this;
                Object.keys(task).map(key=>{self.$set(self.saletask,key,task[key])})
            },
            handleTagColumn(){
                let col = [];
                this.$nextTick(() => {
                    col.push({'key':'moneyid','label':"币种",'icon':"icon-gongxuguanli",'iconsize':"12px",'offset':"2px",'action':this.moneyAction,'popoverVisiable':false});
                    col.push({'key':'typeid','label':"单据分类",'icon':"icon-gongxuguanli",'iconsize':"12px",'offset':"2px",'action':this.typeAction,'popoverVisiable':false});
                })
                return col;
            },
            handleTagPopoverSelect(action,item){
                switch(item.key){
                    case 'moneyid':
                        this.$set(this.saletask,'moneyid',action.moneyid);
                        break;
                    case 'typeid':
                        this.$set(this.saletask,'typeid',action.typeid);
                        break;
                    default:
                        break;
                }
                
            },
            handleInitSaleTaskInfo(){
                let self = this;

                if(this.isInitMoneyAndType) {
                    this.handleGetProductsDraft();
                }else{
                    $lj.postLJRequest("GetSaleTaskEditInfo", {
                        token: $lj.getStorage("token")
                    }, function (ret) {
                        ret.moneyList.forEach((item) => {
                            self.moneyAction.push({text:item.name,moneyid:item.moneyid,rate:item.rate})
                        })
                        ret.typeList.forEach((item) => {
                            self.typeAction.push({text:item.typename,typeid:item.typeid})
                        })
                        ret.banktypeList.forEach((item) => {
                            self.banktypeList.push({text:item.banktypename,banktypeid:item.banktypeid})
                        })
                        ret.outrepList.forEach((item) => {
                            self.outrepList.push(item.outrepname);
                        })
                        ret.paytypeList.forEach((item) => {
                            self.paytypeList.push(item);
                        })

                        // 默认取值
                        if(!self.isModify) {
                            if(!self.saletask.moneyid){
                                self.$set(self.saletask,'moneyid',self.moneyAction[0].moneyid);
                            }
                            if(!self.saletask.typeid){
                                self.$set(self.saletask,'typeid',self.typeAction[0].typeid);
                            }
                            if(!self.saletask.paytype){
                                self.$set(self.saletask,'paytype',self.paytypeList[0]);
                            }
                        }

                        self.handleGetProductsDraft();

                        if(!self.saletask.taskdate) {
                            self.saletask.taskdate = $lj.dateformat(self.currentDate,'yyyy-MM-dd');
                        }
                    })
                }
            },
            handleRefreshSaleTaskMx() {
                // this.saletask.mxList = [];
                this.$set(this.saletask.mxList, []);
                this.$nextTick(() => {
                    this.handleInitSaleTaskInfo();
                    this.fnCalculatePrice();
                })
            },
            handleSingleSelectCust() {
                let self = this;
                $lj.removeEventListener("cust_select");
                $lj.addEventListener("cust_select", function (ret) {
                    if (ret.value) {
                        let custInfo = ret.value;
                        if (custInfo.cusid) {
                            self.$set(self.saletask, "cusname", custInfo.name);
                            self.$set(self.saletask, "cus_address", custInfo.address);
                            self.$set(self.saletask, "rel_rep", custInfo.rep);
                            self.$set(self.saletask, "cus_tele", custInfo.tele);
                            self.$set(self.saletask, "cuscode", custInfo.cuscode);
                            self.$set(self.saletask, "cusid", custInfo.cusid);
                            self.$set(self.saletask, "assign_emp",custInfo.custype);
                            self.$set(self.saletask, "banktypeid",custInfo.banktypeid);
                            self.$set(self.saletask, "station_address",custInfo.stat_address);
                        }

                        if(custInfo.moneyid && custInfo.moneyid > 0){
                            self.$set(self.saletask, "moneyid",custInfo.moneyid);
                        }

                        self.$set(self.saletask,"banktypename",self.bankTypeVal)
                    }
                });
                $lj.openWin("cust", {
                    winMode: "single-select",
                    addable: false
                });
            },
            handleDateTimePicker(value) {
                let date = $lj.dateformat(value,'yyyy-MM-dd');
                if (this.dateMxIndex == -1) {
                    this.saletask[this.dateTag] = date;
                } else {
                    this.saletask.mxList[this.dateMxIndex].requiredate = date;
                    this.dateMxIndex = -1;
                }
                this.datePickerShow = false;
                this.dateTag = '';
            },
            handleAddGoodsToCart() {
                if(this.saletask.moneyid == 0){
                    $lj.toast("请先选择币种")
                    return;
                }

                if(!this.saletask.cusid || this.saletask.cusid == 0){
                    $lj.toast("请先选择客户");
                    this.handleSingleSelectCust();
                    return;
                }

                // 添加产品,保存订单信息
                this.handleSaveProductsDraft();

                let self = this;
                $lj.openWin("product-index-sale", {
                    cusid: self.saletask.cusid,
                    moneyid: self.saletask.moneyid,
                    typeid: self.saletask.typeid
                });
            },
            handleSaveSaleTask() {
                if (!$lj.checkPower(7112)) {
                    alert("您没有销售订单建单权限，请检查");
                    return;
                }
                
                let self = this;

                if(this.saletask.mxList.length <= 0) {
                    $lj.toast("请先添加商品");
                    return;
                }

                if(!this.saletask.assign_emp || this.saletask.assign_emp.length < 0){
                    $lj.toast("请先填写业务员");
                    return;
                }

                let requestBody = {
                    token: $lj.getStorage("token"),
                    saletask: this.saletask
                };
                $lj.postLJRequest('SaveL1APPSaleTask', requestBody, function (ret) {
                    if (ret) {
                        $lj.toast("销售订单保存成功!");
                        /**储存数据 */
                        self.handleClearSaleTaskMx();

                        if(self.isModify) {
                            $lj.sendEvent("saletask_mx_refresh");
                        }else{
                            $lj.sendEvent("cart_refresh");
                        }
                        $lj.closeWin("product-index-sale");
                        $lj.closeWin();
                    }
                })
            },
            handleSaletaskMxPlus(item) {
                item.saleqty++;
                this.handleSaveProductsDraft();
            },
            handleSaletaskMxMinus(item) {
                if(item.saleqty <= 1) return;
                item.saleqty--;
                this.handleSaveProductsDraft();
            },
            handleSaletaskMxDelete(item,index) {
                let self = this;
                $lj.confirm('提示', '是否删除该商品', function() {
                    self.saletask.mxList.splice(index,1);
                    self.fnCalculatePrice();
                    self.handleSaveProductsDraft();
                })
            },
            handleSaletaskMxModify(item,index) {
                // item.index = index;
                // $lj.openWin("product-shopping", {
                //     winMode: 1,
                //     mainObject: item,
                //     cusid: this.saletask.cusid,
                //     moneyid: this.saletask.moneyid,
                //     typeid: this.saletask.typeid
                // });
                console.log('handleSaletaskMxModify item :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(item));
                console.log('handleSaletaskMxModify index :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(index));
                this.editItem = item;
                this.editItemIndex = index;
                this.$nextTick(() => {
                    this.$refs.ljMtrldefShopping.open(item);
                    this.$refs.ljMtrldefShopping.fnSetTaxPrice();
                })
            },
            handleClearSaleTaskMx() {
                let self = this;
                var emptyTask = {};
                // console.log("saletask>>>>>>>>>>",JSON.stringify(self.saletask))
                // this.habit.forEach(item => {
                //     if(self.saletask[item]) emptyTask[item] = self.saletask[item];
                // })
                this.saletask = emptyTask;
                this.handleSaveProductsDraft()
            },
            handleAddSaleTaskMx(rsp) {
                // 添加到购物车
                console.log('handleAddSaleTaskMx rsp :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(rsp));
                this.saletask.mxList.push(rsp.value)
                this.handleSaveProductsDraft()
                this.fnCalculatePrice();
            },
            handleCloseWin(){
                let self = this;
                $lj.confirm('提示', '是否放弃保存？', function () {
                    self.handleClearSaleTaskMx();
                    $lj.closeWin("product-index-sale");
                    $lj.closeWin();
                });
            }
        },
        computed: {
            isModify() {
                return this.winMode == 1;
            },
            title() {
                if (this.isModify) {
                    return "修改销售订单";
                } else {
                    return "新建销售订单";
                }
            },
            repName() {
                return this.saletask.rep || "请选择客户";
            },
            custName() {
                return this.saletask.cusname || "请选择客户";
            },
            // totalPrice() {
            //     if(!this.saletask.mxList) return 0;
            //     let totalSumAll = 0;
            //     this.saletask.mxList.map((item) => {
            //         if(!isNaN(item.enprice)) {
            //             // totalSumAll += (item.enprice * item.rebate * item.saleqty)
            //             let mul = $lj.floatMulMore([item.enprice, item.rebate, item.saleqty]);
            //             totalSumAll = $lj.floatAdd(totalSumAll, mul);
            //         }
            //     });
            //     if(isNaN(totalSumAll)){
            //         return 0
            //     }
            //     return totalSumAll;
            // },
            // discountPrice() {
            //     if(!this.saletask.mxList) return 0;
            //     let totalSumAll = 0;
            //     this.saletask.mxList.map((item) => {
            //         if (!isNaN(item.enprice)) {
            //             let disPrice = $lj.floatMul(item.enprice, item.rebate);
            //             let allDis = $lj.floatSub(item.enprice, disPrice);
            //             if (allDis < 0) {
            //                 return;
            //             }
            //             totalSumAll = $lj.floatAdd(totalSumAll, ($lj.floatMul(allDis , item.saleqty) *100/100).toFixed(2))
            //         }
            //     });
            //     if(isNaN(totalSumAll)){
            //         return 0
            //     }
            //     return totalSumAll;
            // },
            moneyTypeVal() {
                for (let i = 0; i < this.moneyAction.length; i++) {
                    if (this.moneyAction[i].moneyid == this.saletask.moneyid) {
                        return this.moneyAction[i].text;
                    }
                }
                return '币种';
            },
            typeTypeVal() {
                for (let i = 0; i < this.typeAction.length; i++) {
                    if (this.typeAction[i].typeid == this.saletask.typeid) {
                        return this.typeAction[i].text;
                    }
                }
                return '单据类型';
            },
            bankTypeVal() {
                for (let i = 0; i < this.banktypeList.length; i++) {
                    if (this.banktypeList[i].banktypeid == this.saletask.banktypeid) {
                        return this.banktypeList[i].text;
                    }
                }
                return '结算方式';
            },
            ActionVal() {
                return (key) => {
                    if(key == 'moneyid') return this.moneyTypeVal;
                    else if(key == 'typeid') return this.typeTypeVal;
                    else return '无'
                }
            },
            isInitMoneyAndType() {
                return this.moneyAction.length > 0 && this.typeAction.length > 0;
            }
        }
    }
</script>
<style lang="less">
@import "~assets/css/lj-pda.less";

.van-cell-group--inset {
    margin: 0px;
}
.van-divider{
    margin: 10px;
}
.cust {
    margin-bottom: 8px;
    .cusName {
        font-size: 16px;
        font-weight: 400;
    }
    .repName {
        font-size: 14px;
        font-weight: 400;
        margin-right: 10px;
    }
    .tele {
        font-size: 12px;
    }
    .icon-box {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-right: 5px;
        width: 46px;
        height: 46px;
        background-color: @light-fill-quarternary;
        border-radius: 50%;
    }
}
.saletask-info {
    margin-bottom: 16px;
    .tag-main{
      margin-top: 8px;
      .van-tag--round{
        border-radius: 12px;
      }
    }

    .field-icon{
        font-size: 13px;
    }
}
.saletaskmx-info {
    margin-top: 16px;
    margin-bottom: 8px;
    .addgoods {
        color: @light-barkground-elevated-primary;
        margin: 0 16px;
        // margin-bottom: 10px;
        .van-button {
            border-radius: 4px;
            background-color: @musePrimary;
            border: none;
        }
        .add-goods-text {
            color: @light-barkground-elevated-primary;
        //     // font-size: 18px;
        //     // font-weight: 500;
        }
        .iconfont {
            color: @light-barkground-elevated-primary;
            font-size: 14px;
        }
    }
}
.saletask-mx {
    width: 100%;
    height: 100%;
    border-radius: 8px;
    overflow: hidden;
}
.saletask-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // padding: 5px 10px;
    padding-left: 10px;

    

    .price-sum {
        font-size: 16px;
        font-weight: 500;
        color: @colorSharpTextRed;
        margin-right: 15px;
    }

    .goods-buy {
        border: none;
        color: @colorStatusText;
        background: linear-gradient(to right, #ff6034, #ee0a24);
    }

    .good-del {
        border: none;
        color: @colorStatusText;
    }
    .total {
        
        justify-content: flex-end;
        text-align: right;
        margin-right: 8px;
        height: 44px;

        del {
            color: @light-label-tertiary;
            line-height: 16px;
        }
        .price {
            flex-shrink: 0;
            display: flex;
            justify-content: flex-end;
            align-items: baseline;
            .num {
                font-size: 14px;
                color: @musePrimary;
            }
            .p {
                font-size: 20px!important;
                font-weight: 600;
            }
        }
        .other {
            flex: 1;
            display: flex;
            justify-content: flex-end;
            align-items: flex-end;
            // color: @light-label-tertiary;
            color: @musePrimary;
            font-size: 12px;
        }
    }
}

.saletask-footer-wrapper {
    // position: absolute;
    // bottom: 0;
    // left: 0;
    // right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 5px 16px;
    background-color: @cardItemBackgroundColor;

    .confirm-btn {
        width: 40vw;
        // margin: 10px;
        margin-left: 10px;
        border-radius: 18px;
        height: 36px;
        line-height: 36px;
        background-color: @musePrimary;
        color: #fff;
        text-align: center;
        font-size: 14px;
    }
    .total {
        justify-content: flex-end;
        text-align: right;

        del {
            color: @light-label-tertiary;
            line-height: 16px;
        }
        .price {
            display: flex;
            justify-content: flex-end;
            align-items: baseline;
            .num {
                font-size: 14px;
                color: @musePrimary;
            }
            .p {
                font-size: 20px!important;
                font-weight: 600;
            }
        }
        .other {
            display: flex;
            justify-content: flex-end;
            align-items: flex-end;
            // color: @light-label-tertiary;
            color: @musePrimary;
            // font-size: 12px;
        }
    }
}
.saletask-mx {
    margin-bottom: 8px;
}
.saletask-mx-container {
    background-color: #fff;
    width: 100%;
    height: 100%;
}
</style>