<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="Logo地址" prop="logoUrl">
        <el-input
          v-model="queryParams.logoUrl"
          placeholder="请输入Logo地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业开始时间" prop="openTime">
        <el-date-picker clearable
          v-model="queryParams.openTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择营业开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="营业结束时间" prop="closeTime">
        <el-date-picker clearable
          v-model="queryParams.closeTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择营业结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新者id" prop="updater">
        <el-input
          v-model="queryParams.updater"
          placeholder="请输入更新者id"
          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="warning"
          plain
          icon="Download"
          @click="handleExport"
        >导出</el-button>
      </el-col>-->
    </el-row>
    <br>

    <el-table v-loading="loading" :data="museumIntroList">
      <el-table-column label="博物馆名称" align="center" prop="museumName" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="Logo地址" align="center" prop="logoUrl" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="营业开始时间" align="center" prop="openTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.openTime, 'yyyy-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="营业结束时间" align="center" prop="closeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.closeTime, 'yyyy-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者id" align="center" prop="updater" />
      <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>

    <br>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryParams.pageSize"
        layout="->, total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!-- 添加或修改博物馆简介对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="museumIntroRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="Logo地址" prop="logoUrl">
          <el-input v-model="form.logoUrl" placeholder="请输入Logo地址" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="营业开始时间" prop="openTime">
          <el-date-picker clearable
            v-model="form.openTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择营业开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="营业结束时间" prop="closeTime">
          <el-date-picker clearable
            v-model="form.closeTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择营业结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新者id" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入更新者id" />
        </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="MuseumIntro">
import { ref, reactive, toRefs, getCurrentInstance } from 'vue';
import { listMuseumIntro, getMuseumIntro, delMuseumIntro, addMuseumIntro, updateMuseumIntro } from "@/api/museumIntro";

const { proxy } = getCurrentInstance();

const museumIntroList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const isAdd = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    content: null,
    logoUrl: null,
    address: null,
    phone: null,
    openTime: null,
    closeTime: null,
    updater: null
  },
  rules: {
    content: [
      { required: true, message: "内容不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询博物馆简介列表 */
function getList() {
  loading.value = true;
  listMuseumIntro(queryParams.value).then(response => {
    museumIntroList.value = response.list;
    total.value = response.total;
    loading.value = false;
  });
}

// 改变每页条数大小
function handleSizeChange(val) {
  queryParams.value.pageSize = val;
  getList();
}

// 换页
function handleCurrentChange(val) {
  queryParams.value.pageNum = val;
  getList();
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    museumName: null,
    content: null,
    logoUrl: null,
    address: null,
    phone: null,
    openTime: null,
    closeTime: null,
    updateTime: null,
    updater: null
  };
  const museumIntroRef = proxy.$refs["museumIntroRef"]
  if (museumIntroRef) {
    museumIntroRef.resetFields();
  }
}

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

/** 重置按钮操作 */
function resetQuery() {
  const queryRef = proxy.$refs["queryRef"]
  if (queryRef) {
    queryRef.resetFields();
  }
  handleQuery();
}


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加博物馆简介";
  isAdd.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _museumName = row.museumName || ids.value
  getMuseumIntro(_museumName).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改博物馆简介";
    isAdd.value = false;
  });
}

/** 表单提交按钮 */
function submitForm() {
  proxy.$refs["museumIntroRef"].validate(valid => {
    if (valid) {
      if (isAdd.value) {  // 添加
        addMuseumIntro(form.value).then(response => {
          proxy.$message({
            message: response.msg,
            type: 'success'
          })
          open.value = false;
          getList();
        });
      } else {  // 更新
        updateMuseumIntro(form.value).then(response => {
          proxy.$message({
            message: response.msg,
            type: 'success'
          })
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$confirm('是否确认删除博物馆简介编号为"' + row.museumName + '"的数据项？').then(function() {
    return delMuseumIntro(row.museumName);
  }).then(response => {
    getList();
    proxy.$message({
      message: response.msg,
      type: 'success'
    })
  }).catch(() => {});
}

/** 导出按钮操作 */
// function handleExport() {
//   proxy.download('system/museumIntro/export', {
//     ...queryParams.value
//   }, `museumIntro_${new Date().getTime()}.xlsx`)
// }

getList();
</script>
