<template>
    <div v-loading="dialogLoading" :element-loading-text="loadingText">
        <el-form :model="orderForm" :inline="true" ref="confirmPayForm">
            <el-card>
                <el-descriptions title="核对信息" border :column="3" :label-style="{ width: '120px' }">
                    <el-descriptions-item label="总件数">{{ orderClothItemDTOS.length }}</el-descriptions-item>
                    <el-descriptions-item label="总价格（元）">{{ calculateTotalPrice() + calculateDeliveryPrice() }}</el-descriptions-item>
                    <el-descriptions-item label="卡余额（元）" v-if="appUserInfo">{{ appUserInfo.rechargeBalance + appUserInfo.giveBalance }}</el-descriptions-item>
                    <el-descriptions-item label="卡余额（元）" v-else>0</el-descriptions-item>
                    <el-descriptions-item label="衣服价格（元）">{{ calculateTotalPrice() }}</el-descriptions-item>
                    <el-descriptions-item label="优惠金额（元）">{{ calculateTotalDiscountAmount() }}</el-descriptions-item>
                    <el-descriptions-item label="现金余额（元）">{{ appUserInfo ? appUserInfo.rechargeBalance : '0' }}</el-descriptions-item>
                    <el-descriptions-item label="配送费（元）">
                        {{ calculateDeliveryPrice() }}
                    </el-descriptions-item>
                    <el-descriptions-item label="还需支付（元）">{{ Math.round((calculateTotalPrice() + calculateDeliveryPrice() - calculateTotalDiscountAmount()) * 100) / 100 }}</el-descriptions-item>
                    <el-descriptions-item label="赠送余额">{{ appUserInfo ? appUserInfo.giveBalance : '0' }}</el-descriptions-item>
                </el-descriptions>
            </el-card>

            <el-card style="margin-top: 10px">
                <div slot="header" class="clearfix">
                    <span>优惠信息</span>
                </div>
                <el-tabs v-model="confirmCouponTabIndex" type="card" @tab-click="onCouponTabChange">
                    <el-tab-pane label="优惠券" name="0">
                        <el-form-item label="优惠券" prop="fullCoupon">
                            <el-input ref="couponRef" v-model="orderForm.fullCoupon" placeholder="请输入优惠券兑换码" clearable />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary">核销</el-button>
                            <el-button type="primary" @click="onSelectCoupon">列表选券</el-button>
                        </el-form-item>

                        <el-tabs v-model="confirmDeductTabIndex" type="card" @tab-click="onCouponTabChange">
                            <el-tab-pane label="优惠券使用明细" name="0">
                                <el-table :data="selectCoupons">
                                    <el-table-column label="优惠券" align="center" prop="couponTypeTitle" />
                                    <el-table-column label="优惠券类型" align="center" prop="discountWay">
                                        <template slot-scope="scope">
                                            <span v-if="scope.row.discountWay == '0'">抵扣券</span>
                                            <span v-if="scope.row.discountWay == '1'">满减券</span>
                                            <span v-if="scope.row.discountWay == '2'">折扣券</span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="优惠内容" align="center">
                                        <template slot-scope="scope">
                                            <span v-if="scope.row.discountWay == '0'">抵扣{{ scope.row.deductAmount }}元</span>
                                            <span v-if="scope.row.discountWay == '1'">满{{ parseInt(scope.row.minAmount) }}件减{{ scope.row.deductAmount }}件</span>
                                            <span v-if="scope.row.discountWay == '2'">优惠{{ scope.row.discountRate }}%，最多折扣{{ scope.row.deductAmount }}元</span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="是否单件" align="center" prop="isUnique">
                                        <template slot-scope="scope">
                                            <span v-if="scope.row.discountWay == '2'">
                                                <span v-if="scope.row.isUnique == 'Y'">单件</span>
                                                <span v-if="scope.row.isUnique == 'N'">非单件</span>
                                            </span>
                                            <span v-else>--</span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="优惠衣服" align="center" prop="selectClothInfo" :width="350">
                                        <template slot-scope="scope">
                                            <template v-for="(item, index) in scope.row.orderCouponClothDTOS">
                                                <br v-if="index != 0" />
                                                <span>{{ '#' + (item.clothIndex + 1) + ' ' + item.clothName + '  优惠' + item.deductAmount + '元' }}</span>
                                            </template>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
                                        <template slot-scope="scope">
                                            <el-button type="text" @click="changeCouponDiscountCloth(scope.$index)" v-if="scope.$index == selectCoupons.length - 1 && scope.row.discountWay == '2' && scope.row.isUnique == 'Y'">修改优惠衣服</el-button>
                                            <el-button type="text" style="color: #ff4949" @click="removeSelectCoupon(scope.$index)" v-if="scope.$index == selectCoupons.length - 1">删除</el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-tab-pane>
                            <el-tab-pane label="衣服抵扣明细" name="1">
                                <el-table :data="orderClothItemDTOS">
                                    <el-table-column label="序号" type="index" align="center" fixed="left" width="80"> </el-table-column>
                                    <el-table-column label="衣物名称" align="center" prop="name" width="250">
                                        <template slot-scope="scope">
                                            <span>{{ calculateClothName(scope.$index) }}</span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="颜色" align="center" width="120">
                                        <template slot-scope="scope">
                                            <span>
                                                <template v-for="(color, index) in scope.row.orderClothColorDTOS">
                                                    {{ index == 0 ? color.clothColorName : ',' + color.clothColorName }}
                                                </template>
                                            </span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="衣服品牌" align="center" prop="name" width="120">
                                        <template slot-scope="scope">
                                            <span>{{ scope.row.clothBrandName }}</span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="瑕疵" align="center">
                                        <template slot-scope="scope">
                                            <span>
                                                <template v-for="(color, index) in scope.row.orderClothFlawDTOS">
                                                    {{ index == 0 ? color.clothFlawName : ',' + color.clothFlawName }}
                                                </template>
                                            </span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="洗涤方式" align="center" width="80">
                                        <template slot-scope="scope">
                                            <span>{{ scope.row.clothWashModeName }}</span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="价格（元）" align="center" prop="totalPrice" width="100" />
                                    <el-table-column label="优惠金额（元）" align="center" prop="deductAmount" width="130" />
                                    <el-table-column label="还需支付（元）" align="center" width="130">
                                        <template slot-scope="scope">
                                            <span>{{ handelPrice(scope.row.totalPrice - scope.row.deductAmount) }}</span>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-tab-pane>
                        </el-tabs>
                    </el-tab-pane>
                    <el-tab-pane label="平台折扣活动" name="1">
                        <!-- <el-form-item label="折扣" prop="discountCouponId">
                            <el-select v-model="orderForm.discountCouponId" placeholder="请选择" clearable @change="onDiscountCouponSelect">
                                <template v-for="(item, i) in discountCouponVOS">
                                    <el-option :key="i" :label="item.couponTypeTitle + '（' + item.deductAmount * 10 + '折）'" :value="item.id"> </el-option>
                                </template>
                            </el-select>
                        </el-form-item> -->
                        <el-table :data="discountCouponVOS" style="width: 100%">
                            <el-table-column label="序号" type="index" align="center" fixed="left" width="80"> </el-table-column>
                            <el-table-column label="活动名称" align="center" prop="title"> </el-table-column>
                            <el-table-column label="适用衣服" align="center">
                                <template slot-scope="scope">
                                    <span v-if="scope.row.clothScope == 0">全部衣服 折扣{{ scope.row.discount * 100 }}%</span>
                                    <span v-if="scope.row.clothScope == 1">指定衣服</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="适用支付方式" align="center">
                                <template slot-scope="scope">
                                    <span v-if="scope.row.payScope == 0">全部</span>
                                    <span v-if="scope.row.payScope == 1">现金</span>
                                    <span v-if="scope.row.payScope == 2">余额</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="可优惠金额(元)" align="center">
                                <template slot-scope="scope">
                                    <span>{{ calculateTotalDiscountByActivity(scope.row) }}</span>
                                </template>
                            </el-table-column>
                            <!-- <el-table-column label="衣服品牌" align="center" prop="name" width="120"> -->
                            <!-- <template slot-scope="scope"> -->
                            <!-- <span>{{ scope.row.clothBrandName }}</span> -->
                            <!-- </template> -->
                            <!-- </el-table-column> -->
                            <!-- <el-table-column label="瑕疵" align="center"> -->
                            <!-- <template slot-scope="scope"> -->
                            <!-- <span> -->
                            <!-- <template v-for="(color, index) in scope.row.orderClothFlawDTOS"> -->
                            <!-- {{ index == 0 ? color.clothFlawName : ',' + color.clothFlawName }} -->
                            <!-- </template> -->
                            <!-- </span> -->
                            <!-- </template> -->
                            <!-- </el-table-column> -->
                            <!-- <el-table-column label="洗涤方式" align="center" width="80"> -->
                            <!-- <template slot-scope="scope"> -->
                            <!-- <span>{{ scope.row.clothWashModeName }}</span> -->
                            <!-- </template> -->
                            <!-- </el-table-column> -->
                            <!-- <el-table-column label="原价（元）" align="center" prop="totalPrice" width="100" /> -->
                            <!-- <el-table-column label="抵扣金额（元）" align="center" prop="deductAmount" width="130" /> -->
                            <!-- <el-table-column label="折扣金额（元）" align="center" width="130">
                                <template slot-scope="scope">
                                    <span>{{ parseInt((scope.row.totalPrice - scope.row.deductAmount) * 100 * parseInt(100 - orderForm.discountCoupon.deductAmount * 100)) / 10000 }}</span>
                                </template>
                            </el-table-column> -->
                            <el-table-column label="操作" align="center">
                                <template slot-scope="scope">
                                    <el-button type="text" style="color: #67c23a" @click="onSelectActivityItem({})" v-if="activity.id === scope.row.id">不使用</el-button>
                                    <el-button type="text" @click="onSelectActivityItem(scope.row)" v-else>使用</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-tab-pane>
                    <el-tab-pane label="现金券" name="2">
                        <el-form-item label="现金券" prop="cashDeductAmount">
                            <el-input-number
                                style="text-align: left"
                                v-model="orderForm.cashDeductAmount"
                                placeholder="请输入现金券金额"
                                :precision="2"
                                clearable
                                :controls="false"
                                :max="(parseInt(calculateTotalPrice() * 100) + parseInt(calculateDeliveryPrice() * 100) - parseInt(calculateTotalDiscountAmountWithoutCashAmount() * 100)) / 100"
                            />
                        </el-form-item>
                        <el-form-item label="是否特殊" prop="isSpecial">
                            <el-checkbox v-model="orderForm.isSpecial"></el-checkbox>
                        </el-form-item>
                    </el-tab-pane>
                </el-tabs>
            </el-card>

            <el-card style="margin-top: 10px">
                <div slot="header" class="clearfix">
                    <span>收款方式</span>
                    <template v-if="calculateTotalDiscountAmountByCoupon()">
                        <span style="margin-left: 10px">优惠券优惠</span>
                        <span style="color: #ff4949">{{ calculateTotalDiscountAmountByCoupon() }}</span>
                        <span>元</span>
                    </template>

                    <template v-if="calculateTotalDiscountByActivity(activity)">
                        <span style="margin-left: 10px">平台折扣活动优惠</span>
                        <span style="color: #ff4949">{{ calculateTotalDiscountByActivity(activity) }}</span>
                        <span>元</span>
                    </template>

                    <template v-if="orderForm.cashDeductAmount">
                        <span style="margin-left: 10px">现金券优惠</span>
                        <span style="color: #ff4949">{{ orderForm.cashDeductAmount }}</span>
                        <span>元</span>
                    </template>

                    <span style="margin-left: 10px">还需支付</span>
                    <span style="color: #ff4949">{{ (parseInt(calculateTotalPrice() * 100) + parseInt(calculateDeliveryPrice() * 100) - parseInt(calculateTotalDiscountAmount() * 100)) / 100 }}</span>
                    <span>元</span>
                </div>
                <el-form-item label="支付方式" prop="payType">
                    <el-radio-group v-model="orderForm.payType" @change="changePayType">
                        <el-radio-button label="3" name="3">余额</el-radio-button>
                        <el-radio-button label="2" name="2">现金</el-radio-button>
                        <el-radio-button label="0" name="0">微信/支付宝</el-radio-button>
                        <!--                        <el-radio-button label="1" name="1">支付宝</el-radio-button>-->
                    </el-radio-group>
                </el-form-item>
                <br />

                <el-form-item label="付款码" prop="authCode" v-if="orderForm.payType == '0' || orderForm.payType == '1'">
                    <el-input ref="autoFocus" v-model="orderForm.authCode" clearable placeholder="请选择付款码" />
                </el-form-item>

                <el-descriptions border :column="3" :labelStyle="{ width: '140px' }" v-if="orderForm.payType == '3'">
                    <el-descriptions-item label="用户" v-if="appUserInfo">{{ appUserInfo.realName }}</el-descriptions-item>
                    <el-descriptions-item label="当前余额（元）" v-if="appUserInfo">{{ appUserInfo.rechargeBalance + appUserInfo.giveBalance }}</el-descriptions-item>
                    <el-descriptions-item label="支付后余额（元）" v-if="appUserInfo">{{ appUserInfo.rechargeBalance + appUserInfo.giveBalance - calculateTotalPrice() }}</el-descriptions-item>
                </el-descriptions>

                <div style="margin-top: 20px">
                    <el-button type="primary" @click="submitForm(0)" style="margin-right: 10px">立即支付</el-button>
                    <el-button type="primary" @click="submitForm(1)" style="margin-right: 5%">取衣支付</el-button>
                </div>
            </el-card>
        </el-form>

        <el-dialog title="选择优惠券" :visible.sync="setCouponOpen" append-to-body width="55%">
            <!--      :before-close="handleClose"-->

            <el-table :data="deductCouponVOS" style="width: 100%">
                <el-table-column label="优惠券" align="center" prop="couponTypeTitle" />
                <el-table-column label="优惠券类型" align="center" prop="discountType">
                    <template slot-scope="scope">
                        <span v-if="scope.row.discountWay == '0'">抵扣券</span>
                        <span v-if="scope.row.discountWay == '1'">满减券</span>
                        <span v-if="scope.row.discountWay == '2'">折扣券</span>
                    </template>
                </el-table-column>
                <el-table-column label="优惠内容" align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.discountWay == '0'">抵扣{{ scope.row.deductAmount }}元</span>
                        <span v-if="scope.row.discountWay == '1'">满{{ parseInt(scope.row.minAmount) }}件减{{ scope.row.deductAmount }}件</span>
                        <span v-if="scope.row.discountWay == '2'">优惠{{ scope.row.discountRate }}%，最多折扣{{ scope.row.deductAmount }}元</span>
                    </template>
                </el-table-column>
                <el-table-column label="是否单件" align="center" prop="isUnique">
                    <template slot-scope="scope">
                        <span v-if="scope.row.discountWay == '2'">
                            <span v-if="scope.row.isUnique == 'Y'">单件</span>
                            <span v-if="scope.row.isUnique == 'N'">非单件</span>
                        </span>
                        <span v-else>--</span>
                    </template>
                </el-table-column>
                <el-table-column label="适用衣服" align="center">
                    <template slot-scope="scope">
                        <template v-for="(item, index) in scope.row.appCouponTypeApplyClothVOS" v-key="index">
                            <span>{{ index > 0 ? '、' : '' }}{{ item.clothItemName }}</span>
                        </template>
                        <span v-if="!scope.row.appCouponTypeApplyClothVOS">全部衣服</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" @click="onSelectCouponItem(scope.$index)">选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>

        <el-dialog :title="changeDiscountCouponTitle" :visible.sync="changeDiscountCouponOpen" width="70%" append-to-body>
            <!-- <span v-if="changeDiscountCouponOpen"> 共可抵扣{{ selectCoupons[changeDiscountCouponIndex].deductAmount }}元，已抵扣{{ calculateCouponDiscountInfo() }}元，剩余可抵扣{{ selectCoupons[changeDiscountCouponIndex].deductAmount - calculateCouponDiscountInfo() }}元 </span>
            <br /> -->
            <span>可选择衣物</span>
            <br />
            <el-table :data="changeDiscountCouponCanSelectCloths">
                <el-table-column label="序号" align="center" fixed="left" width="80">
                    <template slot-scope="scope">
                        <span>#{{ scope.row.index + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="衣物名称" align="center" prop="name" width="250">
                    <template slot-scope="scope">
                        <span>{{ calculateClothNameByInfo(scope.row) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="颜色" align="center" width="120">
                    <template slot-scope="scope">
                        <span>
                            <template v-for="(color, index) in scope.row.orderClothColorDTOS">
                                {{ index == 0 ? color.clothColorName : ',' + color.clothColorName }}
                            </template>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="衣服品牌" align="center" prop="name" width="120">
                    <template slot-scope="scope">
                        <span>{{ scope.row.clothBrandName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="瑕疵" align="center">
                    <template slot-scope="scope">
                        <span>
                            <template v-for="(color, index) in scope.row.orderClothFlawDTOS">
                                {{ index == 0 ? color.clothFlawName : ',' + color.clothFlawName }}
                            </template>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="洗涤方式" align="center" width="80">
                    <template slot-scope="scope">
                        <span>{{ scope.row.clothWashModeName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="价格（元）" align="center" prop="totalPrice" width="100" />
                <el-table-column label="待优惠金额（元）" align="center" prop="deductAmount" width="130" />
                <el-table-column label="还需支付（元）" align="center" width="130">
                    <template slot-scope="scope">
                        <span>{{ scope.row.totalPrice - scope.row.deductAmount }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" style="color: #67c23a" v-if="scope.row.isSelected">已选择</el-button>
                        <el-button type="text" style="color: #ff4949" @click="addCouponRelateCloth(scope.$index)" v-else>选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br />
            <!-- <span>已选择衣物</span> -->
            <!-- <br /> -->
            <!-- <el-table :data="changeDiscountCouponSelectCloths" style="width: 100%">
                <el-table-column label="序号" align="center" width="80">
                    <template slot-scope="scope">
                        <span>#{{ scope.row.clothIndex + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="衣服名称" align="center" prop="clothName" width="250" />
                <el-table-column label="颜色" align="center" width="120">
                    <template slot-scope="scope">
                        <span>
                            <template v-for="(color, index) in scope.row.cloth.orderClothColorDTOS">
                                {{ index == 0 ? color.clothColorName : ',' + color.clothColorName }}
                            </template>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="衣服品牌" align="center" prop="name" width="120">
                    <template slot-scope="scope">
                        <span>{{ scope.row.cloth.clothBrandName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="瑕疵" align="center">
                    <template slot-scope="scope">
                        <span>
                            <template v-for="(color, index) in scope.row.cloth.orderClothFlawDTOS">
                                {{ index == 0 ? color.clothFlawName : ',' + color.clothFlawName }}
                            </template>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="洗涤方式" align="center" width="80">
                    <template slot-scope="scope">
                        <span>{{ scope.row.cloth.clothWashModeName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="原价（元）" align="center" prop="totalPrice" width="100">
                    <template slot-scope="scope">
                        <span>{{ scope.row.cloth.totalPrice }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="已优惠金额（元）" align="center" prop="deductAmount" width="130">
                    <template slot-scope="scope">
                        <span>{{ scope.row.cloth.deductAmount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="还需支付（元）" align="center" width="130">
                    <template slot-scope="scope">
                        <span>{{ scope.row.cloth.totalPrice - scope.row.cloth.deductAmount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="本次优惠金额（元）" align="center" prop="deductAmount" width="180"> </el-table-column>
                <el-table-column label="操作" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" style="color: #ff4949" @click="removeCouponRelateCloth(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table> -->

            <span slot="footer" class="dialog-footer">
                <el-button @click="changeDiscountCouponOpen = false">取 消</el-button>
                <el-button type="primary" @click="submitChangeCouponRelateCloth">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { collectCloth } from '../../../../api/order/cloth'

export default {
    name: 'cashCloth',
    props: {
        appUserInfo: {
            type: Object
        },
        orderForm: {
            type: Object
        },
        orderClothItemDTOS: {
            type: Array,
            default: []
        },
        clothSpeeds: {
            type: Array,
            default: []
        },
        deductCouponVOS: {
            type: Array,
            default: []
        },
        discountCouponVOS: {
            //可选择的折扣券
            type: Array,
            default: []
        }
    },
    data() {
        return {
            dialogLoading: false,
            setCouponOpen: false, //是否选择优惠券

            loadingText: '',
            confirmCouponTabIndex: '0',
            confirmDeductTabIndex: '0',
            selectCoupons: [],
            changeDiscountCouponIndex: -1,
            changeDiscountCouponOpen: false,
            changeDiscountCouponTitle: '',
            changeDiscountCouponSelectCloths: [],
            changeDiscountCouponCanSelectCloths: [],
            activity: {}
        }
    },
    mounted() {
        if (process.env.ENV != 'production') {
            window.cashClothDialog = this
        }
        console.log('a12')
    },
    methods: {
        changePayType() {
            if (this.orderForm.payType == 0) {
                this.orderForm.authCode = ''
                this.$nextTick((_) => {
                    this.$refs.autoFocus.focus()
                })
            }
        },
        // 处理现金金额
        handelPrice(e) {
            return Math.round(e * 100) / 100
        },
        calculateTotalPrice() {
            var price = 0
            for (let i = 0; i < this.orderClothItemDTOS.length; i++) {
                price += this.calculateClothSpecialPrice(i) + this.orderClothItemDTOS[i].defaultPrice
            }
            return price
        },
        calculateClothSpecialPrice(index) {
            var specialPrice = 0
            if (this.orderClothItemDTOS[index].orderClothCraftDTOS && this.orderClothItemDTOS[index].orderClothCraftDTOS.length > 0) {
                for (let i = 0; i < this.orderClothItemDTOS[index].orderClothCraftDTOS.length; i++) {
                    specialPrice += this.orderClothItemDTOS[index].orderClothCraftDTOS[i].price
                }
            }

            if (this.orderClothItemDTOS[index].isHedging == 'Y') {
                specialPrice += this.orderClothItemDTOS[index].hedgingPrice * 0.01
            }
            for (let i = 0; i < this.clothSpeeds.length; i++) {
                if (this.orderClothItemDTOS[index].clothSpeedId == this.clothSpeeds[i].id) {
                    specialPrice += this.orderClothItemDTOS[index].defaultPrice * (this.clothSpeeds[i].multiple - 1)
                    break
                }
            }
            return specialPrice
        },
        calculateDeliveryPrice() {
            if (this.calculateTotalPrice() >= 100) {
                return 0
            }
            var delivery = 0
            if (this.orderForm.takeClothWay && this.orderForm.takeClothWay == '1') {
                delivery += 10
            }
            if (this.orderForm.sendClothWay && this.orderForm.sendClothWay == '1') {
                delivery += 10
            }
            return delivery
        },
        //计算当前所有优惠金额
        calculateTotalDiscountAmount() {
            var totalDiscountAmount = this.orderForm.cashDeductAmount ? this.orderForm.cashDeductAmount : 0
            this.orderClothItemDTOS.forEach((cloth) => {
                totalDiscountAmount = totalDiscountAmount + cloth.deductAmount
            })
            if (this.activity.id) {
                totalDiscountAmount = totalDiscountAmount + this.calculateTotalDiscountByActivity(this.activity)
            }
            return Math.round(totalDiscountAmount * 100) / 100
        },
        // 计算优惠券优惠金额
        calculateTotalDiscountAmountByCoupon() {
            var totalDiscountAmount = 0
            this.orderClothItemDTOS.forEach((cloth) => {
                totalDiscountAmount = Math.round((totalDiscountAmount + cloth.deductAmount) * 100) / 100
            })
            return totalDiscountAmount
        },

        // 计算平台活动优惠金额
        calculateTotalDiscountByActivity(e) {
            if (e.payScope == 2 && this.orderForm.payType && this.orderForm.payType != 3) {
                return 0
            }
            if (e.payScope == 1 && this.orderForm.payType && this.orderForm.payType == 3) {
                return 0
            }
            let activityAmount = 0
            if (e.clothScope == 0) {
                let total = this.calculateTotalPrice() + this.calculateDeliveryPrice() - this.calculateTotalDiscountAmountByCoupon()
                activityAmount = total * e.discount
            }
            if (e.clothScope == 1) {
                let total = this.calculateTotalPrice() + this.calculateDeliveryPrice() - this.calculateTotalDiscountAmountByCoupon()
                activityAmount = total * e.discount
            }
            return Math.round(activityAmount * 100) / 100
        },

        //计算出去现金券其余的优惠金额
        calculateTotalDiscountAmountWithoutCashAmount() {
            var totalDiscountAmount = 0
            this.orderClothItemDTOS.forEach((cloth) => {
                totalDiscountAmount = totalDiscountAmount + cloth.deductAmount
            })
            if (this.activity.id) {
                totalDiscountAmount = totalDiscountAmount + this.calculateTotalDiscountByActivity(this.activity)
            }

            return Math.round(totalDiscountAmount * 100) / 100
        },
        /**
         * 计算当前优惠券抵扣明细
         */
        calculateCouponDiscountInfo() {
            var deductAmount = 0
            this.changeDiscountCouponSelectCloths.forEach((item) => {
                deductAmount += item.deductAmount
            })
            return deductAmount
        },

        onCouponTabChange(tab, event) {
            console.log('tab.index:' + tab.index)
            // if(tab.index == '1'){
            //   this.$refs['couponRef'].focus()
            // }
        },
        onSelectCoupon() {
            if (this.deductCouponVOS.length == 0) {
                this.$message.error('没有可选择的优惠券')
                return
            }
            this.setCouponOpen = true
        },
        // 选择可优惠金额
        changeCouponDiscountCloth(index) {
            // this.changeDiscountCouponSelectCloths = this.selectCoupons[index].orderCouponClothDTOS.concat()
            // for (let i = 0; i < this.changeDiscountCouponSelectCloths.length; i++) {
            //     var item = this.changeDiscountCouponSelectCloths[i]
            //     item.cloth = JSON.parse(JSON.stringify(this.orderClothItemDTOS[item.clothIndex]))
            // }
            // this.changeDiscountCouponIndex = index
            this.changeDiscountCouponCanSelectCloths = this.selectCoupons[index].applyCloths.concat()
            this.changeDiscountCouponCanSelectCloths.forEach((item) => {
                let deductAmount1 = this.handelPrice(item.totalPrice * ( this.selectCoupons[index].discountRate / 100))
                let deductAmount = deductAmount1 > this.selectCoupons[index].deductAmount ? this.selectCoupons[index].deductAmount : deductAmount1
                item.deductAmount = deductAmount
                item.isSelected = item.id == this.selectCoupons[index].orderCouponClothDTOS[0].id
            })
            // this.orderClothItemDTOS.forEach((item) => {
            //     //如果这件衣服已经被被抵扣完成，则不需要选择
            //     if (item.totalPrice - item.deductAmount <= 0) {
            //         return
            //     }
            //     //添加优惠券支持的衣服
            //     console.log('A4')
            //     if (this.selectCoupons[index].appCouponTypeApplyClothVOS) {
            //         if (!this.selectCoupons[index].appCouponTypeApplyClothVOS.find((i) => i.clothItemId == item.id)) {
            //             return
            //         }
            //     }

            //     //如果这个优惠券已经被选择过，则跳过
            //     console.log('A5')
            //     if (this.selectCoupons[index].orderCouponClothDTOS) {
            //         if (this.selectCoupons[index].orderCouponClothDTOS.find((i) => i.clothIndex == item.index)) {
            //             return
            //         }
            //     }
            //     this.changeDiscountCouponCanSelectCloths.push(item)
            // })
            this.changeDiscountCouponTitle = '修改『' + this.selectCoupons[index].couponTypeTitle + '』可抵扣衣物'
            this.changeDiscountCouponOpen = true
        },
        onDiscountCouponSelect(e) {
            this.orderForm.discountCoupon = this.discountCouponVOS.find((item) => item.id == e)
        },
        getDiscountClothList() {
            var cloths = []
            if (!this.orderForm.discountCoupon) {
                return cloths
            }
            this.orderClothItemDTOS.forEach((cloth) => {
                if (this.orderForm.discountCoupon.appCouponTypeApplyClothVOS) {
                    if (this.orderForm.discountCoupon.appCouponTypeApplyClothVOS.find((item) => item.clothItemId == cloth.id)) {
                        cloths.push(cloth)
                    }
                }
            })
            return cloths
        },
        calculateClothName(index) {
            var clothName = this.orderClothItemDTOS[index].name
            this.orderClothItemDTOS[index].orderClothTypeDTOS.forEach((item) => {
                if (item.clothTypeCode == '0') {
                    clothName += ' - 加厚'
                }
                if (item.clothTypeCode == '0') {
                    clothName += ' - 羊绒'
                }
                if (item.clothTypeCode == '0') {
                    clothName += ' - 真丝'
                }
                if (item.clothTypeCode == '0') {
                    clothName += ' - 儿童'
                }
            })
            return clothName
        },
        calculateClothNameByInfo(cloth) {
            var clothName = cloth.name
            cloth.orderClothTypeDTOS.forEach((item) => {
                if (item.clothTypeCode == '0') {
                    clothName += ' - 加厚'
                }
                if (item.clothTypeCode == '0') {
                    clothName += ' - 羊绒'
                }
                if (item.clothTypeCode == '0') {
                    clothName += ' - 真丝'
                }
                if (item.clothTypeCode == '0') {
                    clothName += ' - 儿童'
                }
            })
            return clothName
        },

        onSelectActivityItem(e) {
            if (e.id) {
                let activityAmount = 0
                if (e.clothScope == 0) {
                    let total = this.calculateTotalPrice() + this.calculateDeliveryPrice() - this.calculateTotalDiscountAmountByCoupon()
                    activityAmount = total * e.discount
                }
                if (e.clothScope == 1) {
                    let total = this.calculateTotalPrice() + this.calculateDeliveryPrice() - this.calculateTotalDiscountAmountByCoupon()
                    activityAmount = total * e.discount
                }
                e.activityAmount = Math.round(activityAmount * 100) / 100
            }
            this.activity = e
        },

        onSelectCouponItem(index) {
            var coupon = this.deductCouponVOS[index]
            // 折扣券
            coupon.orderCouponClothDTOS = []

            //找出优惠券适配优惠的衣服
            var applyCloths = []
            if (coupon.appCouponTypeApplyClothVOS) {
                coupon.appCouponTypeApplyClothVOS.forEach((item) => {
                    for (let i = 0; i < this.orderClothItemDTOS.length; i++) {
                        var cloth = this.orderClothItemDTOS[i]
                        if (cloth.id != item.clothItemId || cloth.totalPrice <= 0) {
                            continue
                        }
                        applyCloths.push(cloth)
                    }
                })
            }

            //排序，按照订单剩余需要支付的金额降序
            applyCloths.sort((a, b) => {
                return b.totalPrice - a.totalPrice
            })
            this.orderClothItemDTOS.sort((a, b) => {
                return b.totalPrice - a.totalPrice
            })

            coupon.applyCloths = coupon.useScope == 2 ? applyCloths : this.orderClothItemDTOS

            if (coupon.useScope == 2 && !applyCloths.length) {
                this.$message.error('没有指定衣物，无法使用该优惠券')
                return
            }
            if (coupon.discountWay == 2) {
                if (coupon.isUnique == 'Y') {
                    // 单件
                    let cloth = coupon.applyCloths[coupon.applyCloths.length - 1]
                    // 获取可用衣物里的最小价格
                    let deductAmount1 = cloth.totalPrice * (coupon.discountRate / 100)
                    let deductAmount = deductAmount1 > coupon.deductAmount ? coupon.deductAmount : deductAmount1
                    coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: deductAmount, clothName: this.calculateClothNameByInfo(cloth) })
                    cloth.deductAmount = deductAmount
                } else {
                    // 全部
                    let list = coupon.applyCloths
                    for (let i = 0; i < list.length; i++) {
                        let cloth = list[i]
                        let deductAmount1 = this.handelPrice(cloth.totalPrice * (coupon.discountRate / 100))
                        let deductAmount = deductAmount1 > coupon.deductAmount ? coupon.deductAmount : deductAmount1
                        coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: deductAmount, clothName: this.calculateClothNameByInfo(cloth) })
                        cloth.deductAmount = deductAmount
                    }
                }
            }
            if (coupon.discountWay == '0') {
                //         //抵扣
                //         if (coupon.isUnique == 'Y') {
                //             //单件，只减一件
                //             var cloth = applyCloths[0]
                //             //抵扣所有衣服里金额最大的
                //             if (cloth.totalPrice - cloth.deductAmount >= coupon.deductAmount) {
                //                 coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: coupon.deductAmount, clothName: this.calculateClothNameByInfo(cloth) })
                //                 cloth.deductAmount += coupon.deductAmount
                //             } else {
                //                 coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: cloth.totalPrice - cloth.deductAmount, clothName: this.calculateClothNameByInfo(cloth) })
                //                 cloth.deductAmount = cloth.totalPrice
                //             }
                //         } else {
                //             //非单件，可减多件
                var deductAmount = coupon.deductAmount
                for (let i = 0; i < coupon.applyCloths.length; i++) {
                    //如果优惠券可抵扣金额用完
                    if (deductAmount <= 0) {
                        break
                    }
                    var cloth = coupon.applyCloths[i]
                    if (cloth.totalPrice - cloth.deductAmount >= deductAmount) {
                        coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: deductAmount, clothName: this.calculateClothNameByInfo(cloth) })
                        cloth.deductAmount += deductAmount
                        deductAmount = 0
                    } else {
                        coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: cloth.totalPrice - cloth.deductAmount, clothName: this.calculateClothNameByInfo(cloth) })
                        cloth.deductAmount = cloth.totalPrice
                        deductAmount -= cloth.totalPrice - cloth.deductAmount
                    }
                }
            }
            // }
            // else
            if (coupon.discountWay == '1') {
                //满减
                //       console.log(applyCloths, coupon.minAmount)
                // if (coupon.useScope === 2 && applyCloths.length < parseInt(coupon.minAmount)) {
                //     //未满足满减条件
                //     this.$message.error('未满足满减条件')
                //     return
                // }
                if (coupon.applyCloths.length < parseInt(coupon.minAmount)) {
                    this.$message.error('未满足满减条件')
                    return
                }

                if (coupon.isUnique !== 'Y') {
                    //单件，只减一件
                    var cloth = coupon.applyCloths[coupon.applyCloths.length - 1]
                    coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: cloth.totalPrice, clothName: this.calculateClothNameByInfo(cloth) })
                    cloth.deductAmount = cloth.totalPrice
                } else {
                    //非单件，可减多件
                    var count = parseInt(coupon.applyCloths.length / parseInt(coupon.minAmount)) * parseInt(coupon.deductAmount)
                    for (let i = 0; i < count; i++) {
                        var cloth = coupon.applyCloths[coupon.applyCloths.length - 1 - i]
                        coupon.orderCouponClothDTOS.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: cloth.totalPrice, clothName: this.calculateClothNameByInfo(cloth) })
                        cloth.deductAmount = cloth.totalPrice
                    }
                }
            }
            //     this.deductCouponVOS.splice(index, 1)
            this.selectCoupons = [coupon]
            this.setCouponOpen = false
            this.$emit('initOrderList', this.orderClothItemDTOS)
        },
        //去除已选择的优惠券
        removeSelectCoupon(index) {
            var coupon = this.selectCoupons[index]
            //将优惠券放回未选择的数组中
            // if (coupon.discountWay == '2') {
            //     this.discountCouponVOS.push(coupon)
            // } else {
            //     this.deductCouponVOS.push(coupon)
            // }
            this.selectCoupons.splice(index, 1)

            //重置这条优惠券给衣服的优惠信息
            var orderCouponClothDTOS = coupon.orderCouponClothDTOS
            coupon.orderCouponClothDTOS = []
            orderCouponClothDTOS.forEach((selectItem) => {
                this.calculateClothDiscountAmount(selectItem.clothIndex)
            })
        },
        /** 提交按钮 */
        async submitForm(payTimeType) {
            this.$refs['confirmPayForm'].validate((valid) => {
                if (valid) {
                    var params = { ...this.orderForm }
                    params.appUserId = this.appUserInfo.id
                    params.orderClothItemDTOS = this.orderClothItemDTOS.concat()
                    params.orderClothItemDTOS.forEach((item) => {
                        item.clothItemName = item.name
                    })
                    var orderCouponDTOS = this.selectCoupons.concat()
                    if (this.orderForm.discountCouponId) {
                        var coupon = this.discountCouponVOS.find((item) => (item.id = this.orderForm.discountCouponId))
                        var orderCouponClothDTOS = []
                        this.getDiscountClothList().forEach((item) => {
                            var orderCouponClothDTO = { id: item.id, clothIndex: item.index, deductAmount: parseInt((item.totalPrice - item.deductAmount) * 100 * parseInt(100 - coupon.deductAmount * 100)) / 10000 }
                            orderCouponClothDTOS.push(orderCouponClothDTO)
                        })
                        orderCouponDTOS.push({ id: coupon.id, ...{ orderCouponClothDTOS: orderCouponClothDTOS } })
                    }
                    params.orderCouponDTOS = orderCouponDTOS
                    params.isSpecial = params.isSpecial ? 'Y' : 'N'
                    params.payTimeType = payTimeType
                    if (payTimeType == 1) {
                        params.payType = 0
                    }
                    if (this.calculateTotalDiscountByActivity(this.activity)) {
                        params.activityId = this.activity.id
                        params.activityDiscountAmount = this.calculateTotalDiscountByActivity(this.activity)
                    }

                    console.log(params)
                    const loading = this.$loading({
                        lock: true,
                        text: 'Loading',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    })
                    collectCloth(params)
                        .then((res) => {
                            this.$message('提交成功')
                            loading.close()
                            this.$emit('onPaySuccess', { ...res.data, authCode: this.orderForm.authCode })
                        })
                        .catch((e) => {
                            loading.close()
                            this.$message.error(e.data.msg)
                        })
                }
            })
        },
        removeCouponRelateCloth(index) {
            var cloth = this.changeDiscountCouponSelectCloths[index].cloth
            cloth.deductAmount = cloth.deductAmount - this.changeDiscountCouponSelectCloths[index].deductAmount
            this.changeDiscountCouponCanSelectCloths.push(cloth)
            this.changeDiscountCouponSelectCloths.splice(index, 1)
        },
        //选择优惠券关联的衣服
        addCouponRelateCloth(index) {
            this.changeDiscountCouponCanSelectCloths.forEach((item, i) => {
                item.clothIndex = item.index
                ;(item.clothName = this.calculateClothNameByInfo(item)), (item.isSelected = i === index)
            })
            this.changeDiscountCouponCanSelectCloths = JSON.parse(JSON.stringify(this.changeDiscountCouponCanSelectCloths))
            // var hasDeductAmount = this.calculateCouponDiscountInfo()
            // if (this.selectCoupons[this.changeDiscountCouponIndex].deductAmount - hasDeductAmount <= 0) {
            //     this.$message.error('抵扣金额已用完，无法抵扣其他衣物')
            //     return
            // }
            // if (this.selectCoupons[this.changeDiscountCouponIndex].isUnique == 'Y' && this.changeDiscountCouponSelectCloths.length > 0) {
            //     this.$message.error('该优惠券为单件抵扣，无法抵扣第二件衣物')
            // }
            // var coupon = this.selectCoupons[this.changeDiscountCouponIndex]
            // var cloth = this.changeDiscountCouponCanSelectCloths[index]

            // if (coupon.isUnique == 'Y') {
            //     //单件，只减一件
            //     //抵扣所有衣服里金额最大的
            //     if (cloth.totalPrice - cloth.deductAmount >= coupon.deductAmount) {
            //         this.changeDiscountCouponSelectCloths.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: coupon.deductAmount, clothName: this.calculateClothNameByInfo(cloth), cloth: cloth })
            //         cloth.deductAmount += coupon.deductAmount
            //     } else {
            //         this.changeDiscountCouponSelectCloths.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: cloth.totalPrice - cloth.deductAmount, clothName: this.calculateClothNameByInfo(cloth), cloth: cloth })
            //         cloth.deductAmount = cloth.totalPrice
            //     }
            // } else {
            //     //非单件，可减多件
            //     var deductAmount = this.selectCoupons[this.changeDiscountCouponIndex].deductAmount - hasDeductAmount
            //     if (cloth.totalPrice - cloth.deductAmount >= deductAmount) {
            //         this.changeDiscountCouponSelectCloths.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: deductAmount, clothName: this.calculateClothNameByInfo(cloth), cloth: cloth })
            //         cloth.deductAmount += deductAmount
            //     } else {
            //         this.changeDiscountCouponSelectCloths.push({ clothIndex: cloth.index, id: cloth.id, deductAmount: cloth.totalPrice - cloth.deductAmount, clothName: this.calculateClothNameByInfo(cloth), cloth: cloth })
            //         cloth.deductAmount = cloth.totalPrice
            //     }
            // }

            // this.changeDiscountCouponCanSelectCloths.splice(index, 1)
        },
        submitChangeCouponRelateCloth() {
            // let changeDiscountCouponSelectCloths = [];
            let item = this.changeDiscountCouponCanSelectCloths.find((object) => object.isSelected)
            this.selectCoupons[0].orderCouponClothDTOS = [item]
            // this.changeDiscountCouponCanSelectCloths.forEach((item) => {
            //     this.calculateClothDiscountAmount(item.index)
            // })
            // this.changeDiscountCouponSelectCloths.forEach((item) => {
            //     this.calculateClothDiscountAmount(item.clothIndex)
            // })
            this.orderClothItemDTOS.forEach((e) => {
                e.deductAmount = item.clothItemId == e.id ? item.deductAmount : 0
            })
            this.selectCoupons = this.selectCoupons.concat()
            // this.orderClothItemDTOS = this.orderClothItemDTOS.concat()
            this.changeDiscountCouponOpen = false
        },
        //计算某一件衣服的优惠金额，并配置到orderClothItemDTOS中
        calculateClothDiscountAmount(index) {
            var cloth = this.orderClothItemDTOS[index]
            cloth.deductAmount = 0
            this.selectCoupons.forEach((i) => {
                var selectCloth = i.orderCouponClothDTOS.find((item) => item.clothIndex == cloth.index)
                if (selectCloth) {
                    cloth.deductAmount = cloth.deductAmount + selectCloth.deductAmount
                }
            })
            this.orderClothItemDTOS[index] = cloth
            this.$emit('initOrderList', this.orderClothItemDTOS)
            // this.orderClothItemDTOS = this.orderClothItemDTOS.concat()
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>

<style scoped></style>
