<template>
    <div>
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>
          <a href="#">基础信息管理</a>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          培训信息
        </el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider></el-divider>
      <!--
        要resetFields起作用，必须配置:model和prop
      -->
      <el-form ref="queryForm" :model="queryModel" inline class="demo-form-inline">
        <el-form-item label="标题" prop="name">
          <el-input type="text" size="mini" v-model="queryModel.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            icon="ios-search"
            @click="changePage(1)"
            :loading="loading"
          >查询</el-button>&nbsp;
          <el-button
            type="info"
            size="mini"
            style="margin-left: 8px"
            @click="handleReset('queryForm')"
          >重置</el-button>&nbsp;
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-row class="button-group">
        <el-button type="primary" size="small" plain icon="el-icon-circle-plus" @click="handleAdd">新增</el-button>
        <el-button
          type="primary"
          size="small"
          plain
          icon="el-icon-remove"
          :disabled="multipleSelection.length==0"
          @click="handleBatchDelete"
        >删除选中项</el-button>
      </el-row>
      <el-table
        :data="tableData"
        style="min-height:400px;"
        v-loading="loading"
        stripe
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column  prop="thumbnailImage"  label="缩略图">
            <template slot-scope="{row}">
            <a v-if="row.thumbnailImage" :href="row.thumbnailImage" target="_blank">
              <el-image
              style="width: 50px; height: 50px"
              :src="row.thumbnailImage"
              fit="fit"></el-image>
            </a>
          </template>
        </el-table-column>
        <el-table-column  prop="enterpriseName"  label="企业名称"></el-table-column>
        <el-table-column  prop="name"  label="名称"></el-table-column>
        <el-table-column  prop="title"  label="标题" width="200"></el-table-column>

        <el-table-column  prop="file"  label="下载文件" width="350">
          <template slot-scope="{row}">
            <div v-for="(item,index) in row.fileList" v-bind:key="index">
              <el-link :href="item.url" target="_blank" type="primary">{{item.name}}</el-link>
            </div>
          </template>
        </el-table-column>
        <el-table-column  prop="endTime"  label="结束时间" width="130"></el-table-column>
        <el-table-column  prop="status"  label="审核状态">
          <template slot-scope="{row}">
            <span v-if="row.status=='0'" style="color: #F56C6C;">未审核</span>
            <span v-else-if="row.status=='1'" style="color: #67C23A;">已审核</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="300">
          <template slot-scope="{row}">
            <el-button size="mini" type="warning" @click="handleEdit(row)">编辑</el-button>
            <el-button v-if="row.status=='0'" size="mini" type="primary" @click="handleCheck(row)">审核</el-button>
            <el-button size="mini" type="success" @click="handlePersonList(row)">报名人员</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="pageIndex"
        :total="totalElements"
        :page-sizes="pageSizeList"
        @current-change="changePage"
        @size-change="pageSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
      <trainingInfo-detail
        v-if="showModal"
        ref="userDetail"
        :businessKey="businessKey"
        :title="modalTitle"
        @close="onDetailModalClose"
      ></trainingInfo-detail>
      <trainingPerson-list
        v-if="showModal2"
        :businessKey="businessKey"
        :title="modalTitle2"
        @close="onDetailModalClose2"
      ></trainingPerson-list>
    </div>
  </template>
  <script>
  import trainingInfoApi from "@/api/base/trainingInfo";
  import trainingInfoDetail from "./trainingInfo-detail";
  import trainingPersonList from "./trainingPerson-list";
  
  export default {
    name: 'baseTrainingInfoList',
    data() {
      var self = this;
  
      return {
        queryModel: {
          name: "",
        },
        loading: false,
        tableData: [],
        pageIndex: 1,
        pageSize: 10,
        totalPages: 0,
        totalElements: 0,
        field: "",
        direction: "",
        pageSizeList: [10, 20, 30],
        multipleSelection: [],
        modalTitle: "",
        modalTitle2: "",          
        businessKey: "",
        showModal: false,
        showModal2: false,
        dialogFormVisible:false,
        newPassword:"",
        roleResult:[],
        props: {
          // 配置项（必选）
          value: "id",
          label: "name",
          children: "children"
        },
      };
    },
    methods: {
      changePage(pageIndex) {
        var self = this;
  
        console.log(pageIndex);
  
        self.pageIndex = pageIndex;
        var formData = new FormData();
  
        formData.append("pageIndex", self.pageIndex);
        formData.append("pageSize", self.pageSize);
  
        formData.append("name", self.queryModel.name);
        
  
        self.loading = true;
  
        trainingInfoApi.pageList(formData).then(function(response) {
          self.loading = false;
  
          var jsonData = response.data;
  
          if(jsonData.result){
            var pageInfo = jsonData.data;
  
            self.tableData = pageInfo.data;
            self.totalPages = pageInfo.totalPages;
            self.totalElements = pageInfo.recordsTotal;
          }
          else {
            self.$message({
                message: jsonData.message + "",
                type: "warning"
              });
          }
        }).catch((error)=>{
          self.loading = false;
        });
      },
      pageSizeChange(pageSize) {
        this.pageSize = pageSize;
      },
      sortChange(data) {
        this.field = data.column.field;
        this.direction = data.order == "ascending" ? "asc" : "desc";
  
        this.changePage(this.pageIndex);
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleReset(name) {
        this.$refs[name].resetFields();
      },
      handleAdd() {
        var self = this;
        
        self.modalTitle = "新增培训信息";
        self.businessKey = "";
        self.showModal = true;
      },
      handleEdit(record) {
        var self = this;
        
        self.modalTitle = "编辑培训信息";
        self.businessKey = record.id;
        self.showModal = true;
      },
      handleCheck(record){
        var self = this;
  
        this.$confirm("是否确认审核？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          trainingInfoApi.check(record.id).then(function(response) {
            var jsonData = response.data;

            if (jsonData.result) {
              // var index = self.tableData.indexOf(record);
              // self.tableData.splice(index, 1);
              self.changePage(self.pageIndex);

              self.$message({
                type: "success",
                message: "审核成功!"
              });
            }
          });
        });
      },
      handleDelete(record) {
        var self = this;
  
        this.$confirm("是否确认删除？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          trainingInfoApi.remove(record.id).then(function(response) {
            var jsonData = response.data;
  
            if (jsonData.result) {
              // var index = self.tableData.indexOf(record);
              // self.tableData.splice(index, 1);
              self.changePage(self.pageIndex);
  
              self.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        });
      },
      handleBatchDelete() {
        var self = this;
  
        var idList = this.multipleSelection.map(record => {
          return record.id;
        });
  
        this.$confirm("是否确认删除选中项？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          trainingInfoApi.batchRemove(idList).then(function(response) {
            var jsonData = response.data;
  
            if (jsonData.result) {
              self.changePage(self.pageIndex);
  
              self.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        });
      },
      onDetailModalClose(refreshed) {
        this.showModal = false;
  
        if(refreshed) {
          this.changePage(this.pageIndex);
        }
      },
      handlePersonList(record) {
        var self = this;
        self.modalTitle2 = "培训报名人员";
        self.businessKey = record.id;
        self.showModal2 = true;
      },
      onDetailModalClose2(refreshed) {
        this.showModal2 = false;
  
        if(refreshed) {
          this.changePage(this.pageIndex);
        }
      },
    },
    mounted: function() {
      var self = this;
      this.changePage(1);
    },
    components: {
        "trainingInfo-detail":trainingInfoDetail,
        "trainingPerson-list":trainingPersonList,
    }
  };
  </script>
  <style lang="scss" scoped>
  .el-breadcrumb {
    margin: 10px;
    line-height: 20px;
  }
  
  .el-divider {
    margin: 5px 0;
  }
  
  .demo-form-inline {
    margin-left: 10px;
    text-align: left;
  }
  
  .button-group {
    padding: 10px;
    text-align: left;
  }
  </style>