<script lang="ts">
export default {
  name: 'depart'
};
</script>

<script setup lang="ts">
import { onMounted, reactive, ref, toRefs } from 'vue';
import { ElForm, ElMessage, ElMessageBox } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
import {
  departmentManage,
  addAuditDepart,
  updateAuditDepart,
  delDepart
} from '@/api/system/dept';
import {
  DeptQueryParam,
  DeptItem,
  AddDeptFormData,
  UpdateDeptFormData
} from '@/types/api/system/dept';

const queryFormRef = ref(ElForm);
const dataAddFormRef = ref(ElForm);
const dataUpdateFormRef = ref(ElForm);

const state = reactive({
  loading: true,
  queryParams: {
    pageNo: 1,
    pageSize: 15,
  } as DeptQueryParam,
  list: [] as DeptItem[],
  total: 0,
  addDialog: {
    title: '',
    visible: false,
    loading: false,
  },
  updateDialog: {
    title: '',
    visible: false,
    loading: false,
  },
  addFormData: {} as AddDeptFormData,
  updateFormData: {} as UpdateDeptFormData,
  rules: {
    name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }]
  }
});

const {
  loading,
  queryParams,
  list,
  total,
  addDialog,
  updateDialog,
  addFormData,
  updateFormData,
  rules,
} = toRefs(state);

function handleQuery() {
  state.loading = true;
  departmentManage(state.queryParams).then(({ data }) => {
    state.list = data.content;
    state.total = data.totalElements;
    state.loading = false;
  });
}

function handleAdd() {
  state.addDialog = {
    title: '添加部门',
    visible: true,
    loading: false
  };
  addFormData.value.name = "";
  addFormData.value.leaderId = 1;
  addFormData.value.remark = "";
}

/**
 * 提交新增
 */
function submitAddFormData() {
  dataAddFormRef.value.validate((valid: any) => {
    if (valid) {
      addDialog.value.loading = true;
      addAuditDepart(state.addFormData).then(() => {
        addCancel();
        addDialog.value.loading = false;
        ElMessage.success("添加成功");
        handleQuery();
      });
    }
  });
}

/**
 * 取消
 */
function addCancel() {
  addDialog.value.visible = false;
  addDialog.value.loading = false;
  addFormData.value.name = "";
  addFormData.value.leaderId = 0;
  addFormData.value.remark = "";
  dataAddFormRef.value.resetFields();
  dataAddFormRef.value.clearValidate();
}

/**
 * 修改权限
 * @param row 
 */
const handleEdit = (row: any) => {
  state.updateDialog = {
    title: '修改权限',
    visible: true,
    loading: false
  };
  updateFormData.value.id = row.id;
  updateFormData.value.name = row.name;
  updateFormData.value.leaderId = row.leaderId;
  updateFormData.value.remark = row.remark;
  updateFormData.value.createTime = row.createTime;
  updateFormData.value.updateTime = row.updateTime;
}


/**
 * 提交更新
 */
function submitUpdateFormData() {
  dataUpdateFormRef.value.validate((valid: any) => {
    if (valid) {
      updateDialog.value.loading = true;
      updateAuditDepart(state.updateFormData).then(() => {
        updateCancel();
        updateDialog.value.loading = false;
        ElMessage.success("修改成功");
        handleQuery();
      });
    }
  });
}

/**
 * 取消更新
 */
function updateCancel() {
  updateDialog.value.visible = false;
  updateDialog.value.loading = false;
  updateFormData.value.id = 0;
  updateFormData.value.name = "";
  updateFormData.value.leaderId = 0;
  updateFormData.value.remark = "";
  updateFormData.value.createTime = "";
  updateFormData.value.updateTime = "";
  dataUpdateFormRef.value.resetFields();
  dataUpdateFormRef.value.clearValidate();
}

/**
 * 删除
 */
function handleDelete(row: any) {
  ElMessageBox.confirm('确认删除部门【' + row.name + '】?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  .then(() => {
    delDepart({ id: row.id }).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  })
  .catch(() => ElMessage.info('已取消删除'));
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item>
        <el-button type="success" :icon="Plus" @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table
      ref="dataTableRef"
      v-loading="loading"
      :data="list"
      highlight-current-row
      border
      >
      <el-table-column label="ID" prop="id" min-width="80" align="center" />
      <el-table-column label="部门" prop="name" min-width="110" align="center" />
      <el-table-column label="创建时间" prop="createTime" min-width="160" align="center" />
      <el-table-column label="备注" prop="remark" min-width="160" align="center" />
      <el-table-column label="操作" align="center" width="280" fixed="right">
        <template #default="scope">
          <el-button type="primary" size="small" @click.stop="handleEdit(scope.row)">查看 / 修改</el-button>
          <el-button type="danger" size="small" @click.stop="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页工具条 -->
    <pagination
      v-if="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />

    <!-- 添加表单弹窗 -->
    <el-dialog
      :title="addDialog.title"
      v-model="addDialog.visible"
      width="760px"
      destroy-on-close
    >
      <el-form
        ref="dataAddFormRef"
        :model="addFormData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="领导ID" prop="leaderId">
          <el-input type="number" v-model="addFormData.leaderId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="部门" prop="name">
          <el-input type="text" v-model="addFormData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="addFormData.remark" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="addDialog.loading" @click="submitAddFormData">确 定</el-button>
          <el-button @click="addCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 更新表单弹窗 -->
    <el-dialog
      :title="updateDialog.title"
      v-model="updateDialog.visible"
      width="760px"
      destroy-on-close
    >
      <el-form
        ref="dataUpdateFormRef"
        :model="updateFormData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="领导ID" prop="leaderId">
          <el-input type="number" v-model="updateFormData.leaderId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="部门" prop="name">
          <el-input type="text" v-model="updateFormData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="updateFormData.remark" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-input type="text" v-model="updateFormData.createTime" readonly disabled />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime">
          <el-input type="text" v-model="updateFormData.updateTime" readonly disabled />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="updateDialog.loading" @click="submitUpdateFormData">确 定</el-button>
          <el-button @click="updateCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
