<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 label="行业" prop="intendedIndustries">
          <el-select v-model="queryModel.intendedIndustries" size="mini" filterable placeholder="请选择" clearable>
            <el-option
                  v-for="result in intendedIndustriesList"
                  :key="result.id"
                  :label="result.name"
                  :value="result.value"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="职位名称" prop="positionName">
          <el-input type="text" size="mini" v-model="queryModel.positionName"></el-input>
        </el-form-item>
        <el-form-item label="结算方式" prop="method">
          <el-select v-model="queryModel.method" size="mini" filterable placeholder="请选择" clearable>
            <el-option
                  v-for="result in methodList"
                  :key="result.id"
                  :label="result.name"
                  :value="result.value"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="工作地区" prop="workArea">
          <el-input type="text" size="mini" v-model="queryModel.workArea"></el-input>
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="queryModel.status" size="mini" filterable placeholder="请选择" clearable>
            <el-option
                  v-for="result in statusList"
                  :key="result.id"
                  :label="result.name"
                  :value="result.value"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTopping">
          <el-select v-model="queryModel.isTopping" size="mini" filterable placeholder="请选择" clearable>
            <el-option
                  v-for="result in isToppingList"
                  :key="result.id"
                  :label="result.name"
                  :value="result.value"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="是否上架" prop="isOnline">
          <el-select v-model="queryModel.isOnline" size="mini" filterable placeholder="请选择" clearable>
            <el-option
                  v-for="result in isOnlineList"
                  :key="result.id"
                  :label="result.name"
                  :value="result.value"
                ></el-option>
            </el-select>
        </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-button
          type="primary"
          size="small"
          plain
          icon="el-icon-upload"
          @click="batchImportVisible = true"
        >导入</el-button>
        <el-button
          type="primary"
          size="small"
          plain
          icon="el-icon-upload2"
          @click="exportXls"
        >导出数据</el-button>
      </el-row>
      <el-table
        :data="tableData"
        v-loading="loading"
        stripe
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
        :key="timeStamp"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column  prop="enterpriseName"  label="企业名称" width="200">
            <template slot-scope="{row}">
                <span>{{row.enterpriseName}}</span>
          </template>
        </el-table-column>
        <el-table-column  prop="industryN"  label="行业"></el-table-column>
        <el-table-column  prop="positionName"  label="职位名称" width="180"></el-table-column>
        <el-table-column  prop="salary"  label="薪资待遇"></el-table-column>
        <el-table-column  prop="settlementMethodN"  label="结算方式"></el-table-column>
        <el-table-column  prop="ageRequirement"  label="年龄要求"></el-table-column>
        <el-table-column  prop="recruitingNumbers"  label="招聘人数"></el-table-column>
        <el-table-column  prop="contacts"  label="联系人"></el-table-column>
        <el-table-column  prop="contactsPhone"  label="联系电话" width="120"></el-table-column>
        <el-table-column  prop="workArea"  label="工作地区"></el-table-column>
        <el-table-column  prop="address"  label="详细地址" width="200"></el-table-column>
        <el-table-column  prop="browseNumber"  label="浏览次数"></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  prop="isTopping"  label="是否置顶">
          <template slot-scope="{row}">
            <span v-if="row.isTopping">
              已置顶
            </span>
          </template>
        </el-table-column>
        <el-table-column  prop="isOnline"  label="是否上架">
          <template slot-scope="{row}">
            <span v-if="row.isOnline">
              <el-link type="success" @click="handleChangeIsOnline(row)">上架</el-link>
            </span>
            <span v-else>
              <el-link type="info"  @click="handleChangeIsOnline(row)">下架</el-link>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作"  fixed="right"  width="450">
          <template slot-scope="{row}">
            <el-button class="btu1" size="mini" type="warning" @click="handleEdit(row)">编辑</el-button>
            <el-button class="btu1" v-if="row.status=='0'" size="mini" type="primary" @click="handleCheck(row)">审核</el-button>
            <el-button class="btu1" size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
            <el-button class="btu1" v-if="row.isTopping" size="mini" type="info" @click="handleTopping(row)">不置顶</el-button>
            <el-button class="btu1" v-else size="mini" type="info" @click="handleTopping(row)">置顶</el-button>
            <el-badge v-if="row.isReadNumber!=0" :value="row.isReadNumber" class="item">
              <el-button size="mini" type="success" @click="handleRelation(row)">收到报名</el-button>
            </el-badge>
            <el-button v-else size="mini" type="success" @click="handleRelation(row)">收到报名</el-button>


            <!-- <el-link type="warning" @click="handleEdit(row)">编辑-</el-link>
            <el-link type="primary" v-if="row.status=='0'" @click="handleCheck(row)">审核-</el-link>
            <el-link type="danger"  @click="handleDelete(row)">删除-</el-link>
            <el-badg v-if="row.isReadNumber!=0" :value="row.isReadNumber">
              <el-link type="success" @click="handleRelation(row)">收到报名</el-link>
            </el-badg>
            <el-link v-else type="success" @click="handleRelation(row)">收到报名</el-link> -->
          </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>
      <enterpriseInfo-detail
        v-if="showModal"
        ref="userDetail"
        :businessKey="businessKey"
        :title="modalTitle"
        @close="onDetailModalClose"
      ></enterpriseInfo-detail>
      <recruitPersonRelation-list
        v-if="showModal2"
        :businessKey="businessKey"
        :title="modalTitle2"
        @close="onDetailModalClose2"
      ></recruitPersonRelation-list>
      <recruitInformationInfo-detail
        v-if="showModal3"
        :businessKey="businessKey"
        :title="modalTitle3"
        @close="onDetailModalClose3"
      ></recruitInformationInfo-detail>
      <el-dialog
      title="批量导入企业"
      :visible.sync="batchImportVisible"
      :modal-append-to-body="false"
      style="text-align: left"
      :close-on-click-modal="false"
    >
      <el-form label-width="150px">
        <el-form-item label="模板下载">
          <el-link
            href="https://oss.xiaoxinda.com/jk-temporary-workers/recruitInformationInfo.xls"
            type="primary"
            target="_blank"
            >点击下载模板</el-link
          >
        </el-form-item>
        <el-form-item label="上传文件">
          <el-upload
            class="upload-demo"
            accept=".xls"
            :action="uploadUrlXls"
            :data="uploadXlsData"
            :headers="headers"
            :on-preview="handleBatchImportPreview"
            name="uploadFile"
            :multiple="true"
            :limit="1"
            :on-remove="handleBatchImportRemove"
            :before-remove="beforeBatchImportRemove"
            :before-upload="beforeUpload"
            :on-exceed="handleBatchImportExceed"
            :on-success="handleBatchImportSuccess"
            :file-list="batchImportFileList"
          >
            <el-button size="small" type="primary" :loading="xlsLoading"
              >点击上传</el-button
            >
            <div slot="tip" class="el-upload__tip">
              只能上传xls文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="batchImportVisible = false"
          >关 闭</el-button
        >
      </div>
    </el-dialog>
    </div>
  </template>
  <script>
  import Constant from "@/constant";
  import recruitInformationInfoApi from "@/api/base/recruitInformationInfo";
  import enterpriseInfoDetail from "./enterpriseInfo-detail";
  import recruitPersonRelationList from "./recruitPersonRelation-list";
  import recruitInformationInfoDetail from "./recruitInformationInfo-detail";
  import dataDictionaryApi from "@/api/sys/dataDictionary";
  import { getToken } from "@/utils/auth"; // get token from cookie

  export default {
    name: 'baseRecruitInformationInfoList',
    data() {
      var self = this;
  
      return {
        queryModel: {
          name: "",
          positionName:"",
          intendedIndustries:"",
          method:"",
          status:"",
          workArea:"",
          isOnline:"",
          isTopping:"",
        },
        loading: false,
        tableData: [],
        pageIndex: 1,
        pageSize: 10,
        totalPages: 0,
        totalElements: 0,
        field: "",
        direction: "",
        pageSizeList: [10, 20, 30],
        multipleSelection: [],
        modalTitle: "",
        modalTitle2: "",   
        modalTitle3: "",      
        businessKey: "",
        showModal: false,
        showModal2:false,
        showModal3:false,
        dialogFormVisible:false,
        newPassword:"",
        intendedIndustriesList:[],
        methodList:[],
        statusList:[
          {
            "id":"0",
            "name":"未审核",
            "value":"0"
          },
          {
            "id":"1",
            "name":"已审核",
            "value":"1"
          }
        ],
        props: {
          // 配置项（必选）
          value: "id",
          label: "name",
          children: "children"
        },
        batchImportVisible: false,
        xlsLoading: false,
        batchImportFileList: [],
        uploadUrlXls: Constant.serverUrl + "/base/recruitInformationInfo/importXls",
        uploadXlsData: {
            subFolder: "recruitInformationInfo",
        },
        headers: {
            Authorization: getToken(),
        },
        timeStamp:"",
        isToppingList:[
          {
            "id":"0",
            "name":"未置顶",
            "value":"0"
          },
          {
            "id":"1",
            "name":"已置顶",
            "value":"1"
          }
        ],
        isOnlineList:[
          {
            "id":"0",
            "name":"下架",
            "value":"0"
          },
          {
            "id":"1",
            "name":"上架",
            "value":"1"
          }
        ]
      };
    },
    created() {
        dataDictionaryApi.findByCatalogName({catalogName: "意向行业",})
        .then((response) => {
            var jsonData = response.data;
            this.intendedIndustriesList = jsonData.data;
        });

        dataDictionaryApi.findByCatalogName({catalogName: "结算方式",})
        .then((response) => {
            var jsonData = response.data;
            this.methodList = jsonData.data;
        });
    },
    methods: {
      changePage(pageIndex,exportFlag) {
        var self = this;
  
        console.log(pageIndex);
  
        self.pageIndex = pageIndex;
        var formData = new FormData();
  
        formData.append("pageIndex", self.pageIndex);
        if(exportFlag!=null){
            formData.append("exportFlag", exportFlag);
            formData.append("pageSize", 10000);
        }
        else{
            formData.append("pageSize", self.pageSize);
        }
  
        formData.append("name", self.queryModel.name);
        formData.append("positionName", self.queryModel.positionName);
        formData.append("intendedIndustries", self.queryModel.intendedIndustries);
        formData.append("method", self.queryModel.method);
        formData.append("status", self.queryModel.status);
        formData.append("workArea", self.queryModel.workArea);
        formData.append("isOnline", self.queryModel.isOnline);
        formData.append("isTopping", self.queryModel.isTopping);
        
  
        self.loading = true;
  
        recruitInformationInfoApi.pageList(formData).then(function(response) {
          self.loading = false;
            var jsonData = response.data;
            if(jsonData.result) {
                if(exportFlag){
                    //导出
                    self.$message({
                        showClose: true,
                        type: "success",
                        message: `报表已生成，<a href="${jsonData.data}">请点击链接下载</a>`,
                        dangerouslyUseHTMLString: true,
                        duration: 30000
                    });
                }
                else{
                    //分页查看
                    var page = jsonData.data;

                    self.tableData = page.data;
                    self.totalPages = page.totalPages;
                    self.totalElements = page.recordsTotal;
                }
            }
            else{
                self.$message.warning(jsonData.message);
            }
        }).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.modalTitle3 = "新增招聘信息";
        self.businessKey = "";
        self.showModal3 = true;
      },
      handleEdit(record) {
        var self = this;
        
        self.modalTitle3 = "编辑招聘信息";
        self.businessKey = record.id;
        self.showModal3 = true;
      },
      handleRelation(record) {
        var self = this;
        self.modalTitle2 = "收到报名列表";
        self.businessKey = record.id;
        self.showModal2 = true;
      },
      handleCheck(record){
        var self = this;
  
        this.$confirm("是否确认审核？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
            recruitInformationInfoApi.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: "审核成功!"
              });
            }
          });
        });
      },
      handleChangeIsOnline(record){
        var self = this;

        recruitInformationInfoApi.changeIsOnline(record.id).then(function(response) {
          var jsonData = response.data;

          if (jsonData.result) {
            self.changePage(self.pageIndex);

            self.$message({
              type: "success",
              message: "修改成功!"
            });
          }
        });

      },
      handleTopping(record){
        var self = this;

        recruitInformationInfoApi.changeTopping(record.id).then(function(response) {
          var jsonData = response.data;

          if (jsonData.result) {
            self.changePage(self.pageIndex);

            self.$message({
              type: "success",
              message: "置顶成功!"
            });
          }
        });
      },
      handleDelete(record) {
        var self = this;
  
        this.$confirm("是否确认删除？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
            recruitInformationInfoApi.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(() => {
            recruitInformationInfoApi.batchRemove(idList).then(function(response) {
            var jsonData = response.data;
  
            if (jsonData.result) {
              self.changePage(self.pageIndex);
  
              self.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        });
      },
      exportXls() {
        this.changePage(1,true);
      },
      onDetailModalClose(refreshed) {
        this.showModal = false;
  
        if(refreshed) {
          this.changePage(this.pageIndex);
        }
      },
      onDetailModalClose2(refreshed,status) {
        this.showModal2 = false;
  
        if(refreshed||status) {
          this.changePage(this.pageIndex);
        }
      },
      onDetailModalClose3(refreshed) {
        this.showModal3 = false;
  
        if(refreshed) {
          this.changePage(this.pageIndex);
        }
      },
      beforeUpload(file, fileList) {
          //导入前判断
          var self = this;
          self.xlsLoading = true;
          return true;
      },
              //批量导入-上传成功
              handleBatchImportSuccess(response, file, fileList) {
        var self = this;
        self.xlsLoading = false;

        if (response.result) {
            self.$message.success(response.message);
            this.batchImportFileList = [];
            this.changePage(1);


        } else {
            //this.$message.error(response.message);
            this.batchImportFileList = [];
            this.changePage(1);

            if (response.data != null) {
            //下载有错误信息提示的报表
            //window.open(response.data);
            self.$message({
                showClose: true,
                dangerouslyUseHTMLString: true,
                message:
                response.message +
                `,<a href="${response.data}" target="_blank">点击下载未导入的数据报表</a>&nbsp;`,
                duration: 30000,
            });
            }
        }

        this.batchImportVisible = false;
      },
      //批量导入-预览
      handleBatchImportPreview(file) {
        console.log(file.url);
      },
      //批量导入-移除
      handleBatchImportRemove(file, fileList) {
        console.log(file, fileList);
      },
      //批量导入-移除前操作
      beforeBatchImportRemove(file, fileList) {
        console.log(file, fileList);
      },
      //批量导入-文件超出个数限制时的钩子
      handleBatchImportExceed(files, fileList) {
      this.$message.warning(
          `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
          } 个文件`
        );
      },
    },
    mounted: function() {
      var self = this;
      this.changePage(1);
    },
    components: {
      "enterpriseInfo-detail":enterpriseInfoDetail,
      "recruitPersonRelation-list":recruitPersonRelationList,
      "recruitInformationInfo-detail":recruitInformationInfoDetail
    }
  };
  </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;
  }
  .item{
    margin-left: 10px;
  }
.btu1{
  margin-top: 10px;
  margin-right: 15px;
}
  </style>