<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="searchForm.tmg_fbsj_datetime"
            type="date"
            placeholder="选择时间"
            value-format="YYYY-MM-DD"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.tm_id"
            placeholder="全部工作室"
            class="w-40"
            clearable
            @clear="initData"
          >
            <el-option
              v-for="g in gzsList"
              :key="g.tm_id"
              :label="g.tm_gzsmc"
              :value="g.tm_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.tmg_lb"
            placeholder="全部类别"
            class="w-40"
            clearable
            @clear="initData"
          >
            <el-option
              v-for="s in typeList"
              :key="s.v"
              :label="s.n"
              :value="s.v"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div>
        <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_gwz" @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"
            clearable
            placeholder="请输搜索关键词"
            :suffix-icon="Search"
            @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="tmg_id"
          label="信息编号"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="tmt_cymc"
          label="工作室名称"
          width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tmg_fbr_user_name"
          label="发布人账号"
          width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tmg_fbr"
          label="发布人"
          width="80"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tmg_lb"
          label="类别"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tmg_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-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="[6, 10, 20]"
          :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 v-model:part="importfile" class="w-full"></file-upload>
      </div>
      <div class="center mt-25">
        <el-button type="primary" @click="importData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import {
  Search,
  Delete,
  Download,
  Plus,
  ZoomIn,
} from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
// let tmg_fbsj_datetime = $ref("");
let searchForm = $ref({
  tmg_fbsj_datetime: "",
  tm_id: "",
  tmg_lb: "",
});
let gzsList = $ref([]);
request({
  url: "/txwx/msgzs/index",
  data: {
    limit: "9999",
    page: "1",
  },
}).then((res) => {
  if (res.code == "1") {
    gzsList = res.data.page_data;
  }
});
let keyword = $ref("");
let tableData = $ref([]);
let multipleSelection = $ref([]);
let importVisible = $ref(false);
const handleSelectionChange = (val) => {
  multipleSelection = [];
  val.forEach((i) => {
    multipleSelection.push(i.tmg_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();
let typeList = $ref([]);
function initData() {
  let data = {
    ...form_query,
    ...searchForm,
    keyword,
  };
  request({
    url: "/txwx/msgzs_gwz/index",
    data,
  }).then((res) => {
    if (res.code == "1") {
      typeList = res.data.table_structure.field.tmg_lb.option;
      tableData = res.data.page_data;
      total = Number(res.data.total_rows);
    }
  });
}

// 新增跳转
function addData() {
  router.push({ name: "gg_add" });
}
// 编辑跳转
function editData(val) {
  router.push({
    name: "gg_edit",
    params: {
      id: val.tmg_id,
    },
  });
}

// 导出
function exportData() {
  let data = {
    tmg_id: multipleSelection,
  };
  download("txwx/msgzs_gwz/index", data);
}
// 下载模板
function exportOne() {
  let data = {
    limit: 1,
    page: 1,
  };
  download("txwx/msgzs_gwz/index", data);
}
// 批量导入
let importfile = $ref('');
function importData() {
  console.log(importfile);
  // if (!importfile) {
  //   return ElMessage({
  //     type: "warning",
  //     message: "请上传文件",
  //   });
  // }
  // request({});
}
// 批量删除
function deleteMul() {
  ElMessageBox.confirm("确认删除这些数据?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      request({
        url: "/txwx/msgzs_gwz/delete",
        data: {
          tmg_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_gwz/delete",
        data: {
          tmg_id: item.tmg_id,
        },
      }).then((res) => {
        if (res.code == "1") {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          initData();
        } else {
          ElMessage({
            type: "success",
            message: res.msg,
          });
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
</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>