<!-- 路线导航管理列表 -->
<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]" id="search_div">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="70px">
            <el-form-item label="行程路线" prop="">
              <el-input class="inputWidth" placeholder="请输入行程路线" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="发布状态" prop="">
              <el-select class="inputWidth" v-model="queryParams.status" placeholder="请选择发布状态" clearable>
                <el-option v-for="dict in note_publish_list" :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-button type="primary" plain icon="Plus" @click="handleAdd"
                v-hasPermi="['system:article:add']">新增</el-button>
              <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
                v-hasPermi="['system:article:edit']">修改</el-button>
              <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
                v-hasPermi="['system:article:remove']">删除</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-table :height="autoTableHeight" v-loading="loading" :data="articleList"
      @selection-change="handleSelectionChange" border>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="线路标题" align="center" prop="" />
      <el-table-column label="线路封面" align="center" prop="" />
      <el-table-column label="线路标签" align="center" prop="" />
      <el-table-column label="行程天数" align="center" prop="" />
      <el-table-column label="行程里数" align="center" prop="" />
      <el-table-column label="行驶时长" align="center" prop="" />
      <el-table-column label="最佳时间" align="center" prop="" />
      <el-table-column label="行程" align="center" prop="" />
      <el-table-column label="加入行程数" align="center" prop="" />
      <el-table-column label="喜欢数" align="center" prop="" />
      <el-table-column label="线路评论数" align="center" prop="" />
      <el-table-column label="发布状态" align="center" prop="" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:article:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:article:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" id="table_page" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
    <!-- 添加或修改景点、租赁管理对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="60%" append-to-body>
      <el-form ref="articleFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="路线标题" prop="isOpen">
          <el-input v-model="form.isOpen" placeholder="请输入路线标题" style="width: 300px;" />
        </el-form-item>
        <el-form-item label="路线封面" prop="attribute">
          <imageUpload :modelValue="form.attribute" :fileSize="20" />
        </el-form-item>
        <el-form-item label="路线标签" prop="province">
          <!-- <el-input v-model="form.province" placeholder="请输入省code" /> -->
          <el-select v-model="form.province" filterable placeholder="请输入要搜索的标签" style="width: 240px">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="涉及的行政区域" prop="city">
          <el-input v-model="form.city" placeholder="请输入市/区" />
        </el-form-item>
        <div style="display: flex;">
          <el-form-item label="行程天数" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入" style="width: 100px;" />天
          </el-form-item>
          <el-form-item label="行程里数" prop="memberLevel">
            <el-input v-model="form.memberLevel" placeholder="请输入" style="width: 100px;" />公里
          </el-form-item>
          <el-form-item label="行驶时长" prop="tagId">
            <el-input v-model="form.tagId" placeholder="请输入" style="width: 100px;" />小时
          </el-form-item>
          <el-form-item label="最佳时间" prop="recommend">
            <el-input v-model="form.recommend" placeholder="请输入" style="width: 100px;" />月
          </el-form-item>
        </div>

        <el-form-item label="添加行程" prop="intro">
          <el-button type="primary" :icon="Plus" @click="add">添加</el-button>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">预 览</el-button>
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">存 草 稿</el-button>
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">提 交</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
        <!-- 添加或修改行程 -->
        <el-dialog :title="addressdialog.title" v-model="addressdialog.visible" width="60%" append-to-body>
      <el-form ref="articleFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="isOpen">
          <el-input v-model="form.isOpen" placeholder="请输入标题" style="width: 300px;" />
        </el-form-item>
        <el-form-item label="描述" prop="attribute">
          <el-input v-model="form.isOpen" placeholder="请输入描述" style="width: 300px;" />
        </el-form-item>
        <el-form-item label="路线标签" prop="province">
          <!-- <el-input v-model="form.province" placeholder="请输入省code" /> -->
          <el-select v-model="form.province" filterable placeholder="请输入要搜索的标签" style="width: 240px">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="涉及的行政区域" prop="city">
          <el-input v-model="form.city" placeholder="请输入市/区" />
        </el-form-item>
        <div style="display: flex;">
          <el-form-item label="行程天数" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入" style="width: 100px;" />天
          </el-form-item>
          <el-form-item label="行程里数" prop="memberLevel">
            <el-input v-model="form.memberLevel" placeholder="请输入" style="width: 100px;" />公里
          </el-form-item>
          <el-form-item label="行驶时长" prop="tagId">
            <el-input v-model="form.tagId" placeholder="请输入" style="width: 100px;" />小时
          </el-form-item>
          <el-form-item label="最佳时间" prop="recommend">
            <el-input v-model="form.recommend" placeholder="请输入" style="width: 100px;" />月
          </el-form-item>
        </div>

        <el-form-item label="添加行程" prop="intro">
          <el-button type="primary" :icon="Plus">添加</el-button>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">预 览</el-button>
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">存 草 稿</el-button>
          <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="Article" lang="ts">
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from '@/api/manage/article';
import { ArticleVO, ArticleQuery, ArticleForm } from '@/api/manage/article/types';
import { Plus} from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const autoTableHeight = ref<number>(750);
const articleList = ref<ArticleVO[]>([]);
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 note_publish_list = ref([
  { label: '已发布', value: 1 },
  { label: '未发布', value: 2 }
]);
const queryFormRef = ref<ElFormInstance>();
const articleFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const addressdialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const initFormData: ArticleForm = {
  id: undefined,
  type: undefined,
  secondType: undefined,
  isOpen: undefined,
  attribute: undefined,
  status: undefined,
  province: undefined,
  city: undefined,
  remark: undefined,
  memberLevel: undefined,
  tagId: undefined,
  recommend: undefined,
  intro: undefined,
  address: undefined,
  longitude: undefined,
  latitude: undefined,
  orderNum: undefined,
  level: undefined
};
const data = reactive<PageData<ArticleForm, ArticleQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: undefined,
    secondType: undefined,
    isOpen: undefined,
    attribute: undefined,
    status: undefined,
    province: undefined,
    city: undefined,
    memberLevel: undefined,
    tagId: undefined,
    recommend: undefined,
    intro: undefined,
    address: undefined,
    longitude: undefined,
    latitude: undefined,
    orderNum: undefined,
    level: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '景点ID/租赁ID/游艇ID不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '类别 景点-0、租赁-1 游艇-2不能为空', trigger: 'change' }],
    secondType: [{ required: true, message: '二级类别不能为空', trigger: 'change' }],
    isOpen: [{ required: true, message: '开放/营业不能为空', trigger: 'blur' }],
    attribute: [{ required: true, message: '景点属性/商家属性不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态 0-未发布 1-审核中 2-发布不能为空', trigger: 'change' }],
    province: [{ required: true, message: '省code不能为空', trigger: 'blur' }],
    city: [{ required: true, message: '市/区不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '个人简介不能为空', trigger: 'blur' }],
    memberLevel: [{ required: true, message: '会员等级不能为空', trigger: 'blur' }],
    tagId: [{ required: true, message: '标签不能为空', trigger: 'blur' }],
    recommend: [{ required: true, message: '推荐语不能为空', trigger: 'blur' }],
    intro: [{ required: true, message: '介绍不能为空', trigger: 'blur' }],
    address: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
    longitude: [{ required: true, message: '经度不能为空', trigger: 'blur' }],
    latitude: [{ required: true, message: '纬度不能为空', trigger: 'blur' }],
    orderNum: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
    level: [{ required: true, message: '景区等级不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);
const options = [
  {
    value: 'Option1',
    label: 'Option1',
  },
  {
    value: 'Option2',
    label: 'Option2',
  },
  {
    value: 'Option3',
    label: 'Option3',
  },
  {
    value: 'Option4',
    label: 'Option4',
  },
  {
    value: 'Option5',
    label: 'Option5',
  },
]
onMounted(() => {
  getList();
  nextTick(() => {
    autoTableHeight.value = proxy?.autoTableHeight();
  });
  window.onresize = () => {
    autoTableHeight.value = proxy?.autoTableHeight();
  };
});
/** 查询景点、租赁管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listArticle(queryParams.value).finally(() => (loading.value = false));
  articleList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: ArticleVO[]) => {
  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?: ArticleVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getArticle(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改路线';
};

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

/** 删除按钮操作 */
const handleDelete = async (row?: ArticleVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除景点、租赁管理编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delArticle(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/article/export',
    {
      ...queryParams.value
    },
    `article_${new Date().getTime()}.xlsx`
  );
};
</script>
<style lang="scss" scoped>
.el-card :deep(.el-card__body) {
  padding-bottom: 0px !important;
}

#table_page {
  height: 50px !important;
  margin-top: 10px !important;
  padding-bottom: 10px !important;
}
</style>
