<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="recommendNo">
              <el-input v-model="queryParams.recommendNo" placeholder="请输入推荐编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="推荐名称" prop="recommendName">
              <el-input v-model="queryParams.recommendName" placeholder="请输入推荐名称" clearable @keyup.enter="handleQuery" />
            </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">
      <div style="display: flex; justify-content: flex-end; margin-bottom: 16px;">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </div>

      <el-table v-loading="loading" border :data="recommendList">
        <el-table-column label="推荐位ID" align="center" min-width="120" prop="id" />
        <el-table-column label="推荐编号" align="center" min-width="150" prop="recommendNo" />
        <el-table-column label="推荐名称" align="center" min-width="150" prop="recommendName" />
        <el-table-column label="推荐位描述" align="center" min-width="200" prop="recommendDescribe" />
        <el-table-column label="操作" align="center" width="150">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改产品推荐位配置对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="recommendFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="推荐编号" prop="recommendNo">
          <el-input v-model="form.recommendNo" placeholder="请输入推荐编号" />
        </el-form-item>
        <el-form-item label="推荐名称" prop="recommendName">
          <el-input v-model="form.recommendName" placeholder="请输入推荐名称" />
        </el-form-item>
        <el-form-item label="推荐位描述" prop="recommendDescribe">
          <el-input v-model="form.recommendDescribe" type="textarea" :rows="3" placeholder="请输入推荐位描述" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" />
        </el-form-item>
      </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="Recommend" lang="ts">
import { listRecommend, getRecommend, delRecommend, addRecommend, updateRecommend } from '@/api/pmsProduct/recommend';
import { RecommendVO, RecommendQuery, RecommendForm } from '@/api/pmsProduct/recommend/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const recommendList = ref<RecommendVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

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

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: RecommendForm = {
  id: undefined,
  recommendNo: undefined,
  recommendName: undefined,
  recommendDescribe: undefined,
  isShow: undefined,
  recommendCoverphoto: undefined,
  remark: undefined,
}
const data = reactive<PageData<RecommendForm, RecommendQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    recommendNo: undefined,
    recommendName: undefined,
    recommendDescribe: undefined,
    isShow: undefined,
    recommendCoverphoto: undefined,
    platformCode: undefined,
    params: {
    }
  },
  rules: {
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询产品推荐位配置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listRecommend(queryParams.value);
  recommendList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

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

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

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加产品推荐位配置";
}

/** 修改按钮操作 */
const handleUpdate = async (row: RecommendVO) => {
  reset();
  const res = await getRecommend(row.id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改产品推荐位配置";
}

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

/** 删除按钮操作 */
const handleDelete = async (row: RecommendVO) => {
  await proxy?.$modal.confirm('是否确认删除产品推荐位配置编号为"' + row.id + '"的数据项？').finally(() => loading.value = false);
  await delRecommend(row.id);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

onMounted(() => {
  getList();
});
</script>
