<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="部门名称" prop="name">
            <el-input
               v-model="queryParams.name"
               placeholder="请输入部门名称"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <!-- <el-form-item label="英文名称" prop="enName">
            <el-input
               v-model="queryParams.enName"
               placeholder="请输入英文名称"
               clearable
               style="width: 200px"
               @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" v-hasPermi="['big:dept:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" v-hasPermi="['big:dept:update']"
                    @click="handleUpdate(ids[0])">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" v-hasPermi="['big:dept:del']"
                    @click="handleDelete">删除</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="deptList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" width="120" />
            <!-- <el-table-column label="部门id" prop="id" width="120" /> -->
            <el-table-column label="部门名称" prop="name" :show-overflow-tooltip="true" />
            <el-table-column label="英文名称" prop="enName" :show-overflow-tooltip="true" />
            <el-table-column label="排序" prop="sort" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
                <template #default="scope">
                    <el-tooltip content="修改" placement="top" v-if="scope.row.roleId !== 1">
                        <el-button link type="primary" v-hasPermi="['big:dept:update']" icon="Edit"
                            @click="handleUpdate(scope.row.id)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top" v-if="scope.row.userId !== 1">
                        <el-button link type="primary" icon="Delete" v-hasPermi="['big:dept:del']"
                            @click="handleDelete(scope.row)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <add_edit @closeDialog="open = false" :type="type" :title="title" :form="form" :visible="open"
            @refreshTable="getList" />
        <el-dialog v-model="imgDialogVisible">
            <img w-full :src="dialogImageUrl" alt="Preview Image" class="img-preview" />
        </el-dialog>
    </div>
</template>
  
<script setup>
import { ref, reactive, toRefs, getCurrentInstance } from 'vue'
import { Picture as IconPicture } from '@element-plus/icons-vue'
import { listUser } from "@/api/system/user";
import { getDeptList, delDept } from '@/api/big/dept'
import add_edit from './components/add_edit.vue'

const { proxy } = getCurrentInstance();
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        enName: undefined,
    }
});

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

// testScreen({}).then(res => {
//  alert(JSON.stringify(res))
// })

let deptList = ref([])

const imgDialogVisible = ref(false)
const dialogImageUrl = ref('')
const open = ref(false);
const loading = ref(false);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const file_list = ref([])
const type = ref("add");

/** 查询部门列表 */
function getList() {
    loading.value = true;
    // console.log(JSON.stringify(queryParams.value));
    getDeptList(queryParams.value).then(res => {
        loading.value = false;
        deptList.value = res.rows;
        total.value = res.total;
    });

}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 添加部门 */
function handleAdd() {
    title.value = "添加部门";
    form.value = {
        name: undefined,
        enName: undefined,
        sort: undefined
    };
    file_list.value = []
    type.value = "add";
    open.value = true;
}
/** 修改部门 */
function handleUpdate(id) {
    title.value = "修改部门";
    deptList.value.find(item => {
        if (item.id == id) {
            form.value = item;
        }
    })
    if (form.value.img != '' && form.value.img != undefined) {
        file_list.value = [{
            name: form.value.path,
            url: form.value.img
        }]
    }
    type.value = "update";

    // console.log(form.value, 'form.value');
    open.value = true;
}

/** 删除按钮操作 */
function handleDelete(row) {
    let rows = deptList.value.filter(item => ids.value.includes(item.id));
    const names = row.name || rows.map(item => item.name).join(',');
    const screenIds = row.id || ids.value.join(',');
    proxy.$modal.confirm('是否确认删除名称为"' + names + '"的数据项？').then(function () {
        return delDept(screenIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });

}

function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}



/** 图片预览 */
const handlePreview = (img) => {
    console.log(img, 'img');
    imgDialogVisible.value = true;
    dialogImageUrl.value = img;
}

getList();
</script>
  
<style lang="scss" scoped>
:deep(.el-table .el-table__cell) {
    z-index: auto;
}

.img-preview {
    max-width: 100%;
    max-height: 100%;
    display: block;
    margin: 0 auto;
}
</style>
  