<template>
  <div class="back-form-container">
    <div>
    <el-form :model="query" label-width="80px" :inline="true">
      
        
        
      
        
        
            
            
            
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input v-model="query.couponName" placeholder="请输入优惠券名称"/>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="优惠券描述" prop="couponDesc">
          <el-input v-model="query.couponDesc" placeholder="请输入优惠券描述" type="textarea"/>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="满x" prop="cashBackPoint">
          <el-input-number v-model="query.cashBackPoint" placeholder="请输入满x"/>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="减x" prop="cashBackPrice">
          <el-input-number v-model="query.cashBackPrice" placeholder="请输入减x"/>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="主题可用，id" prop="conditionByTopic">
          <el-select v-model="query.conditionByTopic" placeholder="请选择主题可用，id">
            <el-option
              v-for="item in store.state.dicts.get('')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="类型可用，id" prop="conditionByType">
          <el-select v-model="query.conditionByType" placeholder="请选择类型可用，id">
            <el-option
              v-for="item in store.state.dicts.get('')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="商品可用，id" prop="conditionByGoods">
          <el-select v-model="query.conditionByGoods" placeholder="请选择商品可用，id">
            <el-option
              v-for="item in store.state.dicts.get('')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="发放数量" prop="grantCount">
          <el-input-number v-model="query.grantCount" placeholder="请输入发放数量"/>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="优惠券余量" prop="count">                <el-text class="mx-1">{{query.count}}</el-text>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="领取条件" prop="receiveType">
          <el-input v-model="query.receiveType" placeholder="请输入领取条件"/>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="有效性，领取开始计时(ClaimTiming)，固定时间(FixedTime)" prop="validity">
          <el-select v-model="query.validity" placeholder="请选择有效性，领取开始计时(ClaimTiming)，固定时间(FixedTime)">
            <el-option
              v-for="item in store.state.dicts.get('coupon_validity')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        
      
        
        
            
            
            
        <el-form-item label="有效期" prop="validityPeriod">      <el-date-picker v-model="query.validityPeriod" type="datetime" placeholder="请选择时间" value-format="YYYY-MM-DD hh:mm:ss"/>
        </el-form-item>
        
      
    </el-form>
    </div>
    <div class="back-table-handler">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain size="small" @click="getList(pageNum)">
            <el-icon>
              <RefreshLeft/>
            </el-icon>
            <span>刷新</span>
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain size="small" @click="openDialog('save', GoodsCoupon.Create())">
            <el-icon>
              <Plus/>
            </el-icon>
            <span>新增</span>
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain size="small" @click="deleteByIds()">
            <el-icon>
              <Delete/>
            </el-icon>
            <span>删除</span>
          </el-button>
        </el-col>
      </el-row>
    </div>

    <div class="back-table">
      <el-table v-loading="listLoading" :data="list" @selection-change="selected" border :key="ListKey">
        <el-table-column type="selection" width="40" align="center"/>
        
        <el-table-column label="id" align="center" prop="id"/>
        <el-table-column label="优惠券名称" align="center" prop="couponName"/>
        <el-table-column label="优惠券描述" align="center" prop="couponDesc"/>
        <el-table-column label="满x" align="center" prop="cashBackPoint"/>
        <el-table-column label="减x" align="center" prop="cashBackPrice"/>
        <el-table-column label="主题可用，id" align="center" prop="conditionByTopic"/>
        <el-table-column label="类型可用，id" align="center" prop="conditionByType"/>
        <el-table-column label="商品可用，id" align="center" prop="conditionByGoods"/>
        <el-table-column label="发放数量" align="center" prop="grantCount"/>
        <el-table-column label="优惠券余量" align="center" prop="count"/>
        <el-table-column label="领取条件" align="center" prop="receiveType"/>
      <el-table-column label="有效性，领取开始计时(ClaimTiming)，固定时间(FixedTime)" align="center" prop="validity">
        <template #default="scope">
          <DictTag :dict-type="'coupon_validity'" :dict-value="scope.row.validity"/>
        </template>
      </el-table-column>
        <el-table-column label="有效期" align="center" prop="validityPeriod"/>
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="openDialog('update', scope.row)">修改</el-button>
            <el-button link type="danger" @click="deleteById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
            background
            :current-page="pageNum"
            @current-change="getList"
            :page-size="pageSize"
            :page-sizes="[10, 20, 30, 50,100]"
            :total="total"
            @size-change="SetPageSized"
            layout="total, sizes, prev, pager, next"
        />
      </div>

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="dialogType=='update'?'更改字典类型':'新增字典类型'" v-model="open" width="500px" append-to-body>
          <el-form ref="dictRef" :model="dialogForm" label-width="80px">
            
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input v-model="dialogForm.couponName" placeholder="请输入优惠券名称"/>
        </el-form-item>
        <el-form-item label="优惠券描述" prop="couponDesc">
          <el-input v-model="dialogForm.couponDesc" placeholder="请输入优惠券描述" type="textarea"/>
        </el-form-item>
        <el-form-item label="满x" prop="cashBackPoint">
          <el-input-number v-model="dialogForm.cashBackPoint" placeholder="请输入满x"/>
        </el-form-item>
        <el-form-item label="减x" prop="cashBackPrice">
          <el-input-number v-model="dialogForm.cashBackPrice" placeholder="请输入减x"/>
        </el-form-item>
        <el-form-item label="主题可用，id" prop="conditionByTopic">
          <el-select v-model="dialogForm.conditionByTopic" placeholder="请选择主题可用，id">
            <el-option
              v-for="item in store.state.dicts.get('')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="类型可用，id" prop="conditionByType">
          <el-select v-model="dialogForm.conditionByType" placeholder="请选择类型可用，id">
            <el-option
              v-for="item in store.state.dicts.get('')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="商品可用，id" prop="conditionByGoods">
          <el-select v-model="dialogForm.conditionByGoods" placeholder="请选择商品可用，id">
            <el-option
              v-for="item in store.state.dicts.get('')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="发放数量" prop="grantCount">
          <el-input-number v-model="dialogForm.grantCount" placeholder="请输入发放数量"/>
        </el-form-item>
        <el-form-item label="优惠券余量" prop="count">                <el-text class="mx-1">{{dialogForm.count}}</el-text>
        </el-form-item>
        <el-form-item label="领取条件" prop="receiveType">
          <el-input v-model="dialogForm.receiveType" placeholder="请输入领取条件"/>
        </el-form-item>
        <el-form-item label="有效性，领取开始计时(ClaimTiming)，固定时间(FixedTime)" prop="validity">
          <el-select v-model="dialogForm.validity" placeholder="请选择有效性，领取开始计时(ClaimTiming)，固定时间(FixedTime)">
            <el-option
              v-for="item in store.state.dicts.get('coupon_validity')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="有效期" prop="validityPeriod">      <el-date-picker v-model="dialogForm.validityPeriod" type="datetime" placeholder="请选择时间" value-format="YYYY-MM-DD hh:mm:ss"/>
        </el-form-item>
          </el-form>
          <template #footer>
            <div class="dialog-footer">
              <el-button type="primary" @click="submitForm()">确 定</el-button>
              <el-button @click="open=false">取 消</el-button>
            </div>
          </template>
        </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref} from "vue";
import {
  DeleteBaseGoodsCouponById,
  GetBaseGoodsCouponListByGoodsCoupon,
  SaveBaseGoodsCoupon,
  UpdateBaseGoodsCoupon,
  GoodsCoupon,
  GoodsCouponQuery
} from "../../../api/back/BaseBackApi.ts";
import {ElMessage} from "element-plus";
import {Delete, Plus, RefreshLeft} from "@element-plus/icons-vue";
import router from "../../../router";
import {store} from "../../../state/TokenState.ts";
import DictTag from "../../../components/dict/DictTag.vue";
import MultipleTag from "../../../components/MultipleTag/MultipleTag.vue";
import ImageUpload from "../../../components/ImageUpload/ImageUpload.vue";

let listLoading = ref(true)
let total = ref(0)
let pageNum = ref(1)
let pageSize = ref(10)
let list = ref<GoodsCoupon[]>([])
let ListKey = ref(0)
//搜索用
//let search = ref<GoodsCoupon>(GoodsCoupon.Create())
//修改，添加用
let open = ref(false)
let dialogType = ref("")//save添加，update修改
let dialogForm = ref<GoodsCoupon>(null)
let query = ref<GoodsCouponQuery>(GoodsCouponQuery.Create())
let selectItems = ref<GoodsCoupon[]>([])
const selected = (arr: GoodsCoupon[]) => {
  selectItems.value = arr
}

getList()
const SetPageSized = (val: number) => {
  pageSize.value = val
  getList(pageNum.value)
}

function openDialog(type: string, row: GoodsCoupon) {
  open.value = true
  dialogType.value = type
  if (type==="save"){
    dialogForm.value = GoodsCoupon.Create()
    return
  }
  //深拷贝，不会影响数据
  dialogForm.value = JSON.parse(JSON.stringify(row))
}

function deleteByIds() {
  for (let i = 0; i < selectItems.value.length; i++) {
      deleteById(selectItems[i].id)
  }
}

function deleteById(id) {
  DeleteBaseGoodsCouponById(id).then(response => {
    if (response.code == 200) {
      ElMessage.success("删除成功,id:" + id)
      getList()
    } else {
      ElMessage.error("删除失败,id:" + id)
    }
  })
}

function getList(pn = 1) {
  pageNum.value = pn
  GetBaseGoodsCouponListByGoodsCoupon(query.value, pageNum.value, pageSize.value).then(response => {
    let data = response.data
    total.value = data.total
    list.value = response.data.list
    ListKey.value++
    listLoading.value = false
  })
}

function submitForm() {
  switch (dialogType.value) {
    case "update":
      UpdateBaseGoodsCoupon(dialogForm.value, dialogForm.value.id).then(response => {
        if (response.code == 200) {
          open.value = false
          getList()
          ElMessage.success("修改成功")
        } else {
          ElMessage.error("修改失败")
        }
      })
      break
    case "save":
      SaveBaseGoodsCoupon(dialogForm.value).then(response => {
        if (response.code == 200) {
          open.value = false
          getList()
          ElMessage.success("添加成功")
        } else {
          ElMessage.error("添加失败")
        }
      })
      break
    default:
      break
  }
}
</script>
