<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

          <!-- <el-input
                  v-model="input3"
                  placeholder="请输入查询内容"
                  class="input-with-select"
                  style="width: 250px;margin-top:-17px"
          >
            <template #prepend>
              <el-select v-model="select" placeholder="搜索" style="width: 75px">
                <el-option label="题名" value="1" />
                <el-option label="国别" value="2" />
                <el-option label="年份" value="3" />
              </el-select>
            </template>
            <template #append>
              <el-button :icon="Search" />
            </template>
          </el-input> -->

<!--        <el-form-item label="年份" prop="year">-->
<!--          <el-select v-model="queryParams.year" placeholder="请选择年份" clearable style="width: 150px;">-->
<!--            <el-option-->
<!--              v-for="dict in year_type"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="月份" prop="month">-->
<!--          <el-select v-model="queryParams.month" placeholder="请选择月份" clearable style="width: 150px;">-->
<!--            <el-option-->
<!--              v-for="dict in month_type"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="区域" prop="nation">
        <el-input
          v-model="queryParams.nation"
          placeholder="请输入区域"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input
            v-model="queryParams.author"
            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-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
          >导出</el-button>
        </el-col> -->
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="studyNationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="标题" align="center" prop="title" />
        <el-table-column label="区域" align="center" prop="nation" />
        <el-table-column label="封面图片" align="center" prop="coverPath" width="120">
          <template #default="scope">
            <image-preview :src="scope.row.coverPath" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="年份" align="center" prop="year">
          <template #default="scope">
            <dict-tag :options="year_type" :value="scope.row.year"/>
          </template>
        </el-table-column>
        <el-table-column label="月份" align="center" prop="month">
          <template #default="scope">
            <dict-tag :options="month_type" :value="scope.row.month"/>
          </template>
        </el-table-column>
        <el-table-column label="作者" align="center" prop="author" />
        <el-table-column label="文档路径" align="center" prop="filePath" />
        <el-table-column label="文档链接" align="center" prop="fileUrl" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" >修改</el-button>
            <el-button link type="primary" 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"
      />
  
      <!-- 添加或修改studyNation对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="studyNationRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" />
          </el-form-item>
          <el-form-item label="区域" prop="nation">
            <el-input v-model="form.nation" placeholder="请输入区域" />
          </el-form-item>
          <el-form-item label="封面图片" prop="coverPath">
            <image-upload v-model="form.coverPath"/>
          </el-form-item>
          <el-form-item label="年份" prop="year">
            <el-select v-model="form.year" placeholder="请选择年份">
              <el-option
                v-for="dict in year_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="月份" prop="month">
            <el-select v-model="form.month" placeholder="请选择月份">
              <el-option
                v-for="dict in month_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" placeholder="请输入作者" />
          </el-form-item>
          <el-form-item label="文档路径" prop="filePath" v-if="open && title === '添加研究（地区）'">
            <FileUpload v-model="form.filePath"/>
          </el-form-item>
          <el-form-item label="文档链接" prop="fileUrl">
            <el-input v-model="form.fileUrl" placeholder="请输入文档链接" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="StudyNation">
  import { listStudyNation, getStudyNation, delFile, addStudyNation, updateStudyNation } from "@/api/VRdemo/studyNation";
  import { ref } from 'vue'
  import { Search } from '@element-plus/icons-vue'
  const input3 = ref('')
  const select = ref('')
  const { proxy } = getCurrentInstance();
  const { year_type, month_type } = proxy.useDict('year_type', 'month_type');
  
  const studyNationList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      companyId: null,
      dataType: null,
      title: null,
      nation: null,
      coverPath: null,
      year: null,
      month: null,
      author: null,
      filePath: null,
      fileUrl: null,
    },
    rules: {
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询studyNation列表 */
  function getList() {
    loading.value = true;
    listStudyNation(queryParams.value).then(response => {
      studyNationList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      id: null,
      companyId: null,
      dataType: null,
      title: null,
      nation: null,
      coverPath: null,
      year: null,
      month: null,
      author: null,
      filePath: null,
      fileUrl: null,
      createTime: null,
      updateTime: null
    };
    proxy.resetForm("studyNationRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加研究（地区）";
  }
  
  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getStudyNation(_id).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改研究（地区）";
    });
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["studyNationRef"].validate(valid => {
      if (valid) {
        if (form.value.id != null) {
          updateStudyNation(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addStudyNation(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除studyNation编号为"' + _ids + '"的数据项？').then(function() {
      return delFile(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('system/studyNation/export', {
      ...queryParams.value
    }, `studyNation_${new Date().getTime()}.xlsx`)
  }
  
  getList();
  </script>
<style>
  .input-with-select .el-input-group__prepend {
    background-color: var(--el-fill-color-blank);
  }
</style>