<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-select clearable @clear="initData" v-model="form_query.tz_ssmsgzs_id" placeholder="全部所属名师工作室">
            <el-option v-for="g in gzsList" :key="g.tm_id" :label="g.tm_gzsmc" :value="g.tm_id"></el-option>
          </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/zyfd/import" @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="form_query.tz_mc" 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="tz_id" label="信息编号" width="100"></el-table-column>
        <el-table-column prop="tz_mc" label="名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="tjzx_name" label="学科" show-overflow-tooltip></el-table-column>
        <el-table-column label="年级" show-overflow-tooltip>
          <template #default="scope">
            <span class="mr-1" v-for="n in scope.row.tjzn_json" :key="n.tjzn_id"> {{ n.tjzn_njmc }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="tz_zjyhmc" label="主讲人" show-overflow-tooltip></el-table-column>
        <el-table-column prop="tz_lll" label="浏览量" show-overflow-tooltip></el-table-column>
        <el-table-column prop="tz_dzl" label="点赞量" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="text" @click="detailData(scope.row)">详情</el-button><el-divider direction="vertical" />
            <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">
        <el-pagination v-model:current-page="form_query.page" v-model:page-size="form_query.limit"
          :page-sizes="[6, 12, 18, 50]" :pager-count="5" :small="true" layout="total,sizes, prev, pager, next, jumper"
          :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script setup>
import {
  Search,
  Delete,
  Download,
  Plus,
  ZoomIn,
} from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
let timeValue = $ref("");
// 名师工作室
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 tableData = $ref([]);
let multipleSelection = $ref([]);
let importVisible = $ref(false);
const handleSelectionChange = (val) => {
  multipleSelection = [];
  val.forEach((i) => {
    multipleSelection.push(i.tz_id);
  });
};
function handleSizeChange(val) {
  form_query.limit = val;
  form_query.page = 1;
  initData();
}
let total = $ref(10);
const form_query = $ref({
  page: 1,
  limit: 6,
  tz_ssmsgzs_id:'',
  tz_mc:'',
});
initData()
let tjzn_json = $ref([])
function initData() {
  let data = {
    ...form_query,
  };
  request({
    url: '/txwx/zyfd/index',
    data,
  }).then(res => {
    if (res.code == '1') {
      tableData = res.data.page_data;
      total = Number(res.data.total_rows);
      tableData.forEach(item => {
        item.tjzn_json = JSON.parse(item.tjzn_json);
      })
    }
  })
};
let ruleForm = $ref({
  gzs_name: "",
  img: "",
  rank: "",
  subject: "",
  grade: "",
});

// 新增
function addData() {
  router.push({ name: "zyfd_add" });
}
// 编辑
function editData(val) {
  router.push({
    name: "zyfd_edit",
    params: {
      id: val.tz_id,
    },
  });
}
function detailData(item) {
  router.push({
    name: "zyfd_detail",
    params: {
      id: item.tz_id,
    },
  });
}
// 导出
function exportData() {
  let data = {
    tz_id: multipleSelection,
  };
  download("/txwx/zyfd/index", data);
}
// 批量删除
function deleteMul() {
  ElMessageBox.confirm("确认删除这些数据?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      request({
        url: '/txwx/zyfd/delete',
        data: {
          tz_id: multipleSelection,
        }
      }).then(res => {
        if (res.code == '1') {
          initData();
          ElMessage({
            type: "success",
            message: "删除成功",
          });
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
// 单个删除
function deleteItem(item) {
  ElMessageBox.confirm("确认删除?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      request({
        url: '/txwx/zyfd/delete',
        data: {
          tz_id: item.tz_id,
        }
      }).then(res => {
        if (res.code == '1') {
          initData();
          ElMessage({
            type: "success",
            message: "删除成功",
          });
        }
      })
    })
    .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>