<template>
    <el-container>
        <el-main>
            <div class="main-body">
                <div class="body-left">
                    <div class="body-left-title">商品编辑</div>
                    <div class="body-form">
                        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="140px" label-position="right">
                            <el-card shadow="never" header="商品类型" v-loading="channelLoad">
                                <el-form-item label="商品品类" prop="category">
                                    <el-cascader ref="cascaderRef" v-model="formData.category" :options="categoryData" :props="deptsProps" @change="getTemplate" clearable style="width: 50%;" />
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                                <el-form-item label="商品类型" prop="product_type" v-if="typeData">
                                    <el-radio-group v-model="formData.product_type">
                                        <el-radio border :label="item.key" v-for="(item,index) in typeData" :key="index">{{item.name}}</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-card>
                            <template v-if="showNext">
                            <el-card shadow="never" header="商品信息">
                                <el-form-item label="商品名称" prop="product_name">
                                    <el-input v-model="formData.product_name" maxlength="40" show-word-limit clearable placeholder="请输入"></el-input>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                                <el-row :gutter="15">
                                    <el-col :span="formData.product_type==11?8:12">
                                        <el-form-item label="顾客实际需支付" prop="price">
                                            <el-input v-model="formData.price" type="number" clearable placeholder="请输入">
                                                <template #append>元</template>
                                            </el-input>
                                            <div class="el-form-item-msg"></div>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="formData.product_type==11?8:12">
                                        <el-form-item label="划线价" prop="line_price">
                                            <el-input v-model="formData.line_price" type="number" clearable placeholder="请输入">
                                                <template #append>元</template>
                                            </el-input>
                                            <div class="el-form-item-msg"></div>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="8" v-if="formData.product_type==11">
                                        <el-form-item label="差价金额" prop="custom_price">
                                            <el-input v-model="formData.custom_price" type="number" clearable placeholder="请输入">
                                                <template #append>元</template>
                                            </el-input>
                                            <div class="el-form-item-msg"></div>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <fieldset>
                                    <legend><el-tag>商品搭配</el-tag></legend>
                                    <el-button type="primary" @click="addSpecs" v-if="specs.length<5">添加分组</el-button>
                                    <div class="specs" v-for="(item,indx) in specs" :key="indx">
                                        <div class="specs-header">
                                            <div class="specs-left">
                                                <el-input v-model="item.name" placeholder="请输入商品组名称" maxlength="10" show-word-limit clearable style="width: 50%;"></el-input>
                                            </div>
                                            <div class="specs-right">
                                                <el-select v-model="item.num" placeholder="请选择">
                                                    <el-option :value="0" label="全部可选"></el-option>
                                                    <el-option :value="len" :label="item.list.length+'选'+len" v-for="len in item.list.length" v-if="item.list.length > 1"></el-option>
                                                </el-select>
                                            </div>
                                        </div>
                                        <div class="specs-tips">商品组名称中不可出现“任选N”“X选N”字样，当商品组的可选范围是“几选1”或“全部可选”时，不可配置重复选</div>
                                        <div class="specs-body">
                                            <div class="body-item" v-for="(its,ind) in item.list" :key="ind">
                                                <div class="item-left">
                                                    <el-row :gutter="10">
                                                        <el-col :span="12" class="mb10">
                                                            <el-input placeholder="请输入单品名称 " v-model="its.name">
                                                                <template #prepend>名称</template>
                                                            </el-input>
                                                        </el-col>
                                                        <el-col :span="12" class="mb10">
                                                            <el-input placeholder="单价" v-model="its.price" type="number">
                                                                <template #prepend>单价</template>
                                                                <template #append>元</template>
                                                            </el-input>
                                                        </el-col>
                                                        <el-col :span="12" class="mb10">
                                                            <el-input placeholder="请输入重量 " v-model="its.weight" type="number">
                                                                <template #prepend>重量</template>
                                                                <template #append>
                                                                    <el-select v-model="its.weight_unit">
                                                                        <el-option value="kg" label="kg(千克)"></el-option>
                                                                        <el-option value="g" label="g(克)"></el-option>
                                                                        <el-option value="L" label="L(升)"></el-option>
                                                                        <el-option value="ml" label="ml(毫升)"></el-option>
                                                                        <el-option value="m" label="m(米)"></el-option>
                                                                        <el-option value="m²" label="m²(平方米)"></el-option>
                                                                        <el-option value="m³" label="m³(立方米)"></el-option>
                                                                    </el-select>
                                                                </template>
                                                            </el-input>
                                                        </el-col>
                                                        <el-col :span="12" class="mb10">
                                                            <el-input placeholder="数量" v-model="its.number" type="number">
                                                                <template #prepend>数量</template>
                                                                <template #append>份</template>
                                                            </el-input>
                                                        </el-col>
                                                    </el-row>
                                                </div>
                                                <div class="item-dels">
                                                    <el-button type="danger" @click="removeItem(indx,ind)" circle icon="el-icon-delete" size="small"></el-button>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="specs-footer">
                                            <el-button size="small" type="primary" @click="addItem(indx)">增加单品</el-button>
                                            <el-button size="small" type="danger" @click="removeSpec(indx)">删除组</el-button>
                                        </div>
                                    </div>
                                </fieldset>
                                <!-- <el-form-item label="多规格" prop="mult_sku">
                                    <el-radio-group v-model="formData.mult_sku">
                                        <el-radio border :label="1">关闭</el-radio>
                                        <el-radio border :label="2">开启</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item> -->
                                <fieldset v-if="formData.mult_sku == 2">
                                    <legend><el-tag>规格明细</el-tag></legend>
                                    <el-button type="primary" @click="addSkuSpecs" v-if="skuSpecs.length<5">新增规格</el-button>
                                    <div class="sku-table">
                                        <el-table :data="skuSpecs" border>
                                            <el-table-column prop="image" label="规格图片" width="180">
                                                <template #default="scope">
                                                    <sc-upload v-model="scope.row.image" title="规格图片" :cropper="true" :compress="1" :aspectRatio="1/1"></sc-upload>
                                                </template>
                                            </el-table-column>
                                            <el-table-column prop="sku_id" label="商品ID(必填)" width="220">
                                                <template #default="scope">
                                                    <el-input v-model="scope.row.sku_id" placeholder="商品ID"></el-input>
                                                </template>
                                            </el-table-column>
                                            <el-table-column prop="name" label="名称(必填)" width="220">
                                                <template #default="scope">
                                                    <el-input v-model="scope.row.name" maxlength="40" show-word-limit placeholder="SKU名称"></el-input>
                                                </template>
                                            </el-table-column>
                                            <el-table-column prop="line_price" label="划线价(必填)" width="160">
                                                <template #default="scope">
                                                    <el-input v-model="scope.row.line_price" type="number" placeholder="划线价">
                                                        <template #prefix>¥</template>
                                                    </el-input>
                                                </template>
                                            </el-table-column>
                                            <el-table-column label="支付价(必填)" width="160">
                                                <template #default="scope">
                                                    <el-input v-model="scope.row.price" :disable="true" readonly placeholder="支付价">
                                                        <template #prefix>¥</template>
                                                    </el-input>
                                                </template>
                                            </el-table-column>
                                            <el-table-column prop="custom_price" label="差价(必填)" width="160" v-if="formData.product_type == 11">
                                                <template #default="scope">
                                                    <el-input v-model="scope.row.custom_price" type="number" placeholder="差价">
                                                        <template #prefix>¥</template>
                                                    </el-input>
                                                </template>
                                            </el-table-column>
                                            <el-table-column label="操作" align="left" fixed="right">
                                                <template #default="scope">
                                                    <el-button type="danger" size="small" @click="removeItemSku(scope.$index)" icon="el-icon-delete">删除</el-button>
                                                    <el-button size="small" @click="itemSkuView(scope.$index,scope.row)">规格详情</el-button>
                                                </template>
                                            </el-table-column>
                                        </el-table>
                                    </div>
                                </fieldset>
                                <el-form-item label="商品头图" prop="image_list">
                                    <sc-upload-multiple v-model="formData.image_list" :multiple="false" draggable :limit="10" tip="图片尺寸不超过5M，图片越小呈现的速度越快，建议分辨率不低于750*562px"></sc-upload-multiple>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                                <el-form-item label="辅助图" prop="detail_image_list">
                                    <sc-upload-multiple v-model="formData.detail_image_list" :multiple="false" draggable :limit="4" tip="辅助图上传1-4张，每张图片不得超过5M，建议分辨率不低于750*562px"></sc-upload-multiple>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                                <el-form-item label="环境图" prop="environment_image_list">
                                    <sc-upload-multiple v-model="formData.environment_image_list" :multiple="false" draggable :limit="10" tip="良好的环境也是用户的优先选择，最多可上传10张。每张图片不得超过5M，建议分辨率不低于750*562px"></sc-upload-multiple>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-card>
                            <el-card shadow="never" header="售卖信息">
                                <el-form-item label="投放渠道" prop="show_channel">
                                    <el-radio-group v-model="formData.show_channel">
                                        <el-radio :label="1">不限制</el-radio>
                                        <el-popover placement="top-start" trigger="hover" content="用户仅可在直播间开播时支付购买，售价及库存均仅在直播间渠道生效。用户无法通过收藏或分享的商品链接支付购买。如希望以此售价通过多渠道分享和售卖，投放渠道需选择“不限制”。" :width="280">
                                            <template #reference>
                                                <el-radio :label="2">
                                                    <template #default>
                                                        <div class="radio-flex">仅直播间展示 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                        <el-popover placement="top-start" trigger="hover" content="用户仅可通过线下二维码等物料购买，商品不会在线上任何渠道展示和售卖。" :width="280">
                                            <template #reference>
                                                <el-radio :label="5">
                                                    <template #default>
                                                        <div class="radio-flex">仅线下 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                        <el-popover placement="top-start" trigger="hover" content="新用户只可在新人频道购买，售价及库存仅在新人频道生效，用户无法通过收藏或分享的商品链接支付购买。" :width="280">
                                            <template #reference>
                                                <el-radio :label="7">
                                                    <template #default>
                                                        <div class="radio-flex">仅新人频道 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                        <el-popover placement="top-start" trigger="hover" content="用户可通过线上所有渠道购买（除新人频道），但不可通过线下二维码物料购买。" :width="280">
                                            <template #reference>
                                                <el-radio :label="8">
                                                    <template #default>
                                                        <div class="radio-flex">仅线上 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                        <el-popover placement="top-start" trigger="hover" content="用户仅可在免费试频道购买,免费价格及库存仅针对参与免费试活动条件的用户生效。" :width="280">
                                            <template #reference>
                                                <el-radio :label="18">
                                                    <template #default>
                                                        <div class="radio-flex">仅免费试 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                        <el-popover placement="top-start" trigger="hover" content="用户仅可在「团购」页面看到和购买商品。" :width="280">
                                            <template #reference>
                                                <el-radio :label="22">
                                                    <template #default>
                                                        <div class="radio-flex">仅团购商城 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                        <el-popover placement="top-start" trigger="hover" content="商品售卖日期内，用户仅在直播间和获客卡渠道可支付购买，售价及库存均仅在直播间和获客卡渠道生效。" :width="280">
                                            <template #reference>
                                                <el-radio :label="23">
                                                    <template #default>
                                                        <div class="radio-flex">直播间+获客卡 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                        <el-popover placement="top-start" trigger="hover" content="该渠道商品不会在常规渠道售卖，需报名参加营销活动进行售卖。" :width="280">
                                            <template #reference>
                                                <el-radio :label="25">
                                                    <template #default>
                                                        <div class="radio-flex">仅活动报名 <el-icon><el-icon-question-filled /></el-icon></div>
                                                    </template>
                                                </el-radio>
                                            </template>
                                        </el-popover>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                                <el-form-item label="库存数量" prop="limit_use_rule">
                                    <el-radio-group v-model="formData.limit_use_rule">
                                        <el-radio :label="2">不限制</el-radio>
                                        <el-radio :label="1">限库存</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                    <div class="date-list" v-if="formData.limit_use_rule==1">
                                        <el-form-item label="" prop="use_num_per_consume">
                                            <el-input v-model="formData.use_num_per_consume">
                                                <template #append>份</template>
                                                <template #prepend>团购库存数量</template>
                                            </el-input>
                                        </el-form-item>
                                    </div>
                                </el-form-item>
                                <el-form-item label="商品售卖日期" prop="sold_start_times">
                                    <div style="width: 30%;">
                                        <el-date-picker v-model="formData.sold_start_times" placeholder="请选择商品售卖日期" start-placeholder="开始时间" end-placeholder="结束时间" type="daterange" range-separator="至" />
                                    </div>
                                    <div class="el-form-item-msg">顾客可以在这个时间内购买商品，超过这个日期，活动自动下线</div>
                                    <div class="radio-flex">
                                        自动延期<el-switch active-text="开" inactive-text="关" inline-prompt v-model="formData.auto_renew" style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" :active-value="1" :inactive-value="0"/>
                                        <span class="fs12">团购到期后自动延长30天（有效期同时延长），可随时关闭。</span>
                                    </div>
                                </el-form-item>
                            </el-card>
                            <el-card shadow="never" header="交易规则">
                                <el-form-item label="顾客可消费日期" prop="use_date_type" @change="use_date_type_change">
                                    <el-radio-group v-model="formData.use_date_type">
                                        <el-radio :label="1">指定天数</el-radio>
                                        <el-radio :label="2">指定日期</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg" v-if="formData.use_date_type==1"></div>
                                    <div class="el-form-item-msg" v-if="formData.use_date_type==2">
                                        可使用日期的结束时间必须晚于售卖日期的结束时间
                                    </div>
                                    <div class="date-list" v-if="formData.use_date_type==1">
                                        <el-input v-model="formData.day_duration">
                                            <template #append>天，购买当日默认可用</template>
                                            <template #prepend>自购买次日起</template>
                                        </el-input>
                                    </div>
                                    <div class="date-list" v-if="formData.use_date_type==2">
                                        <el-date-picker v-model="formData.use_times" placeholder="请选择商品售卖日期" start-placeholder="开始时间" end-placeholder="结束时间" type="daterange" range-separator="至" />
                                    </div>
                                </el-form-item>
                                <el-form-item label="顾客信息设置" prop="customer_reserved_info">
                                    <el-radio-group v-model="formData.customer_reserved_info" disabled>
                                        <el-radio :label="1">不需要</el-radio>
                                        <el-radio :label="2">需要顾客手机号码</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                                <el-form-item label="顾客不可消费日期" prop="can_no_use_date">
                                    <el-radio-group v-model="formData.can_no_use_date">
                                        <el-radio :label="1">所有日期均可使用</el-radio>
                                        <el-radio :label="2">部分日期不可用</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                    <template v-if="formData.can_no_use_date==2">
                                        <div class="date-list">
                                            <div class="date-type-item">
                                                <div class="item-header"><el-checkbox v-model="formData.can_no_use_week" :value="1" label="每周几不可用" /></div>
                                                <div class="item-body" style="width: 500px;" v-if="formData.can_no_use_week">
                                                    <el-form-item label="" prop="no_use_weeks">
                                                        <el-select v-model="formData.no_use_weeks" multiple placeholder="请选择" style="width:100%">
                                                            <el-option v-for="(item,index) in weeksData" :key="index" :label="item.name" :value="item.key"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </div>
                                            </div>
                                            <!-- <div class="date-type-item">
                                                <div class="item-header"><el-checkbox v-model="formData.can_no_use_holiday" :value="1" label="节假日不可用" /></div>
                                                <div class="item-body" style="width: 500px;" v-if="formData.can_no_use_holiday">
                                                    <el-form-item label="" prop="no_use_holidays">
                                                        <el-select v-model="formData.no_use_holidays" multiple placeholder="请选择" style="width:100%">
                                                            <el-option v-for="(item,index) in holiday" :key="index" :label="item.name" :value="item.key"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </div>
                                            </div> -->
                                            <div class="date-type-item">
                                                <div class="item-header"><el-checkbox v-model="formData.can_no_use_day" :value="1" label="指定某天不可用" /></div>
                                                <div class="item-body" style="width: 500px;" v-if="formData.can_no_use_day">
                                                    <el-form-item label="" prop="no_use_days">
                                                        <el-date-picker
                                                            v-model="formData.no_use_days"
                                                            type="dates"
                                                            style="width:100%"
                                                            placeholder="请选择日期"
                                                        />
                                                    </el-form-item>
                                                </div>
                                            </div>
                                        </div>
                                    </template>
                                </el-form-item>
                                <!-- <el-form-item label="券码类型" prop="code_source_type">
                                    <el-radio-group v-model="formData.code_source_type">
                                        <el-radio :label="1">抖音码</el-radio>
                                        <el-radio :label="2">三方码</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item> -->
                            </el-card>
                            <el-card shadow="never" header="消费规则">
                                <!-- <el-form-item label="使用规则">
                                    <div class="el-form-item-msg">
                                        <el-button @click="addRule">添加规则</el-button>
                                    </div>
                                    <div class="date-list" style="margin-top: 10px;" v-if="notificationData.length > 0">
                                        <div class="date-type-item flex" v-for="(item,index) in notificationData" :key="index">
                                            <div class="left">
                                                <el-input v-model="item.title" placeholder="请输入标题" />
                                                <el-input v-model="item.content" placeholder="请输入规则内容" />
                                            </div>
                                            <div class="right">
                                                <el-button type="danger" size="small" @click="removeRule(index)" circle icon="el-icon-delete"></el-button>
                                            </div>
                                        </div>
                                    </div>
                                </el-form-item> -->
                                <el-form-item label="限购规则" prop="limit_rule_type">
                                    <el-radio-group v-model="formData.limit_rule_type">
                                        <el-radio :label="1">不限制购买</el-radio>
                                        <el-radio :label="2">限制购买</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                    <div class="date-list" v-if="formData.limit_rule_type==2">
                                        <el-input v-model="formData.limit_rule">
                                            <template #append>份</template>
                                            <template #prepend>限制每人最多购买</template>
                                        </el-input>
                                    </div>
                                </el-form-item>
                                <el-form-item label="预约规则" prop="booking_type">
                                    <el-radio-group v-model="formData.booking_type">
                                        <el-radio :label="1">无需预约</el-radio>
                                        <el-radio :label="2">到店前需要预约</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                    <div class="date-list" v-if="formData.booking_type==2">
                                        <el-input v-model="formData.booking_date">
                                            <template #prepend>需提前</template>
                                            <template #append>
                                                <el-select v-model="formData.booking_unit" style="width: 80px;">
                                                    <el-option :value="1" label="天"></el-option>
                                                    <el-option :value="2" label="小时"></el-option>
                                                </el-select>
                                            </template>
                                        </el-input>
                                    </div>
                                </el-form-item>
                                <el-form-item label="使用张数限制" prop="rec_person_type">
                                    <el-radio-group v-model="formData.rec_person_type">
                                        <el-radio :label="1">不限制张数</el-radio>
                                        <el-radio :label="2">限制张数</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg"></div>
                                    <div class="date-list" v-if="formData.rec_person_type==2">
                                        <el-input v-model="formData.rec_person_num_max">
                                            <template #prepend>单次消费最多使用张数</template>
                                            <template #append>张</template>
                                        </el-input>
                                    </div>
                                </el-form-item>
                                <el-form-item label="库存方式" prop="stock_type">
                                    <el-radio-group v-model="formData.stock_type">
                                        <el-radio :label="1">付款减库存</el-radio>
                                        <el-radio :label="2">核销减库存</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg">
                                        <span v-if="formData.stock_type==1">用户在小程序支付成功后，已售数量增加，库存减少，不限库存不受影响</span>
                                        <span v-if="formData.stock_type==2">用户在完成订单核销后，已售数量增加，库存减少，不限库存不受影响</span>
                                    </div>
                                </el-form-item>
                            </el-card>
                            <el-card shadow="never" header="其他详情">
                                <el-form-item label="详情内容" prop="content">
                                    <sc-editor v-model="formData.content" placeholder="请输入" :height="800"></sc-editor>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-card>
                            </template>
                        </el-form>
                    </div>
                </div>
                <div class="body-right">
                    <div class="sub-tips">该功能只做预览，实际效果以真机为准</div>
                    <div class="preview-mobile">
                        <div class="mobile-bannber">
                            <el-carousel :interval="5000" arrow="always">
                                <el-carousel-item v-for="item in formData.image_list" :key="item">
                                    <el-image :src="item.url"></el-image>
                                </el-carousel-item>
                            </el-carousel>
                        </div>
                        <div class="mobile-info">
                            <div class="info-price">
                                <span class="price">{{ formData.price?$TOOL.moneyFormat(formData.price):'¥0.00' }}</span>
                                <span class="line">{{ formData.line_price?$TOOL.moneyFormat(formData.line_price):'¥0.00' }}</span>
                                <span class="dis">{{ $TOOL.disFormat(formData.price,formData.line_price) }}折</span>
                            </div>
                            <div class="info-title">{{ formData.product_name?formData.product_name:'演示商品标题' }}</div>
                            <div class="sku-list" v-if="skuSpecs.length > 0">
                                <div class="sku-title">规格<br>选择</div>
                                <div class="sku-list-body">
                                    <div class="sku-item" v-for="(item,indx) in skuSpecs" :key="indx">{{item.name}}</div>
                                </div>
                            </div>
                            <div class="item-tag">
                                <div class="tag-li"><el-icon color="#f00" size="14"><el-icon-circle-check /></el-icon>过期退</div>
                                <div class="tag-li"><el-icon color="#f00" size="14"><el-icon-circle-check /></el-icon>随时退</div>
                            </div>
                        </div>
                        <div class="mobile-group">
                            <div class="group-title">团购套餐</div>
                            <div class="group-body" v-if="specs.length > 0" v-for="(item,indx) in specs" :key="indx">
                                <div class="body-title">
                                    {{item.name}}
                                    <span v-if="item.num == 0">全部可选</span>
                                    <span v-else>{{item.list.length}}选{{item.num}}</span>
                                </div>
                                <div class="body-item">
                                    <div class="body-item-li" v-for="(its,idx) in item.list" :key="idx">
                                        {{its.name}}
                                        <div class="item-right">
                                            <span>({{its.number}}份)</span>
                                            <span>{{$TOOL.moneyFormat(its.price)}}</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="mobile-group">
                            <div class="group-title">购买须知</div>
                            <div class="group-mix">
                                <div class="mix-title">
                                    <el-icon size="14" color="#000"><el-icon-suitcase /></el-icon>有效期
                                </div>
                                <div class="mix-item" v-if="formData.use_date_type==1">
                                    购买后 <span>{{ formData.day_duration }}</span> 天有效
                                </div>
                                <div class="mix-item" v-if="formData.use_date_type==2">
                                    <span v-if="formData.use_times">
                                    {{ $TOOL.dateFormat(formData.use_times[0],'yyyy-MM-dd') }}至{{ $TOOL.dateFormat(formData.use_times[1],'yyyy-MM-dd') }}
                                    </span>
                                </div>
                            </div>
                            <div class="group-mix">
                                <div class="mix-title">
                                    <el-icon size="14" color="#000"><el-icon-clock /></el-icon>可用时间
                                </div>
                                <div class="mix-item" v-if="formData.can_no_use_date==1">
                                    商家营业时间可用
                                </div>
                            </div>
                            <div class="group-mix">
                                <div class="mix-title">
                                    <el-icon size="14" color="#000"><el-icon-alarm-clock /></el-icon>预约规则
                                </div>
                                <div class="mix-item" v-if="formData.booking_type==1">
                                    无需预约，高峰期可能需要排队
                                </div>
                                <div class="mix-item" v-if="formData.booking_type==2">
                                    需要提前<span>{{ formData.booking_date }}</span>{{ formData.booking_unit==1?'天':'小时' }}预约
                                </div>
                            </div>
                        </div>
                        <div class="mobile-group content-data">
                            <div v-html="formData.content"></div>
                        </div>
                    </div>
                </div>
            </div>
        </el-main>
        <el-footer style="text-align: center;">
            <el-button @click="reback" size="large">取 消</el-button>
            <el-button size="large" type="primary" :loading="isSaveing" @click="submit()">提交审核</el-button>
        </el-footer>
    </el-container>
    <skuView ref="skuView" @success="skuResp"></skuView>
</template>

<script>
import dayjs from 'dayjs';
import skuView from './components/sku'
import { defineAsyncComponent } from 'vue';
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
    components:{
        skuView,scEditor
    },
    data(){
        return {
            isSaveing:false,
            showNext:false,
            formData:{
                settle_type:2,
                show_channel:1,
                limit_use_rule:2,
                customer_reserved_info:2,
                auto_renew:1,
                use_date_type:1,
                day_duration:180,
                can_no_use_date:1,
                code_source_type:1,
                limit_rule_type:1,
                limit_rule:1,
                booking_date:1,
                booking_type:1,
                booking_unit:1,
                rec_person_type:1,
                rec_person_num_max:1,
                image_list:[],
                detail_image_list:[],
                environment_image_list:[],
                mult_sku:1,
                stock_type:1
            },
            holiday:[{'key':1,"name":"元旦"},{'key':2,"name":"春节"},{'key':3,"name":"清明"},{'key':4,"name":"劳动节"},{'key':5,"name":"端午节"},{'key':6,"name":"中秋节"},{'key':7,"name":"国庆节"},{'key':8,"name":"情人节"},{'key':9,"name":"圣诞节"},{'key':10,"name":"元宵节"},{'key':11,"name":"520"},{'key':12,"name":"儿童节"},{'key':13,"name":"七夕节"},{'key':14,"name":"二月二"},{'key':15,"name":"母亲节"},{'key':16,"name":"父亲节"},{'key':17,"name":"教师节"}],
            weeksData:[{'key':1,"name":"星期一"},{'key':2,"name":"星期二"},{'key':3,"name":"星期三"},{'key':4,"name":"星期四"},{'key':5,"name":"星期五"},{'key':6,"name":"星期六"},{'key':7,"name":"星期天"},],
            notificationData:[{"title":"","content":""}],
            specs:[{"name":"","num":0,"list":[ {"name":"","price":1,"weight":"1","weight_unit":'kg',"number":1} ]}],
            skuSpecs:[],
            channelLoad:false,
            rules:{
                custom_price: [
                    {required: true, message: '请输入'}
                ],
                no_use_weeks: [
                    {required: true, message: '请选择'}
                ],
                no_use_holidays: [
                    {required: true, message: '请选择'}
                ],
                no_use_days: [
                    {required: true, message: '请选择'}
                ],
                customer_reserved_info: [
                    {required: true, message: '请选择'}
                ],
                line_price: [
                    {required: true, message: '请输入'}
                ],
                price: [
                    {required: true, message: '请输入'}
                ],
                image_list: [
                    {required: true, message: '请上传'}
                ],
                rec_person_type: [
                    {required: true, message: '请选择'}
                ],
                code_source_type: [
                    {required: true, message: '请选择'}
                ],
                can_no_use_date: [
                    {required: true, message: '请选择'}
                ],
                use_date_type: [
                    {required: true, message: '请选择'}
                ],
                product_name: [
                    {required: true, message: '请输入'}
                ],
                use_num_per_consume: [
                    {required: true, message: '请输入'}
                ],
                limit_use_rule: [
                    {required: true, message: '请选择'}
                ],
                sold_start_times: [
                    {required: true, message: '请选择'}
                ],
                show_channel: [
                    {required: true, message: '请选择'}
                ],
                settle_type: [
                    {required: true, message: '请选择'}
                ],
                category: [
                    {required: true, message: '请选择'}
                ],
                product_type: [
                    {required: true, message: '请选择'}
                ],
                stock_type: [
                    {required: true, message: '请选择'}
                ],
            },
            deptsProps:{
                value: "category_id",
                label: "name"
            },
            typeData:null,
            categoryData:[]
        }
    },
    created(){
        this.getData()
        this.formData.sold_start_times = this.formattedCurrentDate();
    },
    methods: {
        use_date_type_change(){
            if (this.formData.use_date_type == 2) {
                this.formData.use_times = this.formattedCurrentDate();
            }
        },
        addRule(){
            this.notificationData.push({"title":"","content":""});
        },
        removeRule(index){
            if (this.notificationData.length <= 1) {
                return this.$message.error("至少保留一条规则")
            }
            this.notificationData.splice(index,1)
        },
        skuResp(data){
            this.skuSpecs[data.index] = data.data;
        },
        itemSkuView(index,data){
            this.$nextTick(() => {
                this.$refs.skuView.open("edit").setData(index,data)
            })
        },
        addSkuSpecs(){
            if(!this.formData.price) return this.$message.error("请先设置顾客实际需支付金额");
            this.skuSpecs.push({"image":"","sku_id":this.generate(),"name":"","line_price":1,"custom_price":1,"price":this.formData.price,"is_default":false,"specs":[]});
        },
        removeItemSku(index){
            this.skuSpecs.splice(index,1)
        },
        addSpecs(){
            if (this.specs.length >= 5) {
                return this.$message.error("最多只能添加三组规格")
            }
            this.specs.push({"name":"","num":0,"list":[ {"name":"","price":1,"weight":"1","weight_unit":'kg',"number":1} ]});
        },
        addItem(index){
            if (this.specs[index].list.length >= 20) {
                return this.$message.error("最多只能添加20个")
            }
            this.specs[index].list.push({"name":"","price":1,"weight":"1","weight_unit":'kg',"number":1})
        },
        removeSpec(index){
            this.specs.splice(index,1)
        },
        removeItem(index,sub){
            this.specs[index].list.splice(sub,1)
        },
        generate() {
            let timestamp = Date.now();
            if (timestamp === this.lastTimestamp) {
                this.sequence++;
            } else {
                this.sequence = 0;
                this.lastTimestamp = timestamp;
            }
            const randomPart = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
            const timestampPart = timestamp.toString().slice(-6);   // 取时间戳后6位
            const seqPart = this.sequence.toString().padStart(2, '0'); // 序列号占2位（0-99）
            let base = timestampPart + seqPart + randomPart; // 6+2+4=12位
            const remainingLength = 18 - base.length;
            const extraRandom = Math.floor(Math.random() * Math.pow(10, remainingLength)).toString().padStart(remainingLength, '0');
            return base + extraRandom; // 最终18位字符串
        },
        formattedCurrentDate() {
            const now = dayjs();
            const currentDate = now.format('YYYY-MM-DD HH:mm:ss');
            const nextYearDate = now.add(360, 'day').format('YYYY-MM-DD HH:mm:ss');
            return [currentDate,nextYearDate];
        },
        async getTemplate(v){
            const nodes = this.$refs.cascaderRef.getCheckedNodes()
            var typeData = this.$TOOL.findNodeById(this.categoryData,nodes[0].value);
            if (!typeData) return this.$message.error("获取商品类型失败")
            this.typeData = typeData.types;
            this.showNext = true;
            // var { formData, $message } = this;
            // if (formData.product_type && formData.category) {
            //     // var resp = await this.$API.merGoods.template.post({"product_type":formData.product_type,"category":formData.category});
            //     this.showNext = true;
            //     return ;
            // }
        },
        async getData(){
            this.channelLoad = true;
            var resp = await this.$API.merGoods.category.get();
            this.channelLoad = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg)
            }
            this.categoryData = resp.data;
            // this.getCate()
        },
        async getCate(){
            var resp = await this.$API.merGoods.type.get();
            this.channelLoad = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg)
            }
            this.typeData = resp.data;
        },
        reback(){
            this.$confirm(`确定要退出添加商品吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.$router.go(-1)
            }).catch(() => {

            })
        },
        async submit(){
            var { formData, $message } = this;
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            // if (this.notificationData.length == 0) {
            //     return this.$message.error("使用规则至少需要一组以上");
            // }
            formData.specs = this.specs;
            // formData.notification = this.notificationData;
            formData.skuSpecs = this.skuSpecs;
            if (formData.sold_start_times) {
                var sold_start_time = formData.sold_start_times;
                formData.sold_start_time = this.$TOOL.dateFormat(sold_start_time[0],"yyyy-MM-dd")
                formData.sold_end_time = this.$TOOL.dateFormat(sold_start_time[1],"yyyy-MM-dd")
            }
            if (formData.use_times) {
                var use_time = formData.use_times;
                formData.use_time_start = this.$TOOL.dateFormat(use_time[0],"yyyy-MM-dd")
                formData.use_time_end = this.$TOOL.dateFormat(use_time[1],"yyyy-MM-dd")
            }
            this.isSaveing = true;
            var resp = await this.$API.merGoods.save.post(formData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return $message.error(resp.msg)
            }
            $message.success(resp.msg)
            this.$router.go(-1)
        }
    }
}
</script>


<style scoped>
.main-body{display: flex;height: 100%;gap: 20px;}
.main-body .reason{height: calc(100% - 75px);}
.main-body .body-left{background-color: #fff;flex: 1 1 0%;}
.body-left-title{height: 60px;line-height: 40px;padding: 10px;font-size: 16px;color: #000;font-weight: bold;border-bottom: 1px solid #f8f8f8;}
.main-body .body-form{height: calc(100% - 80px);overflow: auto;padding: 10px;}
.main-body .body-right{width: 400px;background-color: #fff;}
.main-body .sub-tips{height: 40px;line-height: 40px;text-align: center;font-size: 14px;}
.main-body .preview-mobile{height: calc(100% - 60px);overflow: auto;padding: 10px;background-color: #f8f8f8;}

.group-mix .mix-title{display: flex;align-items: center;line-height: 32px;font-weight: bold;font-size: 12px;gap: 5px;}
.group-mix .mix-item{padding-left: 10px;position: relative;line-height: 32px;margin-left: 10px;}
.group-mix .mix-item span{color: orange;padding: 0 5px;}
.group-mix .mix-item::after{position: absolute;top: 0;width: 5px;height: 5px;background-color: #000;border-radius: 5px;content: "";left: 0;top: 38%;}
.mobile-group{background-color: #fff;margin-top: 10px;padding: 10px;}
.mobile-group .group-title{font-size: 14px;font-weight: bold;color: #333;line-height: 36px;}
.mobile-group .body-title{display: flex;align-items: center;line-height: 32px;font-size: 14px;color: #333;font-weight: bold;gap: 8px;}
.mobile-group .body-title span{font-size: 12px;font-weight: normal;color: #666;}
.mobile-group .body-item-li{display: flex;align-items: center;justify-content: space-between;line-height: 32px;font-size: 12px;color: #333;font-weight: bold;}
.mobile-group .body-item-li .item-right{display: flex;align-items: center;gap: 10px;}

.content-data {
    /deep/ img{
        width: 100%;
        height: 100%;
    }
}
.el-card{margin-bottom: 20px;}

.date-list{background-color: var(--el-fill-color-light);padding: 10px;border-radius: 2px;min-width: 500px;}
.date-type-item{margin-bottom: 15px;}
.date-type-item.flex{display: flex;align-items: center;gap: 10px;justify-content: space-between;}
.date-type-item.flex .left{flex: 1;}

.mobile-bannber{background-color: #f8f8f8;}
.mobile-info{padding: 10px;background-color: #fff;}
.mobile-info .info-title{font-size: 14px;font-weight: bold;line-height: 36px;}
.mobile-info .info-price{display: flex;align-items: flex-end;gap: 8px;}
.mobile-info .info-price .price{color: #f00;font-size: 22px;font-weight: bold;}
.mobile-info .info-price .line{color: #666;font-size: 12px;text-decoration:line-through;}
.mobile-info .info-price .dis{background-color: rgb(255, 234, 234);;color: rgb(255, 68, 68);;padding: 5px;border-radius: 2px;}
.mobile-info .item-tag{display: flex;align-items: center;gap: 10px;font-size: 12px;}
.mobile-info .item-tag .tag-li{display: flex;align-items: center;gap: 5px;}
.mobile-info .sku-list{display: flex;align-items: center;margin: 10px 0;}
.mobile-info .sku-list .sku-title{color: #999090;font-size: 12px;}
.mobile-info .sku-list .sku-list-body{display: flex;align-items: center;margin-left: 8px;flex: 1;overflow: hidden;overflow-x: auto;gap: 8px;}
.mobile-info .sku-list .sku-list-body .sku-item{background-color: #f6fafd;padding: 8px 16px;border-radius: 8px;text-wrap: nowrap;}

.preview-mobile{margin-top: 10px;box-shadow: 0 20px 30px 0 rgb(63 63 65 / 6%);border-radius: 10px;border: 1px solid #f8f8f8;box-sizing: border-box;background-color: #f8f8f8;}
.radio-flex{display: flex;align-items: center;gap: 5px;}
.fs12{color: #999;font-size: 12px;}

.specs{margin-top: 15px;background-color: #f8f8f8;}
.specs-header{
    display: flex;
    background-color: #ddd;
    padding: 10px;
}
.specs-tips{
    padding: 0 10px 10px 10px;
    background-color: #ddd;
}
.specs-header .specs-left{
    display: flex;
    align-items: center;
    flex: 1;
    gap: 10px;
}
.specs-header .specs-right{
    display: flex;
    margin-left: auto;
    margin-right: 0;
}
.specs-body{display: flex;align-items: center;gap: 10px;padding: 10px;flex-direction: row;flex-wrap: wrap;}
.specs-body .body-item{padding: 5px;display: flex;align-items: center;}
.specs-body .body-item .item-dels{margin-left: 10px;}
.item-img{margin-bottom: 5px;}
.mb10{margin-bottom: 10px;}
.specs-footer{background-color: #ddd;padding: 10px;}
.sku-table{margin-top: 10px;}
.el-upload__tip,
.el-upload-list__item,
.el-dialog__wrapper {
    z-index: 9999 !important;
}
</style>
