<template>
  <div class="createWork" v-loading="loading">
    <div class="recordDiv" v-for="(item, index) in resultData" :key="index">
      <div class="record-list">
        <div class="left">{{item.yzz_name || item.yzk_name}} </div>
        <div class="right">
          <el-button size="mini" type="primary" plain @click="lookDetail(item.yzz_id || item.yzk_id, true)">查看</el-button>
          <el-button size="mini" type="primary" plain @click="handleDelete(item.yzz_id || item.yzk_id)">删除</el-button>
          <el-button size="mini" type="primary" plain @click="lookDetail(item.yzz_id || item.yzk_id)">编辑</el-button>
        </div>
      </div>
      <el-divider border-style="dashed" />
    </div>

    <div class="paginationDiv">
      <el-pagination background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" :page-size="pageSize" :currentPage="currentPage"></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      resultData: [],
      pageSize: 10,
      currentPage: 1,
      total: 0,
    }
  },
  props: ["workType"],
  methods: {
    initData(keyword) {
      this.loading = true;
      if (this.workType == 1) {
        request({
          url: '/yzy/zy_zbkc/index',
          data: {
            keyword: keyword,
            limit: this.pageSize,
            page: this.currentPage,
          }
        }).then(res => {
          if (res.code == 1) {
            this.resultData = res.data.page_data;
            this.total = Number(res.data.total_rows);
            this.loading = false;
          }
        })
      } else if (this.workType == 2) {
        request({
          url: '/yzy/zy_kczy/index',
          data: {
            keyword: keyword,
            limit: this.pageSize,
            page: this.currentPage,
          }
        }).then(res => {
          if (res.code == 1) {
            this.resultData = res.data.page_data;
            this.total = Number(res.data.total_rows);
            this.loading = false;
          }
        })
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initData();
    },
    lookDetail(id, isLook) {
      this.$router.push({ name: "personal_homework_wlzb_work", params: { workType: this.workType }, query: { id, isLook } });
    },
    handleDelete(id) {
      ElMessageBox.confirm('是否确定删除当前作业？', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        if (this.workType == 1) {
          request({
            url: '/yzy/zy_zbkc/delete',
            data: { yzz_id: id }
          }).then(res => {
            if (res.code == 1) {
              this.$message.success("删除成功！");
              this.initData();
            }
          })
        } else if (this.workType == 2) {
          request({
            url: '/yzy/zy_kczy/delete',
            data: { yzk_id: id }
          }).then(res => {
            if (res.code == 1) {
              this.$message.success("删除成功！");
              this.initData();
            }
          })
        }
      }).catch(() => {

      })
    }
  },
  created() {
    this.initData();
  }
}
</script>

<style lang="scss" scoped>
.createWork {
  padding: 0 40px;
  margin: 50px 0;
  .recordDiv {
    .record-list {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 14px;
      .el-button--primary.is-plain {
        padding: 0 16px;
        height: 24px;
        line-height: 24px;
        color: #019ff9;
        border: 1px solid #00a3ff;
        border-radius: 12px;
        background: transparent;
        font-size: 12px;
      }
    }
    .el-divider--horizontal {
      margin: 15px 0;
    }
  }
  .paginationDiv {
    position: absolute;
    right: 30px;
    bottom: 15px;
    padding: 3px;
  }
}
</style>