<template>
  <div>
    <el-form class="dm-wrap" :model="form" ref="form" :rules="rules" label-width="130px">
      <el-form-item label="卡券类型">
        <el-radio-group v-model="form.cardType" :disabled="isEdit || isInfo" @change="reset">
          <el-radio :label="0">抵金券</el-radio>
          <el-radio :label="1">折扣券</el-radio>
          <el-radio :label="2">兑换券</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="卡券适用渠道" prop="cardApplyChannel">
        <el-select v-model="form.cardApplyChannel" multiple class="w400" placeholder="请选择" :disabled="isEdit || isInfo">
          <el-option v-for="item in cardApplyChannelOptions" :key="item.value" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="cardName" label="卡券名称">
        <dm-input v-model="form.cardName" class="w400" placeholder="请输入卡券名称" :disabled="isEdit || isInfo" :byteType="1" :maxlength="9"></dm-input>
      </el-form-item>

      <el-form-item prop="subName" label="备注名">
        <dm-input v-model="form.subName" class="w400" placeholder="请输入备注名称" :disabled="isEdit || isInfo" :byteType="1" :maxlength="10"></dm-input>
      </el-form-item>

      <el-form-item prop="subTitle" label="卡券副标题">
        <dm-input v-model="form.subTitle" class="w400" placeholder="请输入卡券副标题" :disabled="isEdit || isInfo" :byteType="1" :maxlength="18"></dm-input>
      </el-form-item>

      <el-form-item prop="erpDemoCode" label="demo券号">
        <el-input v-model="form.erpDemoCode" class="w400" placeholder="请输入demo券号" :disabled="isEdit || isInfo" @blur="checkErpDemoCode"></el-input>
      </el-form-item>

      <!-- 微盟微商城  微盟券号 -->
      <template v-if="form.cardApplyChannel.indexOf('WMmicroMall') !== -1 && countFlag">
        <el-alert type="warning" show-icon :closable="false" class="f-alert" style="margin-bottom: 10px;margin-left:130px;">
          <span slot="title">选择设置微盟微商城-微盟券号时，卡券信息将以微盟侧为准，本页填写信息将视为无效。</span>
        </el-alert>
        <el-form-item label="微盟券号">
          <div class="mb10" v-for="(saveItem, idx) in weimobDemoCodeList" :key="idx">
            <el-select class="w200" :disabled="saveItem.save || isEdit || isInfo" v-model="saveItem.pid" filterable placeholder="请选择店铺" clearable @change="handleShopChange($event, idx)">
              <el-option :disabled="wmShopIds.includes(item.wmPid)" v-for="item in weimobShopList" :key="item.wmPid" :label="item.wmPidName" :value="item.wmPid"></el-option>
            </el-select>
            <el-select :disabled="!saveItem.pid || saveItem.save || isEdit || isInfo" :loading="saveItem.loading" class="w200" v-model="saveItem.card" filterable placeholder="请选择微盟券号" clearable>
              <el-option v-for="item in saveItem.cardList" :key="item.cardTemplateId" :label="item.name" :value="item.cardTemplateId"></el-option>
            </el-select>
            <template v-if="isAdd || isCopy">
              <el-button type="text" v-if="!saveItem.save" class="ml10" @click="saveWmShop(saveItem, idx)">保存</el-button>
              <el-button type="text" class="ml10" @click="delWmShop(saveItem, idx)">删除</el-button>
            </template>
            <span v-if="saveItem.useCount > 0" class="fz12 gray ml10"><i class="el-icon-warning mr5" style="color:rgb(230, 162, 60);"></i>该券号此前已被选择使用，请确认选择项</span>
          </div>
          <template v-if="isAdd || isCopy">
            <el-button v-if="countFlag == 2 || isAdd || isCopy" :disabled="wmShopAddDisabled || weimobDemoCodeList.length >= weimobShopList.length" type="primary" @click="addWmShop">添加店铺</el-button>
            <span class="fz12 gray ml10">* 店铺不可重复选择，新建卡券仅保存点击保存过的数据</span>
          </template>
        </el-form-item>
      </template>
      <el-form-item prop="cardColor" label="卡券颜色" style="height: 40px;">
        <el-color-picker v-model="form.cardColor" :disabled="isInfo"></el-color-picker> <span class="vertical-top">{{ form.cardColor }}</span>
      </el-form-item>

      <!-- 抵金券 -->
      <el-form-item prop="cardDenomination" label="减免金额" v-if="form.cardType === 0"> <el-input-number controls-position="right" :disabled="isEdit || isInfo" v-model="form.cardDenomination" class="200" :precison="0" :min="0"></el-input-number> 元 <span class="fz12 gray">请输入大于0的整数</span> </el-form-item>

      <!-- 折扣券 -->
      <el-form-item prop="cardDenomination" label="折扣额度" v-if="form.cardType === 1"> <el-input-number controls-position="right" :disabled="isEdit || isInfo" v-model="form.cardDenomination" class="200" :precision="1" :step="0.1" :max="9.9" :min="0"></el-input-number> 折 <span class="fz12 gray">请填写1-9.9之间的数字</span></el-form-item>

      <el-form-item label="优惠金额封顶" v-if="form.cardType === 1"> <el-input-number controls-position="right" :disabled="isEdit || isInfo || discountAmountCheck" v-model="form.discount_amount_upper_limit" class="200" :precision="2" :step="1" :min="0"></el-input-number> 元 &nbsp;<el-checkbox v-model="discountAmountCheck" :disabled="isEdit || isInfo">不封顶</el-checkbox><span class="pl10 fz12 gray vertical-baseline">*该条件目前仅线上可用</span> </el-form-item>

      <el-form-item label="最低适用商品件数"> <el-input-number controls-position="right" :disabled="isEdit || isInfo || goodsDiscountCheckLower" v-model="form.goods_discounts_number_lower_limit" class="200" :precision="0" :step="1" :min="0"></el-input-number> 件 &nbsp;<el-checkbox v-model="goodsDiscountCheckLower" :disabled="isEdit || isInfo">不限</el-checkbox></el-form-item>
      <el-form-item label="最高适用商品件数"> <el-input-number controls-position="right" :disabled="isEdit || isInfo || goodsDiscountCheck" v-model="form.goods_discounts_number_upper_limit" class="200" :precision="0" :step="1" :min="0"></el-input-number> 件 &nbsp;<el-checkbox v-model="goodsDiscountCheck" :disabled="isEdit || isInfo">不限</el-checkbox></el-form-item>

      <!-- 兑换券无 -->
      <el-form-item prop="" label="使用条件">
        <div v-show="form.cardType === 0" class="mb10">
          <el-checkbox :disabled="isEdit || isInfo" v-model="sale_limit.flag">最低消费满</el-checkbox>
          <el-input-number controls-position="right" :min="0" @focus="sale_limit.flag = true" :disabled="isEdit || isInfo" v-model="sale_limit.fee" class="w200"></el-input-number> 元可使用
        </div>
        <div v-show="form.cardType !== 2"><el-checkbox :disabled="isEdit || isInfo" v-model="goods.flag">适用商品</el-checkbox> <span class="fz12 gray">（至少填写一项）</span></div>
        <div class="mb10" v-show="form.cardType !== 2"><span class="inline-block w100 pr10 text-right">适用商品</span> <dm-input @focus="goods.flag = true" v-model="goods.ok" :disabled="isEdit || isInfo" class="w300" placeholder="限制18个汉字内" :maxlength="18"></dm-input></div>
        <div v-show="form.cardType !== 2" class="mb10"><span class="inline-block w100 pr10 text-right">不适用商品</span> <dm-input @focus="goods.flag = true" v-model="goods.no" :disabled="isEdit || isInfo" class="w300" placeholder="限制18个汉字内" :maxlength="18"></dm-input></div>
        <!-- 微盟适用商品 -->
        <div v-loading="wmloading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, 0)">
          <div v-if="(form.cardType === 0 || form.cardType === 1) && form.cardApplyChannel.indexOf('WMmicroMall') !== -1">
            <div v-for="(list, idx) in goodsList" :key="idx" class="mb10" id="wnsysp">
              <span class="inline-block w100 pr10 text-right">
                微盟适用商品
                <span v-show="goodsList.length !== 1">{{ idx + 1 }}</span>
              </span>
              <el-select :disabled="isEdit" class="w200" v-model="list.v1" filterable placeholder=" 选择商品类目" clearable :loading="loading" @change="handleF1Change($event, idx)">
                <el-option v-for="item in f1" :key="item.id" :label="item.title" :value="item.id"></el-option>
              </el-select>
              <el-select v-show="list.f2.length > 0" :disabled="isEdit || list.isCopy" class="w200" v-model="list.v2" filterable remote reserve-keyword placeholder="选择商品参数" clearable :loading="loading" @change="handleF2Change($event, idx)">
                <el-option v-for="item in list.f2" :key="item.id" :label="item.title" :value="item.id"></el-option>
              </el-select>
              <el-select v-if="list.f3.length > 0" :disabled="isEdit || list.isCopy" class="w250" v-model="list.v3" multiple filterable remote reserve-keyword placeholder="选择商品参数值" clearable :loading="loading">
                <el-option v-for="item in list.f3" :key="item.id" :label="item.title" :value="item.id"></el-option>
              </el-select>
              <div v-if="isAdd || isCopy" class="inline-block">
                <el-button v-if="list.isCopy" type="warning" icon="el-icon-edit" circle @click="getLine(idx)"></el-button>
                <el-button v-show="idx === goodsList.length - 1 && goodsList.length < 10" type="primary" icon="el-icon-plus" circle @click="addLine"></el-button>
                <el-button icon="el-icon-delete" circle @click="delLine(idx)"></el-button>
              </div>
            </div>
            <el-alert type="warning" show-icon :closable="false" class="f-alert">
              <span slot="title">
                微盟适用商品数据来源于微盟侧商品及商品参数，请确保填写正确。
                <b v-if="latestReptileTime">最近同步时间 {{ latestReptileTime }}</b>
              </span>
            </el-alert>
          </div>
        </div>
        <!-- 线下适用商品 -->
        <div v-if="(form.cardType === 0 || form.cardType === 1) && form.cardApplyChannel.indexOf('offlineStore') !== -1 && xxSysp.goodsEnable">
          <span class="inline-block w100 pr10 text-right">线下适用商品</span>
          <div class="sysp__content clearfix inline-block">
            <div class="fl" v-if="xxSysp.filterAbbrInfo.length">
              <template v-for="(item, idx) in xxSysp.filterAbbrInfo">
                {{ item.optName }}
                <span :key="idx" class="item">{{ item.text }}</span>
              </template>
            </div>
            <template v-if="xxSysp.goodsEnable && (((isEdit || isInfo) && xxSysp.filterAbbrInfo.length) || isAdd || isCopy)">
              <el-button type="primary" @click="handleXxSyspClick" class="fl mt5" size="mini">{{ isEdit || isInfo ? '查看' : '编辑' }}</el-button>
              <el-button v-show="xxSysp.filterAbbrInfo.length && !isEdit && !isInfo" @click="handleXxSyspReset" class="fl mt5" size="mini">清空</el-button>
            </template>
            <p v-else class="fz14 gray">未选择</p>
          </div>
          <div v-if="xxSysp.goodsEnable && (((isEdit || isInfo) && xxSysp.filterAbbrInfo.length) || isAdd || isCopy)" class="tips fz12 gray"><span class="w100 pr10 inline-block"></span>* 仅支持第三方api接口核销卡券时使用</div>
        </div>
        <!-- 兑换券 -->
        <div v-show="form.cardType === 2" class="mb10"><el-checkbox :disabled="isEdit || isInfo" v-model="sale_limit.flag">消费条件</el-checkbox> <span class="fz12 gray">（至少填写一项）</span></div>
        <div class="mb10" v-show="form.cardType === 2">
          <el-radio v-model="sale_limit.type" :label="1" :disabled="isEdit || isInfo">
            消费金额满
            <el-input-number
              controls-position="right"
              @focus="
                sale_limit.type = 1;
                sale_limit.flag = true;
              "
              :disabled="isEdit || isInfo"
              :precision="1"
              :min="0"
              class="w300"
              v-model="sale_limit.fee"
            ></el-input-number>
            元可使用
          </el-radio>
        </div>
        <div v-show="form.cardType === 2" class="mb10">
          <el-radio v-model="sale_limit.type" :label="2" :disabled="isEdit || isInfo">
            消费指定商品
            <el-input
              @focus="
                sale_limit.type = 2;
                sale_limit.flag = true;
              "
              :disabled="isEdit || isInfo"
              class="w300"
              v-model="sale_limit.goods"
            ></el-input>
            可使用
            <div class="pl25 pt10">指定商品货号 <dm-goods-input :disabled="isEdit || isInfo" class="w300 inline-block" v-model="form.proNoList" :multiple="false"></dm-goods-input></div>
            <div class="pl25 pt10" v-show="products_exchange_number_show">兑换商品件数 <el-input-number controls-position="right" :disabled="isEdit || isInfo" v-model="form.products_exchange_number" class="200" :precision="0" :step="1" :min="0"></el-input-number></div>
          </el-radio>
        </div>
        <!-- 只有线下门店展示适用商品折扣 -->
        <div v-if="form.cardApplyChannel.indexOf('offlineStore') !== -1">
          <el-checkbox v-model="discount_limit.flag" :disabled="isEdit || isInfo">适用商品折扣</el-checkbox>
          <!-- 1: 大于等于 2:大于  3: 等于 4: 小于 5: 小于等于 -->
          <div v-if="discount_limit.flag" class="inline-block">
            <el-select :disabled="isEdit || isInfo" v-model="discount_limit.type" class="w100 ml10">
              <el-option label="大于等于" :value="1"></el-option>
              <el-option label="大于" :value="2"></el-option>
              <el-option label="等于" :value="3"></el-option>
              <el-option label="小于" :value="4"></el-option>
              <el-option label="小于等于" :value="5"></el-option>
            </el-select>
            <el-input-number controls-position="right" :disabled="isEdit || isInfo" v-model="discount_limit.count" :precision="2" :min="0" :max="10" class="w150"></el-input-number>&nbsp;&nbsp;折
          </div>
          <span class="fz12 gray ml5">* 仅支持第三方api接口核销卡券时使用</span>
        </div>
        <div>
          <el-checkbox v-model="only" :disabled="isEdit || isInfo">不与其它优惠共享</el-checkbox>
          <span class="fz12 gray ml5">* 仅用于微信原生卡券详情展示，无实际业务逻辑</span>
        </div>
      </el-form-item>
      <el-form-item prop="cardLimit" label="领券限制">
        <el-radio-group v-model="cardLimitRadio" :disabled="isEdit || isInfo">
          <el-radio :label="1">1张/人</el-radio>
          <el-radio :label="100">100张/人</el-radio>
          <el-radio :label="0"> <el-input-number controls-position="right" :min="0" class="w150" v-model="form.cardLimit" :disabled="isEdit || isInfo"></el-input-number> 张/人 <span class="fz13 gray">每个用户领券上限，默认为1</span> </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="costValue" label="成本费用" v-show="form.cardType !== 1"> <el-input-number controls-position="right" :disabled="isEdit || isInfo" :precision="2" :min="0" class="w200" v-model="form.costValue"></el-input-number> 元 <span class="fz13 gray">请填写大于0的数字（限制两位小数）</span> </el-form-item>
      <el-form-item label="code类型">
        <el-radio v-model="form.useCustomCode" :label="0" :disabled="isEdit || isInfo">随机生成</el-radio>
        <el-radio v-model="form.useCustomCode" :label="1" :disabled="isEdit || isInfo">自定义号段</el-radio>
      </el-form-item>

      <el-form-item prop="customCodeBegin" label="code号段" v-show="form.useCustomCode === 1">
        <el-input :disabled="isEdit || isInfo" :min="0" :maxlength="16" class="w250" v-model="form.customCodeBegin" @blur="checkInputNumber(form.customCodeBegin, 'customCodeBegin')"></el-input> —
        <el-input :disabled="isEdit || isInfo" :min="0" :maxlength="16" class="w250" v-model="form.customCodeEnd" @blur="checkInputNumber(form.customCodeEnd, 'customCodeEnd')"></el-input>
        <span class="fz13 gray">限制16位以内纯数字</span>
        <div class="pt20">
          <el-checkbox v-model="form.useCodePrefix" :true-label="1" :false-label="0" :disabled="isEdit || isInfo">固定开头</el-checkbox>
          <el-input class="w400" :disabled="isEdit || isInfo" v-model="form.customCodePrefix" :maxlength="6" @focus="form.useCodePrefix = 1" @blur="checkInputString(form.customCodePrefix, 'customCodePrefix')"></el-input>
          <span class="fz13 gray">限制6位以内字母/数字</span>
        </div>
        <div class="pt20">
          <el-checkbox v-model="form.useCodeSuffix" :true-label="1" :false-label="0" :disabled="isEdit || isInfo">固定结尾</el-checkbox>
          <el-input :disabled="isEdit || isInfo" class="w400" v-model="form.customCodeSuffix" :maxlength="4" @focus="form.useCodeSuffix = 1" @blur="checkInputString(form.customCodeSuffix, 'customCodeSuffix')"></el-input>
          <span class="fz13 gray">限制4位以内字母/数字</span>
        </div>
      </el-form-item>

      <el-form-item prop="cardCodeCount" :label="form.useCustomCode ? '默认数量' : '新建数量'">
        <div v-show="!form.useCustomCode"><el-input-number controls-position="right" :min="0" :max="10000000" :disabled="isEdit || isInfo" class="w200" v-model="form.cardCodeCount"></el-input-number> 张 <span class="fz13 gray">优惠券创建数量，如不填，则默认为100,000张</span></div>
        <div v-show="form.useCustomCode"><el-input-number controls-position="right" :min="0" :max="100000" disabled class="w200" :value="form.customCodeEnd - form.customCodeBegin + 1"></el-input-number> 张 <span class="fz13 gray">优惠券创建数量，如不填，则默认为100,000张</span></div>
      </el-form-item>

      <el-form-item class="is-required" prop="useCustomCode" label="有效期">
        <div>
          <el-radio v-model="form.cardEffectiveMode" :disabled="isEdit || isInfo || cardValidity" :label="0">固定日期</el-radio>
          <el-date-picker v-model="form.dateTime" @focus="form.cardEffectiveMode = 0" :disabled="isEdit || isInfo || cardValidity" :picker-options="pickerOptions" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </div>
        <div class="pt20">
          <el-radio v-model="form.cardEffectiveMode" :label="1" :disabled="isEdit || isInfo || cardValidity">领取后，</el-radio>
          <el-select v-model="form.startDay" @focus="form.cardEffectiveMode = 1" class="w200" placeholder="请选择" :disabled="isEdit || isInfo || cardValidity">
            <el-option v-for="item in startDayOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          生效，有效天数 <el-input-number controls-position="right" class="w200" @focus="form.cardEffectiveMode = 1" :disabled="isEdit || isInfo || cardValidity" v-model="form.limitDay" :precision="0" :min="1" :max="isEdit || isInfo ? 100000000 : 1200"></el-input-number> 天
        </div>
      </el-form-item>

      <el-form-item prop="cardExplain" label="适用门店说明">
        <dm-input class="w400" v-model="form.cardExplain" :disabled="isInfo" :maxlength="50"></dm-input>
      </el-form-item>

      <el-form-item label="适用门店">
        <el-radio :disabled="isEdit" v-model="storeMode" :label="0">所有门店</el-radio>
        <el-radio :disabled="isEdit" v-model="storeMode" :label="1">部分门店</el-radio>
        <span v-if="isEdit" class="fz12 gray ml20">编辑适用门店前会员领取该卡券不包含新增的适用门店；编辑适用门店后会员领取该卡券包含新增的适用门店。</span>
        <!-- :readonly="isEdit"  -->
        <vue-gic-store-card class="pt10" v-bind="storeParams" v-show="storeMode" ref="storeCard" :uuid.sync="uuid"></vue-gic-store-card>
        <el-alert v-show="storeMode && form.cardApplyChannel.indexOf('WMmicroMall') !== -1" title="选择的门店必须保持与微盟侧门店一致，请确认选择无误" type="warning" show-icon :closable="false" class="mt20 width80"></el-alert>
      </el-form-item>

      <el-form-item v-if="giveEnable && form.useCustomCode != 1" label="卡券转赠">
        <el-switch :disabled="isEdit" v-model="form.giveFlag" :active-value="1" :inactive-value="0"></el-switch>
        <span class="tips fz12 gray ml5">* code类型为自定义号段暂时不支持卡券转赠，转赠多次会消耗相同次数的库存，请设置足够的安全库存</span>
      </el-form-item>

      <el-form-item prop="image" label="(选填)封面图片">
        <dm-upload-img :disabled="isEdit || isInfo" :model.sync="form.image" width="64" label="上传图片" url="/api-plug/upload-img" tips="图片建议尺寸：850px*350px，大小不超过2M"></dm-upload-img>
      </el-form-item>

      <el-form-item prop="coverDescript" label="(选填)封面简介">
        <dm-input class="w400" v-model="form.coverDescript" :disabled="isEdit || isInfo" :maxlength="12"></dm-input>
      </el-form-item>

      <el-form-item prop="useDescript" label="使用说明">
        <dm-input class="w400" type="textarea" :rows="4" v-model="form.useDescript" :maxlength="500" :disabled="isInfo"></dm-input>
      </el-form-item>

      <el-form-item prop="teletexts" label="(选填)图文介绍">
        <p class="fz13 gray">图片建议尺寸：900px*500px，大小不超过2M，格式限制JPG；至少上传1组图文，最多输入5000字</p>
        <img-text-drag :list.sync="imgTextList" :disabled="form.auditingStatus === 1"></img-text-drag>
      </el-form-item>

      <!-- <el-form-item label="放入卡券展架" v-show="cardLimitRadio === 1" v-loading="cardShelfsLoading">
        <el-transfer :disabled="isInfo" filterable :filter-method="filterMethod" filter-placeholder="输入展架名称搜索" v-model="form.shelfIds" :titles="['所有门店展架', '选中的展架']" :data="cardShelfsList"> </el-transfer>
      </el-form-item> -->

      <el-form-item prop="verificationType" label="核销方式" v-show="form.cardType === 2">
        <el-radio v-model="form.verificationType" :label="0" :disabled="isEdit || isInfo">会员自助核销</el-radio>
        <el-radio v-model="form.verificationType" :label="1" :disabled="isEdit || isInfo">收银台录入核销</el-radio>
      </el-form-item>

      <div class="btn-wrap_fixed" :class="{ on: asideShow }">
        <el-button type="primary" @click="submitForm('form')" v-loading="submitLoading" v-show="!isInfo && canEdit">保存并提交审核</el-button>
        <el-button @click="$router.go(-1)">返 回</el-button>
      </div>
    </el-form>

    <el-dialog title="适用商品" :visible.sync="xxSysp.visible" width="690px">
      <vue-gic-goods-selector ref="selector" :projectName="requestProject"></vue-gic-goods-selector>
      <div slot="footer" class="dialog-footer">
        <el-button @click="xxSysp.visible = false">{{ isEdit ? '关闭' : '取消' }}</el-button>
        <el-button v-if="isAdd || isCopy" type="primary" @click="xxSyspConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import cardForm from './form.js';
export default cardForm;
</script>

<style lang="scss" scoped>
.f-alert {
  width: 780px !important;
  /deep/ .el-alert__content {
    height: 18px;
    line-height: 18px;
  }
}
</style>
