<template>
  <div class="card-search">
    <a-form ref="formRef" name="advanced_search" class="ant-advanced-search-form" :model="searchParams">
      <a-row :gutter="24">
        <a-col :span="6">
          <a-form-item label="岗位名称" :label-col="{span:6}" name="postName">
            <a-input v-model:value="searchParams.postName" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="" :label-col="{span:3}" name="">
            <label style="margin-left:26px;">招聘人数：</label>
            <a-input type="number" v-model:value="searchParams.minCount" style="width: 30%;margin-right: 10px;"
                     placeholder=""/>
            <a-input type="number" v-model:value="searchParams.maxCount" style="width: 30%;" placeholder=""
            />
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="招聘企业" :label-col="{span:6}" name="companyName">
            <a-input v-model:value="searchParams.companyName" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="6" style="text-align: left">
          <a-button type="primary" html-type="submit" @click="onSearch">查询</a-button>
          <a-button
            style="margin: 0 8px"
            @click="
              () => {
                formRef.resetFields();
                loadData();
              }
            ">重置</a-button>
          <a style="font-size: 12px" @click="expand = !expand">
            <template v-if="expand">
              <UpOutlined />
            </template>
            <template v-else>
              <DownOutlined />
            </template>
            {{ expand ? '收缩' : '展开' }}
          </a>
        </a-col>
      </a-row>
      <a-row :gutter="24" v-show="expand">
        <a-col :span="6">
          <a-form-item label="岗位状态" :label-col="{span:6}" name="recordStatus">
            <a-select
              ref="select"
              v-model:value="searchParams.recordStatus"
              :options="postStatusList"
              :field-names="{ label: 'name', value: 'value' }"
              :allow-clear="true"
              @change="loadData"
            >
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="工种名称" :label-col="{span:6}" name="WorkNmae">
            <a-input v-model:value="searchParams.WorkNmae" placeholder=""/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="edit-operation">
        <a-col :span="24" style="text-align: right">
          <a-button type="primary" html-type="submit" functioncode="T01020202" @click='onAdd'>新增</a-button>
          <BImportExcel
            functioncode="T01020205"
            :options="importOptions"
            @success="loadData"
          ></BImportExcel>
          <BExportExcel :title="'导出'" :filename="'岗位信息'"
                        :url="'companyService/post/export'"
                        :params="{...searchParams, isExport: true, rows:10000}"></BExportExcel>
        </a-col>
      </a-row>
    </a-form>
    <div class="search-result-list">
      <a-table :columns="columns" :data-source="dataList" :scroll="{ x:'100%', y: 500 }" :pagination="pagination"
               :loading="formState.loading"
               @change="handleTableChange"
               :row-selection="{ selectedRowKeys: formState.selectedRowKeys, onChange: onSelectChange}"
               :row-key="record=>record.postID"
               bordered>
        <template #bodyCell="{ column, text, record }">
          <template v-if="column.key === 'operation'">
            <div class="table-operation">
              <a-button type="link" size="small" @click='onEdit(record)' functioncode="T01020203">编辑</a-button>
              <a-button type="link" size="small" @click="onDel(record)" functioncode="T01020204">删除</a-button>
            </div>
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script lang="ts">
import {reactive, ref, computed, defineComponent, createVNode} from 'vue';
import {DownOutlined, UpOutlined} from '@ant-design/icons-vue';
import type {FormInstance} from 'ant-design-vue';
import {Modal} from 'ant-design-vue';
import type {TableColumnsType, TableProps} from 'ant-design-vue';
import {getList, del} from '@/api/companyService/post';
import BExportExcel from "@/components/basic/excel/exportExcel/exportExcel.vue";
import BImportExcel from '@/components/basic/excel/importExcel/importExcel.vue';
import {getPaginationTotalTitle} from "@/utils/common";
import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
import dayjs from 'dayjs';
import {useRoute} from 'vue-router';
import {useTabsViewStore} from "@/store/modules/tabsView";
import type {ImportProps} from "@/components/basic/excel/importExcel/ImportProps";

export default defineComponent({
  components: {DownOutlined, UpOutlined, BExportExcel, BImportExcel},
  setup: function () {
    const formRef = ref<FormInstance>();
    const searchParams = reactive({
      pageIndex: 1,
      pageSize: 20,
      primaryKey: '',
      minCount: null,
      maxCount: null,
      recordStatus: null
    });
    const expand = ref(false);
    const postStatusList = [{name: '启用', value: 1}, {name: '停用', value: 0}];
    const importOptions = ref<ImportProps>({
      title: '导入',
      url: 'companyService/post/importPost  ',
      columns: [
        {cnName: '岗位名称', enName: 'postName', width: 100},
        {cnName: '有效开始时间', enName: 'validTime', width: 100},
        {cnName: '有效期(天)', enName: 'validDay', width: 100},
        {cnName: '岗位状态', enName: 'recordStatusName', width: 100},
        {cnName: '所属企业', enName: 'companyName', width: 100},
        {cnName: '工作性质', enName: 'workNature', width: 100},
        {cnName: '工作年限', enName: 'workYear', width: 100},
        {cnName: '招聘人数', enName: 'recruitCount', width: 100},
        {cnName: '文化程度', enName: 'cultureLevelName', width: 100},
        {cnName: '最高薪酬', enName: 'maxSalary', width: 100},
        {cnName: '最低薪酬', enName: 'minSalary', width: 100},
        {cnName: '福利待遇', enName: 'welfare', width: 100},
        {cnName: '联系人', enName: 'userName', width: 100},
        {cnName: '联系电话', enName: 'userMobile', width: 100},
        {cnName: '邮箱', enName: 'postEmail', width: 100},
        {cnName: '工作时长', enName: 'workTime', width: 100},
        {cnName: '是否试用期', enName: 'isTrailName', width: 100},
        {cnName: '试用期时长(月)', enName: 'trailtime', width: 100},
        {cnName: '试用期最高薪酬', enName: 'trailMaxSalary', width: 100},
        {cnName: '试用期最低薪酬', enName: 'trailMinSalary', width: 100},
        {cnName: '岗位描述', enName: 'postDesc', width: 100},
      ],
      template: {
        tempFileName: '岗位信息导入模板.xlsx',
        url: '',
        params: null,
      },
    });
    const formState = reactive({
      total: 0,
      selectedRowKeys: [],
      loading: false
    });
    const columns: TableColumnsType = [
      {
        title: '序号',
        align: "center",
        key: 'institutionID',
        customRender: item => `${searchParams.pageSize * (searchParams.pageIndex - 1) + item.index + 1}`
      },
      {title: '岗位名称', dataIndex: 'postName', key: 'postName', align: "center"},
      {title: '招聘人数', dataIndex: 'recruitCount', key: 'recruitCount', width: 120, align: "center"},
      {
        title: '有效开始日期', dataIndex: 'validTime', key: 'validTime', align: "center", customRender: (item) => {
          return item.record.validTime == null ? "" : (dayjs(item.record.validTime).format('YYYY-MM-DD'))
        }
      },
      {title: '有效期(天)', dataIndex: 'validDay', key: 'validDay', align: "center"},
      {title: '招聘企业', dataIndex: 'companyName', key: 'companyName', align: "center"},
      {
        title: '岗位状态', dataIndex: 'recordStatus', key: 'recordStatus', align: "center", customRender: (item) => {
          return item.record.recordStatus == 1 ? "启用" : "停用";
        }
      },
      {title: '工种名称', dataIndex: 'workNmae', key: 'workNmae', align: "center"},
      {title: '文化程度', dataIndex: 'cultureLevelName', key: 'cultureLevelName', align: "center"},
      {
        title: '薪酬', dataIndex: 'siteCount', key: 'siteCount', align: "center", customRender: (item) => {
          const salary = `${item.record.minSalary??""}~${item.record.maxSalary??""}`
          return salary;
        }
      },
      {title: '联系人', dataIndex: 'userName', key: 'userName', align: "center"},
      {title: '联系电话', dataIndex: 'userMobile', key: 'userMobile', align: "center"},
      {title: '操作', key: 'operation', fixed: 'right', width: 170, align: "center"},
    ];
    const pagination = computed(() => ({
      total: formState.total,
      current: searchParams.pageIndex,
      pageSize: searchParams.pageSize,
      showSizeChanger: true,
      showTotal: total => getPaginationTotalTitle(total)
    }));

    const tabsViewStore = useTabsViewStore();
    const dataList = ref([]);
    const loadData = async function () {
      formState.loading = true;
      const result: any = await getList(searchParams);
      dataList.value = result.list;
      console.log(dataList.value);
      formState.total = result.total;
      formState.loading = false;
    }

    const onAdd = () => {
      tabsViewStore.addTabByPath('/companyService/post/add', {});
    };
    const onEdit = (item: any) => {
      console.log(item);
      console.log(item.postID);
      tabsViewStore.addTabByPath('/companyService/post/edit', {id: item.postID});
    };

    const handleTableChange: TableProps['onChange'] = (pag: { pageSize: number; current: number },) => {
      searchParams.pageIndex = pag.current;
      searchParams.pageSize = pag.pageSize;
      loadData();
    };

    const onSelectChange = (selectedRowKeys: any) => {
      formState.selectedRowKeys = selectedRowKeys;
    };

    const onSearch = () => {
      loadData();
    }

    const onDel = (item: any) => {
      console.log(item.postID);
      Modal.confirm({
        title: '确认删除该岗位？',
        icon: createVNode(ExclamationCircleOutlined),
        content: '',
        okText: '确认删除',
        okType: 'danger',
        okButtonProps: {},
        cancelText: '取消',
        onOk() {
          del(item.postID).then(() => {
            loadData();
          });
        },
        onCancel() {
        },
      })
    };

    const importPost = () => {
      console.log('导入');
    }
    const exportPost = () => {
      console.log('导出');
    }

    return {
      formRef,
      searchParams,
      formState,
      columns,
      pagination,
      dataList,
      importOptions,
      handleTableChange,
      onSelectChange,
      onSearch,
      importPost,
      exportPost,
      onDel,
      loadData,
      onAdd,
      onEdit,
      expand,
      postStatusList
    };
  },
  created() {
    this.loadData();
  },
  activated() {
    const route = useRoute();
    if (route.params.reload) this.loadData();
  }
});
</script>
<style lang="less" scoped></style>
