<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="任务名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入任务名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="任务状态" prop="category">
              <el-select v-model="queryParams.category" placeholder="请选择任务状态" clearable>
                <el-option v-for="dict in mk_jigsaw_task_catagory" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['manage:mkJigsaw:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="mkJigsawList">
        <el-table-column label="任务名称" align="center" prop="name" />
        <el-table-column label="任务类别" align="center" prop="category">
          <template #default="scope">
            <dict-tag :options="mk_jigsaw_task_catagory" :value="scope.row.category" />
          </template>
        </el-table-column>
        <el-table-column label="商品ID" align="center" prop="productId" />
        <el-table-column label="商品名称" align="center" prop="productName" />
        <el-table-column label="商品类别" align="center" prop="productCategory" />
        <el-table-column label="商品图片" align="center" prop="productImage" />
        <el-table-column label="活动时间" align="center" prop="startDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startDate, '{y}.{m}.{d}') }}~{{ parseTime(scope.row.endDate, '{y}.{m}.{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="拼图数量" align="center" prop="jigsawConfigCount" />
        <el-table-column label="设置得奖人数" align="center" prop="awardConfigCount" />
        <el-table-column label="参与人数" align="center" prop="joinCount" />
        <el-table-column label="实际得奖人数" align="center" prop="awardCount" />
        <el-table-column label="拼图碎片获得人数" align="center" prop="awardCount">
          <template #default="scope">
            <el-button type="primary" link>查看详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="拼图详情" align="center" prop="awardCount">
          <template #default="scope">
            <el-button type="primary" link>查看详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="发布状态" align="center" prop="status">
          <template #default="scope">
            <span>{{ scope.row.status }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="awardCount" />
        <el-table-column label="创建时间" align="center" prop="awardCount" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['manage:mkJigsaw:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['manage:mkJigsaw:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改拼图游戏对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="50%" append-to-body>
      <el-form ref="mkJigsawFormRef" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入任务名称" style="width: 300px" />
        </el-form-item>
        <el-form-item label="任务类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择任务类别" style="width: 300px">
            <el-option v-for="dict in mk_jigsaw_task_catagory" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择商品" prop="productId">
          <el-input v-model="form.productId" placeholder="请选择商品" style="width: 300px" />
        </el-form-item>
        <el-form-item label="活动时间" prop="startDate">
          <el-date-picker v-model="form.startDate" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设置拼图数量" prop="jigsawConfigCount">
          <el-input v-model="form.jigsawConfigCount" placeholder="请输入拼图配置数量" style="width: 300px" disabled />
        </el-form-item>
        <el-form-item label="得奖人数" prop="awardConfigCount">
          <el-input v-model="form.awardConfigCount" placeholder="请输入得奖人数" style="width: 300px" />
        </el-form-item>
        <el-form-item label="控制得奖拼图" prop="controlJigsawIndex">
          <el-select v-model="form.controlJigsawIndex" placeholder="请选择控制得奖拼图" style="width: 300px" @change="selectnumlist">
            <el-option v-for="dict in numlist" :key="dict.label" :label="dict.label" :value="dict.label"></el-option>
          </el-select>
        </el-form-item>
        <div class="numlistclasss">
          <div v-for="item in numlistdata" :key="item.name">
            <el-form-item :label="`${item.name}：可获得概率`">
              <el-input v-model="item.probability" placeholder="请输入概率" style="width: 100px" />%
            </el-form-item>
          </div>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MkJigsaw" lang="ts">
import { listMkJigsaw, getMkJigsaw, delMkJigsaw, addMkJigsaw, updateMkJigsaw } from '@/api/manage/mkJigsaw';
import { MkJigsawVO, MkJigsawQuery, MkJigsawForm } from '@/api/manage/mkJigsaw/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { mk_jigsaw_task_catagory } = toRefs<any>(proxy?.useDict('mk_jigsaw_task_catagory'));

const mkJigsawList = ref<MkJigsawVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const mkJigsawFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const numlist = ref([
  { value: '1', label: '片数1' },
  { value: '2', label: '片数2' },
  { value: '3', label: '片数3' },
  { value: '4', label: '片数4' },
  { value: '5', label: '片数5' },
  { value: '6', label: '片数6' },
  { value: '7', label: '片数7' },
  { value: '8', label: '片数8' },
  { value: '9', label: '片数9' }
]);
const numlistdata = ref([
  { probability: '', name: '片数1' },
  { probability: '', name: '片数2' },
  { probability: '', name: '片数3' },
  { probability: '', name: '片数4' },
  { probability: '', name: '片数5' },
  { probability: '', name: '片数6' },
  { probability: '', name: '片数7' },
  { probability: '', name: '片数8' },
  { probability: '', name: '片数9' }
]);
const initFormData: MkJigsawForm = {
  id: undefined,
  name: undefined,
  category: undefined,
  productId: undefined,
  productName: undefined,
  startDate: undefined,
  endDate: undefined,
  jigsawConfigCount: 9,
  awardConfigCount: undefined,
  controlJigsawIndex: undefined,
  joinCount: undefined,
  awardCount: undefined,
  status: undefined
};
const data = reactive<PageData<MkJigsawForm, MkJigsawQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    category: undefined,
    productId: undefined,
    productName: undefined,
    startDate: undefined,
    endDate: undefined,
    jigsawConfigCount: undefined,
    awardConfigCount: undefined,
    controlJigsawIndex: undefined,
    joinCount: undefined,
    awardCount: undefined,
    status: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: 'ID不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
    category: [{ required: true, message: '任务类别 0-普适拼图任务 1-心愿拼图任务不能为空', trigger: 'change' }],
    productId: [{ required: true, message: '商品ID不能为空', trigger: 'blur' }],
    productName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' }],
    startDate: [{ required: true, message: '活动开始时间不能为空', trigger: 'blur' }],
    endDate: [{ required: true, message: '活动结束时间不能为空', trigger: 'blur' }],
    jigsawConfigCount: [{ required: true, message: '拼图配置数量不能为空', trigger: 'blur' }],
    awardConfigCount: [{ required: true, message: '得奖配置人数不能为空', trigger: 'blur' }],
    controlJigsawIndex: [{ required: true, message: '控制得奖拼图不能为空', trigger: 'blur' }],
    joinCount: [{ required: true, message: '参与人数不能为空', trigger: 'blur' }],
    awardCount: [{ required: true, message: '实际得奖人数不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '发布状态 0-未发布 1-已发布不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询拼图游戏列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMkJigsaw(queryParams.value);
  mkJigsawList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  mkJigsawFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: MkJigsawVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加拼图任务';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: MkJigsawVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getMkJigsaw(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改拼图任务';
};

/** 提交按钮 */
const submitForm = () => {
  mkJigsawFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateMkJigsaw(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addMkJigsaw(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: MkJigsawVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除拼图游戏编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delMkJigsaw(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'manage/mkJigsaw/export',
    {
      ...queryParams.value
    },
    `mkJigsaw_${new Date().getTime()}.xlsx`
  );
};
const selectnumlist = () => {
   numlistdata.value = ([
    { probability: '', name: '片数1' },
    { probability: '', name: '片数2' },
    { probability: '', name: '片数3' },
    { probability: '', name: '片数4' },
    { probability: '', name: '片数5' },
    { probability: '', name: '片数6' },
    { probability: '', name: '片数7' },
    { probability: '', name: '片数8' },
    { probability: '', name: '片数9' }
  ]);
  console.log(form.value.controlJigsawIndex);
  numlistdata.value = numlistdata.value.filter((i) => i.name !== form.value.controlJigsawIndex);
};

onMounted(() => {
  getList();
});
</script>
<style>
.numlistclasss {
  display: flex;
  flex-wrap: wrap;
  > div {
    width: 33%;
  }
}
</style>
