<template>
  <div>
    <div class="search flex justify-between bg-white rounded-md">
      <el-form :inline="true" class="demo-form-inline" size="default">
        <el-form-item>
          <el-date-picker
            v-model="tmy_fbsj_datetime"
            type="date"
            placeholder="选择时间"
            value-format="YYYY-MM-DD"
            clearable
            @clear="initData"
          />
        </el-form-item>
      </el-form>
      <div>
        <el-button type="success" @click="clearData"> 清空 </el-button>
        <el-button type="primary" @click="initData"> 查询 </el-button>
      </div>
    </div>
    <div class="bg-white rounded-md mt-4 px-23px py-16px">
      <div class="flex justify-between">
        <div>
          <el-button type="primary" @click="addData">新增</el-button>
          <el-button type="primary" @click="exportData">导出</el-button>

          <import-button url="/txwx/msgzs_yxcg" @success="initData">
            <template #text>批量导入</template>
          </import-button>
          <el-button
            type="danger"
            @click="deleteMul"
            :disabled="multipleSelection.length == 0"
            >删除</el-button
          >
        </div>
        <div class="flex content-center">
          <el-input
            v-model="keyword"
            class="w-40 mx-2"
            size="small"
            placeholder="请输搜索关键词"
            :suffix-icon="Search"
            clearable
            @clear="initData"
          />
          <el-button type="primary" @click="initData">查询</el-button>
        </div>
      </div>
      <el-divider />
      <el-table :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column
          prop="tmy_id"
          label="信息编号"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="tmy_title"
          label="标题"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tmy_fbr_user_name"
          label="发布人账号"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tmy_fbr"
          label="发布人"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tmy_fbsj_datetime"
          label="发布时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="editData(scope.row)">编辑</el-button
            ><el-divider direction="vertical" />
            <el-button type="text" @click="deleteItem(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end my-10">
        <el-pagination
          v-model:current-page="form_query.page"
          v-model:page-size="form_query.limit"
          :page-sizes="[10, 20, 50, 100]"
          :pager-count="5"
          :small="true"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="importVisible"
      title="批量导入"
      width="520px"
      class="min-h-280px"
    >
      <div class="px-15">
        <div class="absolute right-20">
          <span
            class="text-blue-500 font-medium cursor-pointer"
            @click="exportOne"
            >下载模板</span
          >
        </div>
        <file-upload-progress
          :limit="1"
          v-model:full="importfile"
          class="w-full"
        ></file-upload-progress>
      </div>
      <div class="center mt-25">
        <el-button type="primary" @click="importData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  <script setup>
import { Search } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
let tmy_fbsj_datetime = $ref("");

let keyword = $ref("");
let tableData = $ref([]);
let multipleSelection = $ref([]);
let importVisible = $ref(false);
const handleSelectionChange = (val) => {
  multipleSelection = [];
  val.forEach((i) => {
    multipleSelection.push(i.tmy_id);
  });
};
function handleSizeChange(val) {
  form_query.limit = val;
  form_query.page = 1;
  initData();
}
function handleCurrentChange(val) {
  form_query.page = val;
  initData();
}
let total = $ref(10);
const form_query = $ref({
  page: 1,
  limit: 6,
});
initData();
function initData() {
  let data = {
    ...form_query,
    tmy_title: keyword,
    tmy_fbsj_datetime,
  };
  request({
    url: "/txwx/msgzs_yxcg/index",
    data,
  }).then((res) => {
    tableData = res.data.page_data;
    total = Number(res.data.total_rows);
  });
}
// 清空筛选
function clearData() {
  tmy_fbsj_datetime = '';
  initData();
}
// 新增
function addData() {
  router.push({ name: "yxcg_add" });
}
// 编辑
function editData(val) {
  router.push({
    name: "yxcg_edit",
    params: {
      id: val.tmy_id,
    },
  });
}
// 批量删除
function deleteMul() {
  ElMessageBox.confirm("确认删除这些数据?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      request({
        url: "/txwx/msgzs_yxcg/delete",
        data: {
          tmy_id: multipleSelection,
        },
      }).then((res) => {
        if (res.code == "1") {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          initData();
        } else {
          ElMessage({
            type: "success",
            message: res.msg,
          });
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
// 单个删除
function deleteItem(item) {
  ElMessageBox.confirm("确认删除?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      request({
        url: "/txwx/msgzs_yxcg/delete",
        data: {
          tmy_id: item.tmy_id,
        },
      }).then((res) => {
        if (res.code == "1") {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          initData();
        } else {
          ElMessage({
            type: "success",
            message: res.msg,
          });
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
// 导出
function exportData() {
  let data = {
    tmy_id: multipleSelection,
    limit: 9999,
    page: 1,
  };
  download("/txwx/msgzs_yxcg/index", data);
}
// 下载模板
function exportOne() {
  let data = {
    limit: 1,
    page: 1,
  };
  download("txwx/msgzs_yxcg/index", data);
}
// 批量导入
let importfile = $ref("");
function importData() {
  console.log(importfile);
  if (!importfile) {
    return ElMessage({
      type: "warning",
      message: "请上传文件",
    });
  }
  request["import"]({
    url: "txwx/msgzs_yxcg/import",
    data: { file: importfile },
  });
}
</script>
    <style lang="scss" scoped>
@use "~/styles/table.scss";
@use "~/styles/pagination.scss";
.center {
  text-align: center;
}
.search {
  height: 72px;
  line-height: 85px;
  padding: 0 29px;
}
.el-input.rank {
  width: 200px;
}
.total {
  span {
    width: 80px;
  }
}
.member {
  margin: 5px 0;
  .el-input {
    width: 200px;
  }
}
.detail_dialog {
  width: 420px;
  margin: 0 auto;
  > div {
    width: 100%;
    margin: 15px 0;
    color: #000;
  }
  .span_label {
    text-align: right;
    display: inline-block;
    width: 90px;
  }
}
</style>