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


    <el-form-item label="姓名">
      <el-input v-model='params.leave_message_name' placeholder='请输入姓名'></el-input>
    </el-form-item>
    <el-form-item label="电话">
      <el-input v-model='params.leave_message_phone' placeholder='请输入电话'></el-input>
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model='params.leave_message_email' 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="addLeaveMessageDialogVisible = true"> 添加 </el-button>
    </el-col> -->


    <!-- 导出 -->
    <el-button icon="ElIconDocument" @click="exportExcel(params)">导出</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 => getLeaveMessageList(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>

  <!-- 添加留言 -->
  <AddLeaveMessageDialog v-model="addLeaveMessageDialogVisible" :done="() => tableRef.reload()"></AddLeaveMessageDialog>
  <!-- 编辑留言 -->
  <EditLeaveMessageDialog v-model="EditLeaveMessageDialogVisible" :data="EditLeaveMessageDialogRow"
    :done="() => tableRef.reload()"></EditLeaveMessageDialog>
  <!-- 留言详情 -->
  <DetailLeaveMessageDialog v-model="DetailLeaveMessageDialogVisible" :data="DetailLeaveMessageDialogRow">
  </DetailLeaveMessageDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getLeaveMessageList, editLeaveMessage, deleteLeaveMessage, exportExcel } from '~/service/leave_message';
import AddLeaveMessageDialog from './components/AddLeaveMessageDialog.vue';
import EditLeaveMessageDialog from './components/EditLeaveMessageDialog.vue';
import DetailLeaveMessageDialog from './components/DetailLeaveMessageDialog.vue';

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

const addLeaveMessageDialogVisible = ref(false);
const EditLeaveMessageDialogVisible = ref(false);
const EditLeaveMessageDialogRow = ref({});
const DetailLeaveMessageDialogVisible = ref(false);
const DetailLeaveMessageDialogRow = ref({});

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

// 查询参数
const params = reactive({
  leave_message_name: "",
  leave_message_phone: "",
  leave_message_email: "",

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "leave_message_name",
    label: '姓名',
  },
  {
    prop: "leave_message_phone",
    label: '电话',
  },
  {
    prop: "leave_message_email",
    label: '邮箱',
  },
  {
    prop: "leave_message_info",
    label: '信息',
  },
  {
    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 deleteLeaveMessage({
      leave_message_id: data.map(v => v.leave_message_id).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditLeaveMessageDialogVisible.value = true
  EditLeaveMessageDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailLeaveMessageDialogVisible.value = true
  DetailLeaveMessageDialogRow.value = row
}






</script>
