<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <!-- 搜索栏与表格区域 -->
        <div class="main-container">
            <div class="search-container TableSelectMain">
                <el-form
                    class="table-select-main widthAuto"
                    :model="searchForm"
                    label-position="left"
                >
                    <el-row :gutter="15">
                        <el-col :lg="4" :sm="10">
                            <el-form-item>
                                <SelectCompany
                                    ref="companyId"
                                    :companyName="companyName"
                                    @companyEmpty="companyEmpty"
                                    @companyChange="companyChange"
                                ></SelectCompany>
                            </el-form-item>
                        </el-col>
                        <el-col :lg="4" :sm="10">
                            <el-form-item>
                                <el-input placeholder="请输入岗位" v-model="searchForm.job"></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :lg="4" :sm="10">
                            <el-form-item>
                                <el-select placeholder="启用状态" v-model="searchForm.statuz" clearable>
                                    <el-option label="是" :value="1"></el-option>
                                    <el-option label="否" :value="0"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>-->
                        <el-col :lg="4" :sm="10" v-show="show">
                            <el-form-item>
                                <el-select
                                    placeholder="审核状态"
                                    v-model="searchForm.checkFlag"
                                    clearable
                                >
                                    <el-option label="待审核" :value="2"></el-option>
                                    <el-option label="已通过" :value="1"></el-option>
                                    <el-option label="已驳回" :value="0"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-button type="primary" plain round @click="query">查询</el-button>
                                <el-button type="primary" plain round @click="reset">重置</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24" v-show="show">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    round
                                    plain
                                    icon="el-icon-plus"
                                    @click="create(null)"
                                >新建</el-button>
                                <!-- <el-button type="primary" plain round @click="showBatch">批量展示</el-button> -->
                                <!-- <el-button type="primary" plain round @click="noshowBatch">批量取消</el-button> -->
                                <el-button type="primary" plain round @click="putaway">批量启用</el-button>
                                <el-button type="primary" plain round @click="soldOut">批量关闭</el-button>
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    @click="deleteData(ids, 'batch')"
                                >批量删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">{{ ids.length }}</span> 项
            </div>

            <div class="MaintenanceTable">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    tooltip-effect="dark"
                    @selection-change="selectionChange"
                    class="OutTable"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>
                    <el-table-column prop="company.name" label="公司" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="job" label="岗位" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column label="薪资" min-width="70px;" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{ scope.row.lowerSalary | pricePipe(0, '￥', true) }} -
                            {{ scope.row.upperSalary | pricePipe(0, '', true) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="学历" :show-overflow-tooltip="true">
                        <template slot-scope="scope">{{ scope.row.eduLevel | educationStatus }}</template>
                    </el-table-column>
                    <el-table-column prop="upperWorking" label="工作年限" :show-overflow-tooltip="true">
                        <template
                            slot-scope="scope"
                        >{{ scope.row.lowerWorking }} - {{ scope.row.upperWorking}}</template>
                    </el-table-column>
                    <el-table-column label="置顶" min-width="110px;" v-if="show">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.hotFlag"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                @change="hotFlagChange(scope.row.id, scope.row.hotFlag)"
                            ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="启用状态" min-width="110px;" v-if="show">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.statuz"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                :disabled="scope.row.checkFlag !==1"
                                @change="statuzChange(scope.row.id, scope.row.statuz)"
                            ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="审核状态" min-width="110px;">
                        <template slot-scope="scope">
                            <span class="warn" v-show="scope.row.checkFlag == 2">待审核</span>
                            <span class="success" v-show="scope.row.checkFlag == 1">审核通过</span>
                            <RejectReason
                                :content="scope.row.reason"
                                v-show="scope.row.checkFlag == 0"
                            ></RejectReason>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="200px;">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                v-show="show"
                                @click="deleteData(scope.row.id)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="edit(scope.row)"
                                v-show="show"
                                :disabled="scope.row.checkFlag === 1"
                            >编辑</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="showDetailsDialog(scope.row)"
                            >详情</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                v-show="!show"
                                @click="pass(scope.row.id)"
                            >通过</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                v-show="!show"
                                @click="rejectShow(scope.row.id)"
                            >驳回</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagenation">
                    <PageComponent
                        :total="total"
                        @pagination="handlePagination"
                        :pageSize.sync="page.pageSize"
                        :pageNum.sync="page.pageNum"
                    ></PageComponent>
                </div>
            </div>
        </div>
        <!-- 添加与编辑弹层 -->
        <AddEditDialog @queryListData="queryListData" ref="addEditDialog"></AddEditDialog>
        <!-- 详情弹层 -->
        <DetailsDialog @queryListData="queryListData" ref="detailsDialog"></DetailsDialog>
        <!-- 驳回弹窗 -->
        <RejectDialog @isReject="isReject" ref="regectDialog"></RejectDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import DetailsDialog from './components/detailsDialog';
import RejectDialog from '@/apps/mobile/components/reject';
import RejectReason from '@/apps/mobile/components/ReasonForRejection/index';
import SelectCompany from '@/apps/mobile/components/selectCompany/index';
import {
    queryPage,
    removeData,
    editData,
    queryCompany,
    useBath,
    nouseBath,
    showBath,
    noShowBath,
    examine,
    queryMaintenanceCompany,
} from '@/apps/mobile/api/recruitment/index';
import educationStatus from '../model/education-status';
export default {
    components: {
        TitleBar,
        AddEditDialog,
        DetailsDialog,
        RejectDialog,
        RejectReason,
        SelectCompany,
    },
    data() {
        return {
            searchForm: {}, // 搜索表单
            tableData: [], // 列表数据
            page: {
                // 页码
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false, // table是否展示loading
            ids: [], // 选中的列
            addEditDialogVisible: false,
            addEditDialogType: 'add',
            addEditDialogForm: {},
            companyList: [],
            show: 1,
            selection: [],
            companyName: '', //公司名称
        };
    },
    methods: {
        create() {
            this.$refs.addEditDialog.open('add', null);
        },
        edit(data) {
            this.$refs.addEditDialog.open('edit', data);
        },
        selectionChange(data) {
            this.selection = data;
            this.ids = data.map(item => {
                return item.id;
            });
        },
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },

        // 分页获取招聘信息
        queryListData() {
            // this.listLoading = true;
            let obj = {
                checkFlag: this.show ? this.searchForm.checkFlag : 2,
                ...this.page,
                ...this.searchForm,
                platform: 'web',
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryPage(obj)
                .then(res => {
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        /**
         * 删除功能
         * @param {String} type 授权操作类型:'batch'批量授权  'single'单个授权
         * @param {Number} id 当前需要删除的id或集合
         */
        deleteData(id, type = 'single') {
            let obj = {};
            if (type === 'batch') {
                if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
                // console.log(this.ids, "ids");
                obj = {
                    ids: this.ids
                }
                this.delete(obj);
            } else {
                obj = {
                    ids: [id]
                }
                this.delete(obj);
            }
        },
        delete(obj) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    removeData(obj)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(() => {});
                })
                .catch(() => {});
        },
        hotFlagChange(id, hotFlag) {
            let obj = {
                id,
                hotFlag,
            };
            this.editformData(obj);
        },
        statuzChange(id, statuz) {
            let obj = {
                id,
                statuz,
            };
            this.editformData(obj);
        },
        // 编辑
        editformData(data) {
            editData(data)
                .then(() => {
                    this.$message.success('编辑成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        showDetailsDialog(data) {
            this.$refs.detailsDialog.open(data);
        },
        // 批量启用
        putaway() {
            const ids = this.selection.filter(item => {
                return item.checkFlag === 1;
            });
            const arr = ids.map(item => {
                return item.id;
            });
            if (arr.length === 0) {
                this.$message.warning('请至少选中一项审核通过的!');
                return false;
            }
            useBath({ids: arr})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量关闭
        soldOut() {
            const ids = this.selection.filter(item => {
                return item.checkFlag === 1;
            });
            const arr = ids.map(item => {
                return item.id;
            });
            if (arr.length === 0) {
                this.$message.warning('请至少选中一项审核通过的!');
                return false;
            }
            nouseBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量首页展示
        showBatch() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            showBath(this.ids)
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量取消首页展示
        noshowBatch() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            noShowBath(this.ids)
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 审核通过  (仅在审批页面使用)
        pass(id) {
            this.$confirm('是否通过审批?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    let obj = {
                        id,
                        checkFlag: 1,
                        statuz: 1,
                    };
                    // this.editformData(obj);
                    this.approve(obj);
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消',
                    });
                });
        },
        // 审核驳回
        isReject(data) {
            console.log(data, 'is.reject');
            let obj = {
                id: data.id,
                reason: data.checkComment,
                checkFlag: 0,
            };
            this.approve(obj);
        },
        // 仅审核页面调用
        changeShow() {
            this.show = 0;
        },
        // 查询
        query() {
            if(this.searchForm.companyId == null){
                this.$refs.companyId.reset();
            }
            for (let k in this.searchForm) {
                if (this.searchForm[k] === '' || this.searchForm[k] === null || this.searchForm[k] === undefined) {
                    delete this.searchForm[k];
                }
            }
            this.page.pageNum = 1;
            this.queryListData();
        },
        // 重置
        reset() {
            this.searchForm = {};
            this.page.pageNum = 1;
            this.$refs.companyId.reset();
            this.queryListData();
        },
        // 驳回弹框
        rejectShow(id) {
            this.$refs.regectDialog.open(id);
        },
        // 审批
        approve(data) {
            examine(data)
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        companyEmpty() {
            console.log('companyEmpty');
            this.searchForm.companyId = null;
            this.companyName = '';
        },
        companyChange(data) {
            console.log(data);
            if (data.id) {
                this.searchForm.companyId = data.id;
            } else {
                this.searchForm.companyId = null;
                this.companyName = '';
            }
        },
    },
    created() {
        this.$nextTick(() => {
            this.queryListData();
        });
    },
    filters: {
        educationStatus: eduLevel => {
            let str = '';
            for (let i = 0; i < educationStatus.length; i++) {
                if (educationStatus[i].id === eduLevel) {
                    str = educationStatus[i].name;
                    break;
                }
            }
            return str;
        },
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
