<template>
  <div class="page">
    <el-card shadow="never">
      <div class="search">
        <el-form inline>
          <!-- 关键字 -->
          <el-form-item :label="$t('message.device.formI18nLabel.keyword')">
            <el-input v-model="searchParams.keyWord" style="width: 200px; margin-left: 20px" class="search-input" :placeholder="$t('message.configuration.designScreen.keywordPlaceholder')" clearable @keyup.enter="handleSearch" />
          </el-form-item>
          <el-form-item>
            <!-- 查询 -->
            <el-button type="primary" @click="handleSearch">
              <el-icon><ele-Search /></el-icon>
              {{ $t('message.formI18nButton.query') }}
            </el-button>
          </el-form-item>
          <el-form-item>
            <!-- 新增大屏 -->
            <el-button type="primary" v-auth="'add'" @click="addOrEdit()">
              <el-icon>
                <ele-FolderAdd />
              </el-icon>
              {{ $t('message.configuration.designScreen.addScreen') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <!-- 序号 -->
        <el-table-column type="index" :label="$t('message.tableI18nColumn.index')" width="70" align="center" />
        <!-- ID -->
        <el-table-column prop="id" :label="$t('message.tableI18nColumn.id')" show-overflow-tooltip></el-table-column>
        <!-- 大屏名称 -->
        <el-table-column prop="projectName" :label="$t('message.configuration.designScreen.screenName')" show-overflow-tooltip></el-table-column>
        <!-- 描述 -->
        <el-table-column prop="remarks" :label="$t('message.tableI18nColumn.des')" show-overflow-tooltip></el-table-column>
        <!-- 创建时间 -->
        <el-table-column prop="createdAt" :label="$t('message.tableI18nColumn.createdAt')" min-width="100" align="center"></el-table-column>
        <!-- 更新时间 -->
        <el-table-column prop="updatedAt" :label="$t('message.tableI18nColumn.updatedAt')" min-width="100" align="center"></el-table-column>
        <!-- 操作 -->
        <el-table-column :label="$t('message.tableI18nColumn.operation')" width="200" align="center">
          <template #default="scope">
            <!-- 预览 -->
            <el-button size="small" text type="primary" @click="preview(scope.row)">{{ $t('message.configuration.designScreen.previewBtn') }}</el-button>
            <!-- 编辑 -->
            <el-button size="small" text type="warning" v-auth="'edit'" @click="addOrEdit(scope.row)">{{ $t('message.tableI18nAction.edit') }}</el-button>
            <!-- 设计大屏 -->
            <el-button size="small" text type="warning" @click="edit(scope.row)">{{ $t('message.configuration.designScreen.designBtn') }}</el-button>
            <!-- 删除 -->
            <el-button size="small" text type="info" v-auth="'del'" @click="onDel(scope.row)">{{ $t('message.tableI18nAction.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
      <EditForm ref="editFormRef" @getList="getList(1)"></EditForm>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from 'vue';
import api from '/@/api/screen';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useSearch } from '/@/hooks/useCommon';
import EditForm from './edit.vue';
import { useI18n } from 'vue-i18n';
// 国际化
const { t } = useI18n();

const editFormRef = ref();

const searchParams = reactive({
  keyWord: ''
});

// 监听搜索参数变化，确保在API请求中正确传递搜索条件
watch(searchParams, (newVal:any) => {
  // 处理搜索参数的值
  for (const key in newVal) {
    if (newVal[key] === '' || newVal[key] === null) {
      delete params[key];
    } else {
      params[key] = newVal[key];
    }
  }
}, { deep: true });

const { params, tableData, getList, loading } = useSearch<any[]>(api.getList, 'Data', searchParams);

getList();

// 搜索
const handleSearch = () => {
  getList(1);
};

const addOrEdit = async (row?: any) => {
  if (row) {
    editFormRef.value.open(row);
    return;
  } else {
    editFormRef.value.open();
  }
};

// const add = async () => {
//   ElMessageBox.prompt('请输入项目名称', '创建大屏项目', {
//     confirmButtonText: '确认',
//     cancelButtonText: '取消',
//     inputValidator: (value: string) => {
//       if (value.trim()) {
//         return true;
//       } else {
//         return '请输入项目名称';
//       }
//     },
//     inputErrorMessage: '请输入描述',
//   }).then(async ({ value: projectName }) => {
//     ElMessageBox.prompt('请输入描述', '创建大屏项目', {
//       confirmButtonText: '确认',
//       cancelButtonText: '取消',
//     }).then(async ({ value: remarks }) => {
//       await api.add({
//         indexImage: null,
//         projectName,
//         remarks,
//       });
//       ElMessage.success('新增成功');
//       getList();
//     });
//   });
// };

const edit = async (row: any) => {
  const url = import.meta.env.VITE_SCREEN_URL + '#/chart/home/' + row.id;
  window.open(url);
};

const preview = async (row: any) => {
  const url = import.meta.env.VITE_SCREEN_URL + '#/chart/preview/' + row.id;
  window.open(url);
};

const onDel = (row: any) => {
  ElMessageBox.confirm(
    t('message.configuration.designScreen.deleteScreenMessage', { name: row.projectName }), 
    t('message.tableI18nConfirm.deleteTitle'), {
    confirmButtonText: t('message.tableI18nConfirm.confirmText'),
    cancelButtonText: t('message.tableI18nConfirm.cancelText'),
    type: 'warning',
  }).then(async () => {
    await api.del([row.id as string]);
    ElMessage.success(t('message.tableI18nConfirm.deleteSuccess'));
    getList();
  });
};
</script>
