<template>
  <div class="language-management">
    <div class="content">
      <div class="languagetitle">
        <div class="title">语言管理</div>

        <div class="add" @click="dialogVisible = true">
          <img class="add-img" src="@/assets/plus2.png" alt="" />
        </div>

      </div>
      <!-- 语言列表 -->
      <div class="languagelist">
        <el-table
          :data="tableData"
          stripe
          style="width: 100%; margin-top: 10px"
          max-height="700"
        >
          <el-table-column prop="language" label="语言名称" />
          <el-table-column prop="" label="" />
          <el-table-column prop="" label="" />
          <el-table-column prop="" label="" />
          <el-table-column label="操作">

            <template #default="scope">
              <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                修改
              </el-button>
              <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
              >
                删除
              </el-button>
            </template>

          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
  <!-- 编辑弹窗 -->
  <el-drawer v-model="dialogVisible" :with-header="false" @close="dialogVisible = false, languageNamedeta = {}">
    <div class="dialog-content">
      <div class="dialog-title">语言{{languageNamedeta.id? '编辑' : '新增'}}</div>
      <el-input
        v-model="languageNamedeta.language"
        style="width: 100%; margin-top: 20px"
        :rows="3"
        type="textarea"
        placeholder="请输入语言内容"
      />
      <div class="dialog-btn">
        <div class="dialogSave" @click="languageSave">保存</div>
        <div class="dialogCancel" @click="dialogVisible = false">取消</div>
      </div>
    </div>
  </el-drawer>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { getLanguageList,deleteLanguage,editLanguage,addLanguage} from "@/api/account";
import {
  ref, // 响应式基础
  watch, // 侦听器
  onMounted, // 组件挂载完成后执行
  onUpdated, // 组件更新后执行
  onUnmounted, // 组件销毁前执行
} from "vue";
const refname = ref("");
const tableData = ref([]);
const dialogVisible = ref(false);
let languageNamedeta = ref({});// 编辑添加弹窗数据
// 编辑弹窗
function handleEdit(index, row) {
    dialogVisible.value = true;
    languageNamedeta.value = row;
}
// 保存语言
function languageSave() {
  if (languageNamedeta.value.language == null) {
    ElMessage({
      type: "error",
      message: "请输入语言内容",
    });
    return;
  }
  if (languageNamedeta.value.id) {
    // 修改语言
  editLanguage(languageNamedeta.value)
   .then(() => {
      ElMessage({
        type: "success",
        message: "修改成功",
      });
      getLanguageListData();
   })
   .catch(() => {
      ElMessage({
        type: "error",
        message: "修改失败",
      });
    });
  dialogVisible.value = false;
}else{
    // 新增语言
    addLanguage(languageNamedeta.value)
   .then(() => {
      ElMessage({
        type: "success",
        message: "添加成功",
      });
      getLanguageListData();
   })
   .catch(() => {
      ElMessage({
        type: "error",
        message: "添加失败",
      });
    });
  dialogVisible.value = false;
}
}
// 删除语言
function handleDelete(index, row) {
    ElMessageBox.confirm("您确认要删除这个语言吗？")
    .then(() => {
        deleteLanguage({ id: row.id })
        .then(() => {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          getLanguageListData();
        })
        .catch(() => {
          ElMessage({
            type: "error",
            message: "删除失败",
          });
        });
    })
    .catch(() => {});
}

function getLanguageListData() {
     // 获取语言列表
     getLanguageList()
   .then((res) => {
    console.log(res);
      tableData.value = res;
    }).catch(() => {
      ElMessage({
        type: "error",
        message: "获取语言列表失败",
      });
    });
}

watch(refname, async (newQuestion, oldQuestion) => {
  // 变化后执行
});
onMounted(() => {
  getLanguageListData();
   // 组件挂载完成后执行
});
onUpdated(() => {
  // 组件更新后执行
});
onUnmounted(() => {
  // 组件销毁前执行
});
</script>

<style scoped>
.language-management {
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.content {
  width: 95%;
  height: 95%;
  background-color: #f5f5f5;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  box-shadow: 2px 4px 8px rgba(0, 0, 0, 0.3);
}
.languagetitle {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  background-color: #e2e2e2;
  border-radius: 10px;
}
.title {
  font-size: 24px;
  font-weight: bold;
  color: #999999;
  margin-left: 40px;
}
.add {
  width: 150px;
  height: 50px;
  background-color: #cccccc;
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.add:hover {
  background-color: #999999;
}
.add-img {
  width: 40px;
  height: 40px;
}
.languagelist {
  width: 97%;
  height: 85%;
  background-color: #ffffff;
  margin-bottom: 20px;
  border-radius: 10px;
  overflow: auto;
  scrollbar-width: none;
}
.dialog-content {
  width: 100%;
  height: 100%;
}
.dialog-title {
  width: 100%;
  height: 50px;
  font-size: 25px;
  color: #b4b4b4;
  font-weight: bold;
  margin-top: 20px;
  margin-bottom: 20px;
  text-align: center;
}
.dialog-btn {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
.dialogSave {
  width: 45%;
  height: 50px;
  background-color: #d6d6d6;
  line-height: 50px;
  text-align: center;
  font-weight: bold;
  border-radius: 5px;
  font-size: 16px;
  color: #ffffff;
}
.dialogSave:hover {
  background-color: #b4b4b4;
}
.dialogCancel {
  width: 45%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-weight: bold;
  border-radius: 5px;
  font-size: 16px;
  color: #d6d6d6;
  border: 1px solid #d6d6d6;
}
.dialogCancel:hover {
  background-color: #e7e6e6;
  color: #ffffff;
}
</style>
