<!--
 * @Descripttion: (优惠券/tb_coupon)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-07-24)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-07-24)
-->
<template>
  <div class="app-container">
    <el-row :gutter="24">
      <!-- 搜索框 queryParams.需要搜索的字段 -->
      <el-form :model="queryParams" label-position="left" style="margin:15px;" inline ref="queryForm" v-show="showSearch"
        @submit.prevent>
        <el-form-item label="优惠劵名称" prop="couponName">
          <el-input v-model="queryParams.couponName" placeholder="请输入优惠劵名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="优惠劵类型" prop="couponType">
          <el-select v-model="queryParams.couponType" placeholder="请选择优惠劵类型" clearable @change="handleQuery">
            <el-option v-for="item in  coupon_type " :key="item.dictValue" :label="item.dictLabel"
              :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示状态" prop="couponDisplayStatus">
          <el-radio-group v-model="queryParams.couponDisplayStatus">
            <el-radio v-for="item in  display_status " :key="item.dictValue" :label="item.dictValue">{{ item.dictLabel
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <!-- 工具按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:coupon:addOrUpdate']" plain icon="plus"
          @click="AddDialogVisible = true">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:coupon:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="Upload" @click="UploadDialogVisible = true"
          v-hasPermi="['business:coupon:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['business:coupon:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格渲染  prop="对应的字段"-->
    <el-table v-loading="loading" :data="dataList" ref="tableRef" border highlight-current-row
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />

      <el-table-column prop="couponName" width="350" label="优惠劵名称" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="couponType" label="优惠劵类型" align="center">
        <template #default="scope">
          <dict-tag :options="coupon_type" :value="scope.row.couponType" />
        </template>
      </el-table-column>
      <el-table-column prop="couponEffectiveDays" label="有效天数" align="center" />
      <el-table-column prop="couponSendNumber" label="发送数量" align="center">
        <template #default="scope">
          <div v-if="scope.row.couponSendNumber == -1">不限量</div>
          <div v-else>{{ scope.row.couponSendNumber }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="couponGetNumber" label="领取数量" align="center" />
      <el-table-column prop="couponDisplayStatus" label="显示状态" align="center">
        <template #default="scope">
          <dict-tag :options="display_status" :value="scope.row.couponDisplayStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="couponSort" label="排序" align="center" sortable />

      <el-table-column label="操作" width="350" fixed="right">
        <template #default="scope">
          <el-button type="primary" size="small" icon="edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:coupon:addOrUpdate']">编辑</el-button>
          <el-button type="danger" size="small" icon="delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:coupon:delete']">删除</el-button>
          <el-button size="small" icon="view" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>

  <!-- 添加 -->
  <AddDialog v-model="AddDialogVisible" :done="() => resetQuery()"></AddDialog>
  <!-- 编辑 -->
  <EditDialog v-model="EditDialogVisible" :data="EditDialogRow" :done="() => resetQuery()"></EditDialog>
  <!-- 详情 -->
  <DetailDialog v-model="DetailDialogVisible" :data="DetailDialogRow" :done="() => resetQuery()"></DetailDialog>
  <!-- 导入 -->
  <UploadDialog v-model="UploadDialogVisible" :done="() => resetQuery()"></UploadDialog>
</template>
<script setup name="coupon">
import { ElMessageBox } from 'element-plus'
import modal from '@/plugins/modal.js'
import { exportCoupon, couponList, delCoupon } from '@/api/business/Marketing/CouponManage/Coupons/coupon.js'
import AddDialog from "./components/AddDialog.vue";
import EditDialog from "./components/EditDialog.vue";
import DetailDialog from "./components/DetailDialog.vue";
import UploadDialog from "./components/UploadDialog.vue";

const AddDialogVisible = ref(false);
const EditDialogVisible = ref(false);
const EditDialogRow = ref({});
const DetailDialogVisible = ref(false);
const DetailDialogRow = ref({});
const UploadDialogVisible = ref(false);

const { proxy } = getCurrentInstance()

// 选中categoryId数组数组
const ids = ref([])
// 非单选禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 数据列表
const dataList = ref([])
// 总记录数
const total = ref(0)
// 是否加载
const loading = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
})
const { queryParams } = toRefs(data)

// 业务参数


// 业务方法

// 字典获取
const coupon_type = ref([]);
async function getcoupon_type() {
  await proxy.getDicts('coupon_type').then((res) => {
    coupon_type.value = res.data
  })
}
getcoupon_type()
// 字典获取
const coupon_expire_type = ref([]);
async function getcoupon_expire_type() {
  await proxy.getDicts('coupon_expire_type').then((res) => {
    coupon_expire_type.value = res.data
  })
}
// getcoupon_expire_type()
// 字典获取
const applicable_scope = ref([]);
async function getapplicable_scope() {
  await proxy.getDicts('applicable_scope').then((res) => {
    applicable_scope.value = res.data
  })
}
// getapplicable_scope()
// 字典获取
const display_status = ref([]);
async function getdisplay_status() {
  await proxy.getDicts('display_status').then((res) => {
    display_status.value = res.data
  })
}
getdisplay_status()




//基础方法

// 查询数据
function getList() {
  loading.value = true


  couponList(queryParams.value).then((res) => {
    if (res.code == 200) {
      loading.value = false;
      dataList.value = res.data.result;
      total.value = res.data.totalNum;

      dataList.value.forEach(item => {
        if (item.couponGoodsIdsArr.length != - 0) {
          item.couponGoodsIds = item.couponGoodsIdsArr.split(',')
        }
        if (item.couponApplicableScopeConfig) {
          item.couponApplicableScopeConfig = JSON.parse(item.couponApplicableScopeConfig)
        }
        if (item.couponFixedStartTime && item.couponFixedEndTime) {
          item.couponFixedTime = []
          item.couponFixedTime.push(item.couponFixedStartTime)
          item.couponFixedTime.push(item.couponFixedEndTime)
          // console.log(item.couponFixedTime,'列表的捏');
        }
      });
    }
  });
}


// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.couponId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置查询操作 */
function resetQuery() {
  proxy.resetForm('queryForm')
  handleQuery()
}
/** 搜索按钮操作 */
function handleQuery() {
  getList()
}
/** 删除按钮操作 */
function handleDelete(row) {
  const Ids = row.couponId || ids.value

  ElMessageBox.confirm("是否确认删除？", "系统提示", {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: "warning",
  })
    .then(function () {
      return delCoupon(Ids)
    })
    .then(() => {
      handleQuery()
      modal.msgSuccess("删除成功")
    })
    .catch(() => { })
}

/** 导出按钮操作 */
function handleExport(row) {
  const Ids = row.couponId || ids.value
  const name = ref("所有")

  if (Ids.length != 0) {
    let str = ''
    for (const key in Ids) {
      str += Ids[key] + ','
    }
    str = str.slice(0, str.length - 1)
    queryParams.value.ids = str
    name.value = "选中"
  }

  ElMessageBox.confirm('是否确认导出' + name.value + '数据?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(function () {
      return exportCoupon(queryParams.value)
    })
    .then((response) => {
      proxy.download(response.data.path)
    })
}


// 修改
function handleUpdate(row) {
  EditDialogVisible.value = true
  EditDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailDialogVisible.value = true
  DetailDialogRow.value = row
}

handleQuery()
</script>
