<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="招聘企业">
        <el-select
          v-model="queryParams.orgId"
          placeholder="请选择"
          style="width: 220px"
        >
          <el-option
            v-for="item in orgList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="招聘类型" prop="recruitTypeId">
          <el-select
              v-model="queryParams.recruitTypeId"
              placeholder="请选择"
              filterable
              style="width: 100%"
          >
          <el-option
              v-for="item in recruitAllList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
          </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="qualificationId">
          <el-select
              v-model="queryParams.qualificationId"
              placeholder="请选择"
              filterable
              style="width: 100%"
          >
          <el-option
              v-for="item in qualificationList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
          </el-select>
      </el-form-item>
      <el-form-item label="经验" prop="experienceId">
          <el-select
              v-model="queryParams.experienceId"
              placeholder="请选择"
              filterable
              style="width: 100%"
          >
          <el-option
              v-for="item in experienceList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @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="Plus"
            @click="visibleOpenpay = true; detailInfo = {}"
        >新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" show-overflow-tooltip>
      <el-table-column label="招聘企业" prop="orgName" />
      <el-table-column label="职位" prop="name" />
      <el-table-column label="职位类型" prop="industryName" />
      <el-table-column label="招聘类型" prop="recruitType" />
      <el-table-column label="经验" prop="experience" align="center"  />
      <el-table-column label="学历" prop="qualification" />
      <el-table-column label="地址" prop="address">
        <template #default="{row}">
          {{ row.province }} {{ row.city }} {{ row.region }} {{ row.address }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template #default="{row}">
          {{ row.status == 'up' ? '上架' : '下架' }}
        </template>
      </el-table-column>
      <el-table-column label="操作人" prop="userName" align="center" width="130"/>
      <el-table-column label="创建时间" prop="createTime" align="center" width="160"/>
      <el-table-column label="操作" width="280" align="center" class-name="small-padding fixed-width" >
        <template #default="{row}">
          <el-button v-if="row.status == 'up'" link type="primary" icon="Edit" @click="toggleStatus(row)">下架</el-button>
          <el-button v-if="row.status == 'down'" link type="primary" icon="Edit" @click="toggleStatus(row)">上架</el-button>
          <el-button v-if="row.status == 'down'" link type="primary" icon="Edit" @click="visibleOpenpay = true; detailInfo = {...row,title: '编辑岗位信息'}">编辑</el-button>
          <el-button link type="primary" icon="View" @click="visibleOpenpay = true; detailInfo = {...row,title: '岗位详情'}">详情</el-button>
          <el-button v-if="row.status == 'down' && row.launch != '1'" link type="primary" icon="Delete" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
    <Add
      :addVisible="visibleOpenpay"
      @changeVisible="visibleOpenpay = false"
      @getList="getList"
      :detailInfo="detailInfo"
      :selectOp="{
        recruitAllList,
        experienceList,
        qualificationList,
        orgList,
        industryList
      }"
    />
  </div>
</template>

<script setup>
import { recruitList,recruitDel,recruitTypeAll,experienceAll,qualificationAll,orgListAll,industryListAll,recruitStatus } from "@/api/recruit";
import { ref } from "vue";
import Add from "./add.vue";
const { proxy } = getCurrentInstance();
const list = ref([]);
const loading = ref(false);
const total = ref(0);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
})
const recruitAllList = ref([])
const experienceList = ref([])
const qualificationList = ref([])
const orgList = ref([])
const industryList = ref([])

const visibleOpenpay = ref(false)
recruitTypeAll().then(res => {
  recruitAllList.value = res.data.list
})
industryListAll().then(res => {
  industryList.value = res.data
})
orgListAll().then(res => {
  orgList.value = res.data.list
})
experienceAll().then(res => {
  res.data.list.forEach(el => {
    el.name = el.description
  });
  experienceList.value = res.data.list
})
qualificationAll().then(res => {
  qualificationList.value = res.data.list
})
function toggleStatus(row) {
  const text = row.status == 'up' ? '下架' : '上架'
  proxy.$modal.confirm(`确定要${text}吗？`).then(function() {
      return recruitStatus({id: row.id});
  }).then(() => {
      getList();
      proxy.$modal.msgSuccess(`${text}成功`);
  }).catch(() => {});
}
/** 查询列表 */
function getList() {
  const form = { ...queryParams.value }
  if (form.time && form.time.length == 2) {
    form.startTime = form.time[0] + ' 00:00:00'
    form.endTime = form.time[1] + ' 23:59:59'
  }
  delete form.time;
  loading.value = true;
  recruitList(form).then(res => {
    list.value = res.data.list;
    total.value = res.data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
  }
  handleQuery();
}
function del(row) {
    proxy.$modal.confirm('您确定要删除吗？').then(function() {
      return recruitDel({
        id: row.id,
      });
    }).then((res) => {
      getList();
      proxy.$modal.msgSuccess('删除成功');
    }).catch(() => {});
}
getList();
</script>
<style lang="scss" scoped>
.el-dialog .el-dialog__body {
  max-height: 650px;
}
</style>