<template>
  <div class="app-container">
    <!-- 当vue的搜索框form表单有且只有一个输入框时，点击回车，会触发刷新整个页面，
      可使用 @submit.native.prevent 阻止页面刷新 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      @submit.native.prevent
    >
      <el-form-item label="职位名称">
        <el-input
          v-model="queryParams.jobName"
          placeholder="请输入职位名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="角色状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="selectRows.length !== 1"
          @click="handleUpdate"
          >编辑</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="!selectRows.length"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- @selection-change="handleSelectionChange" -->
    <el-table v-loading="loading" :data="tableData">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="职位编号" prop="hireInfoId" width="120" />
      <el-table-column
        label="职位名称"
        prop="jobName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="工作地点"
        prop="jobLocation"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="薪资" prop="salary" />
      <el-table-column label="资历" prop="seniority" />
      <el-table-column label="福利" prop="benefit" />

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="0"
            inactive-value="1"
            @change="changeStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增编辑弹窗 width="500px"-->
    <el-dialog :title="title" :visible.sync="dlgVisible" append-to-body>
      <addJob
        :loading="dlgLoading"
        :infoData="infoData"
        @save="handleSave"
        @cancel="handleCancel"
      ></addJob>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  getDetail,
  addHireInfo,
  editHireInfo,
  editHireInfoStatus,
  delHireInfo,
} from "@/api/recruitInfoManage";

import addJob from "./components/addJob.vue";

export default {
  name: "RecruitInfoManage",
  components: { addJob },
  dicts: [],
  data() {
    return {
      // 是否展示筛选项
      showSearch: true,
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobName: "",
      },
      // 数据量
      total: 0,
      // 表格数据
      tableData: [],
      // 选中的数据
      selectRows: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 弹窗相关
      dlgVisible: false,
      title: "",
      infoData: {},
      dlgLoading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        jobName: "",
      };
      this.getList();
    },
    // 查询列表
    getList() {
      this.loading = true;
      getList(this.queryParams)
        .then((res) => {
          console.log(res);
          this.tableData = res.rows;
          this.total = res.total;
        })
        .finally(() => (this.loading = false));
    },
    // 新增
    handleAdd() {
      this.dlgVisible = true;
      this.title = "新增";
      this.infoData = {};
    },
    // 修改
    handleUpdate(row) {
      this.dlgVisible = true;
      this.title = "编辑";
      const hireInfoId = row.hireInfoId
        ? row.hireInfoId
        : this.selectRows[0].hireInfoId;
      this.dlgLoading = true;
      getDetail(hireInfoId)
        .then((res) => {
          console.log(res);
          this.infoData = res.data;
        })
        .finally(() => (this.dlgLoading = false));
    },
    // 确认新增/修改
    handleSave(data) {
      this.dlgLoading = true;
      if (data.hireInfoId) {
        editHireInfo(data)
          .then((res) => {
            this.$message.success(res.msg);
            this.handleCancel();
            this.getList();
          })
          .finally(() => (this.dlgLoading = false));
      } else {
        addHireInfo(data)
          .then((res) => {
            this.$message.success(res.msg);
            this.handleCancel();
            this.getList();
          })
          .finally(() => (this.dlgLoading = false));
      }
    },
    // 关闭弹窗
    handleCancel() {
      this.dlgVisible = false;
    },
    // 删除
    handleDelete(row) {
      const ids = this.selectRows.map((item) => item.hireInfoId).join();
      const hireInfoIds = row.hireInfoId || ids;
      this.$modal
        .confirm("是否确认删除数据")
        .then(function () {
          return delHireInfo(hireInfoIds);
        })
        .then((res) => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleSelectionChange(selection) {
      this.selectRows = selection;
    },
    // 修改招聘信息状态
    changeStatus(row) {
      let params = {
        hireInfoIds: row.hireInfoId,
        status: row.status,
      };
      // console.log(params, 1211);
      this.$modal
        .confirm("是否确认修改状态")
        .then(function () {
          return editHireInfoStatus(params);
        })
        .then((res) => {
          this.getList();
          this.$modal.msgSuccess(res.msg);
        })
        .catch((err) => {
          console.log(err, "err");
          row.status = row.status === "0" ? "1" : "0";
        });
    },
  },
};
</script>
