<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>班型管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/classes/list">班型列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 搜索 -->
  <el-form inline :model="params">


    <el-form-item label="名称">
      <el-input v-model='params.classes_name' placeholder='请输入名称'></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;">
    <!-- 添加班型 -->
    <el-col :span="1">
      <el-button type="primary" @click="addClassesDialogVisible = 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 => getClassesList(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>

  <!-- 添加班型 -->
  <AddClassesDialog v-model="addClassesDialogVisible" :done="() => tableRef.reload()"></AddClassesDialog>
  <!-- 编辑班型 -->
  <EditClassesDialog v-model="EditClassesDialogVisible" :data="EditClassesDialogRow" :done="() => tableRef.reload()">
  </EditClassesDialog>
  <!-- 班型详情 -->
  <DetailClassesDialog v-model="DetailClassesDialogVisible" :data="DetailClassesDialogRow"></DetailClassesDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive } from 'vue';
import { useLoginStore } from "~/store";
import { getClassesList, deleteClasses, exportExcel, downloadTemplate, importExcel } from '~/service/classes';
import AddClassesDialog from './components/AddClassesDialog.vue';
import EditClassesDialog from './components/EditClassesDialog.vue';
import DetailClassesDialog from './components/DetailClassesDialog.vue';

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

const addClassesDialogVisible = ref(false);
const EditClassesDialogVisible = ref(false);
const EditClassesDialogRow = ref({});
const DetailClassesDialogVisible = ref(false);
const DetailClassesDialogRow = ref({});

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

// 查询参数
const params = reactive({
  classes_name: "",

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "classes_name",
    label: '名称',
    width: '150'
  },
  {
    prop: "classes_desc",
    label: '简介',
    width: '150'
  },
  {
    prop: "classes_sort",
    label: '排序',
    width: '150'
  },
  {
    label: '操作',
    prop: 'chaoz',
    width: '250'
  }
];


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 deleteClasses({
      classes_guid: data.map(v => v.classes_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditClassesDialogVisible.value = true
  EditClassesDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailClassesDialogVisible.value = true
  DetailClassesDialogRow.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();
};




</script>
