<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>多语言设置管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/i18n_lang/list">多语言设置列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 搜索 -->
  <el-form inline :model="params">


    <el-form-item label="语言类型">
      <el-select v-model="params.i18n_lang_type_guid" clearable placeholder="请选择" style="width:150px">
        <el-option v-for="item in i18n_lang_type_list" :key="item.i18n_lang_type_guid" :label="item.i18n_lang_type_name"
          :value="item.i18n_lang_type_guid"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="语言key">
      <el-input v-model='params.i18n_lang_key' placeholder='请输入语言key'></el-input>
    </el-form-item>
    <el-form-item label="语言name">
      <el-input v-model='params.i18n_lang_name' placeholder='请输入语言name'></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="tableRef.reload()" icon="ElIconSearch">
        搜索
      </el-button>
    </el-form-item>
  </el-form>
  <el-space style="margin-bottom: 10px;">
    <!-- <div v-for="item in i18n_lang_type_list">
      <div>{{ item.i18n_lang_type_name }}</div>
    </div> -->
    <!-- 添加多语言设置 -->
    <el-col :span="1">
      <el-button type="primary" @click="addI18nLangDialogVisible = true"> 添加 </el-button>
    </el-col>


    <!-- 导出 -->
    <el-button icon="ElIconDocument" @click="exportExcel(params)">导出</el-button>

    <!-- 导入 -->
    <el-upload class="upload-demo" :action="importExcel" :headers="headers" :on-success="handleExcelSuccess"
      :on-progress="uploadLoading" :on-error="closeUploadLoading" style="margin-left: 10px" :show-file-list="false">
      <el-button type="primary">导入</el-button>
    </el-upload>

    <!-- 下载导入模板 -->
    <el-button icon="ElIconDownload" @click="downloadTemplate()">下载导入模板</el-button>


    <!-- 下拉操作 -->
    <el-dropdown v-if="selectionData.length">
      <el-button type="primary">
        批量操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
      </el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="handleDelete(selectionData)">
            批量删除
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </el-space>
  <!-- 数据表格 -->
  <DataTable ref="tableRef" style="width: 100%" :onSelectionChange="data => (selectionData = data)" :column="column"
    :params="params" :request="params => getI18nLangList(params)">





    <template #chaoz="scope">
      <el-space>
        <el-button size="small" @click="handleUpdate(scope.row)">
          编辑
        </el-button>
        <el-dropdown @command="handleCommand">
          <el-button type="primary" size="small">
            更多<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item :command="{ type: 'detail', row: scope.row }">
                详情
              </el-dropdown-item>
              <el-dropdown-item :command="{ type: 'delete', row: scope.row }">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-space>
    </template>
  </DataTable>

  <!-- 添加多语言设置 -->
  <AddI18nLangDialog v-model="addI18nLangDialogVisible" :done="() => tableRef.reload()"></AddI18nLangDialog>
  <!-- 编辑多语言设置 -->
  <EditI18nLangDialog v-model="EditI18nLangDialogVisible" :data="EditI18nLangDialogRow" :done="() => tableRef.reload()">
  </EditI18nLangDialog>
  <!-- 多语言设置详情 -->
  <DetailI18nLangDialog v-model="DetailI18nLangDialogVisible" :data="DetailI18nLangDialogRow"></DetailI18nLangDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getI18nLangList, editI18nLang, deleteI18nLang, exportExcel, downloadTemplate, importExcel } from '~/service/i18n_lang';
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import AddI18nLangDialog from './components/AddI18nLangDialog.vue';
import EditI18nLangDialog from './components/EditI18nLangDialog.vue';
import DetailI18nLangDialog from './components/DetailI18nLangDialog.vue';

const tableRef = ref();
const selectionData = ref([]);
const store = useLoginStore();
const i18n_lang_type_list = ref([]);

const addI18nLangDialogVisible = ref(false);
const EditI18nLangDialogVisible = ref(false);
const EditI18nLangDialogRow = ref({});
const DetailI18nLangDialogVisible = ref(false);
const DetailI18nLangDialogRow = ref({});

const headers = {
  Accept: "application/json",
  ...store.headers,
};

// 查询参数
const params = reactive({
  i18n_lang_type_guid: "",
  i18n_lang_key: "",
  i18n_lang_name: ""
});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "i18n_lang_type_name",
    label: '语言类型',
  },
  {
    prop: "i18n_lang_key",
    label: '语言key',
  },
  {
    prop: "i18n_lang_name",
    label: '语言name',
  },
  {
    label: '操作',
    prop: 'chaoz',
    width: '250',
    fixed: 'right'
  }
];


const handleCommand = ({ type, row }) => {
  switch (type) {
    case "detail":
      handleDetail(row);
      break;
    case 'delete':
      handleDelete([row]);
      break;
  }
};

// 删除数据
const handleDelete = data => {
  ElMessageBox.confirm(`您确定要删除该多语言设置吗？`).then(async () => {
    const res = await deleteI18nLang({
      i18n_lang_guid: data.map(v => v.i18n_lang_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditI18nLangDialogVisible.value = true
  EditI18nLangDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailI18nLangDialogVisible.value = true
  DetailI18nLangDialogRow.value = row
}


// 导入方法
let loadingImoprt = null;
const uploadLoading = () => {
  loadingImoprt = ElLoading.service({
    lock: true,
    text: "正在导入中...",
    background: "rgba(255, 255, 255, 0.7)",
  });
};
const closeUploadLoading = () => loadingImoprt.close();
const handleExcelSuccess = (value) => {
  if (value.code == 0) {
    ElMessageBox.alert(value.msg, "导入信息", {
      dangerouslyUseHTMLString: true,
      confirmButtonText: "确定",
    });
  } else {
    ElMessage.error(value.msg);
  }
  closeUploadLoading();
  tableRef.value.reload();
};


// 获取
async function get_i18n_lang_type() {
  await getI18nLangTypeList().then((res) => {
    i18n_lang_type_list.value = res.data
  })
}
get_i18n_lang_type()



</script>
