<template>
    <div>

        <div class="communal-list">
            <div class="communal-title">
                <div>岗位列表</div>
                <div class="communal-box">
                    <el-input @input="Inquireclick()" placeholder="公司/部门/岗位" v-model="input" clearable>
                    </el-input>
                    <el-button @click="Inquireclick()" type="primary"><i class="iconfont icon-sousuo"></i></el-button>
                    <el-button @click="addDepartment()" type="primary">新增</el-button>
                </div>
            </div>
            <template>
                <el-table :data="tableDatas.slice((currentPage - 1) * pageSize, currentPage * pageSize)" border
                    style="width: 100%">
                    <el-table-column prop="num" label="序 号" width="55">
                        <template slot-scope="scope">
                            {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="jobName" label="岗位名称" width="240">
                    </el-table-column>
                    <el-table-column prop="companyName" label="所属公司" width="200">
                    </el-table-column>
                    <el-table-column prop="depName" label="所属所属部门" width="220">
                    </el-table-column>
                    <el-table-column prop="remark" label="备注" width="300">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="upDate(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="del(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>
            <div class="block">
                <el-pagination align='center' @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="currentPage" :page-sizes="[10, 15, 20]" :page-size="pageSize"
                    layout="total, sizes, prev, pager, next" :total="tableDatas.length">
                </el-pagination>
            </div>
        </div>
        <el-dialog title="添加岗位" :visible.sync="addJobVisible" width="30%" :before-close="handleClose" :close-on-click-modal="false">
            <div>
                <el-form :model="addData" :rules="rules" ref="addData" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="岗位名称" prop="JobName">
                        <el-input placeholder="请输入内容" v-model="addData.JobName">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="所属公司" prop="CompanyId">
                        <el-select v-model="addData.CompanyId" filterable placeholder="请选择所属公司" @change="handleChange">
                            <el-option v-for="item in companyData" :key="item.id" :label="item.companyName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="所属部门" prop="DepId">
                        <el-select v-model="addData.DepId" placeholder="所属部门">
                            <el-option v-for="item in depData" :key="item.id" :label="item.depName" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="备 注" prop="Remark">
                        <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="addData.Remark"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addJobVisible = false">取 消</el-button>
                <el-button type="primary" @click="Addbtn">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="修改部门" :visible.sync="upJobVisible" width="30%" :before-close="handleClose">
            <div>
                <el-form :model="upData" :rules="rules" ref="upData" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="岗位名称" prop="JobName">
                        <el-input placeholder="请输入内容" v-model="upData.JobName">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="所属公司" prop="CompanyId">
                        <el-select v-model="upData.CompanyId" filterable placeholder="请选择所属公司" @change="handleChange">
                            <el-option v-for="item in companyData" :key="item.id" :label="item.companyName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="所属部门" prop="DepId">
                        <el-select v-model="upData.DepId" placeholder="所属部门">
                            <el-option v-for="item in depData" :key="item.id" :label="item.depName" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="备 注" prop="Remark">
                        <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="upData.Remark"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="upJobVisible = false">取 消</el-button>
                <el-button type="primary" @click="upDateBtn">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { co, el } from '@fullcalendar/core/internal-common';
import { del } from 'vue';
export default {
    data() {

        return {

            tableDatas: [],
            tableData: [],
            companyData: [],//公司数据
            depData: [],//部门数据
            currentPage: 1, // 当前页码
            pageSize: 15,// 每页的数据条数
            addJobVisible: false,
            upJobVisible: false,
            addData: {
                CompanyId: '',
                DepId: '',
                JobName: '',
                CreateUserId: 0,
                Remark: ''
            },
            upData: {
                Id: 0,
                CompanyId: '',
                DepId: '',
                JobName: '',
                Remark: ''
            },
            input: '',
            token: '',
            userId: 0,
            rules: {
                JobName: [
                    { required: true, message: '请输入岗位名称', trigger: 'blur' },
                ],
                CompanyId: [
                    { required: true, message: '请选择所属公司', trigger: 'blur' },
                ],
                DepId: [
                    { required: true, message: '请选择所属部门', trigger: 'blur' },
                ],
            },
        }
    },
    methods: {
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val;

        },
        Department(compId) {
            var url = "/api/System/QueryDepartmentList"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    CompanyId: compId
                }
            }).then(function (res) {
                if (res.data.code == 200) {

                    that.depData = res.data.data;
                }
            })
        },
        //公司下拉框绑定
        company() {
            var url = "/api/System/getCompanyList"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                }
            }).then(function (res) {
                console.log(res)

                if (res.data.code == 200) {

                    that.companyData = res.data.data

                }
            })
        },
        jobPost() {
            var url = "/api/System/QueryJobPost"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    CompanyId: 0,
                    DepId: 0,
                }
            }).then(function (res) {

                console.log(res)

                if (res.data.code == 200) {
                    that.tableDatas = res.data.data;
                    that.tableData = that.tableDatas
                    if (that.tableDatas.slice((that.currentPage - 1) * that.pageSize, that.currentPage * that.pageSize).length == 0) {
                        if (that.currentPage > 1) {
                            that.currentPage = that.currentPage - 1;
                        }
                    }
                }
            })
        },
        handleChange(event) {

            var comId = event;
            const that = this;
            that.upData.DepId = ''
            that.Department(event);
        },
        Inquireclick() {
            var newarr = [];
            if (this.input == "") {
                newarr = this.tableData;
            } else {
                for (var i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].depName.indexOf(this.input) != -1) {
                        newarr.push(this.tableData[i]);
                    } else if (this.tableData[i].companyName.indexOf(this.input) != -1) {
                        newarr.push(this.tableData[i]);
                    } else if (this.tableData[i].jobName.indexOf(this.input) != -1) {
                        newarr.push(this.tableData[i]);
                    }
                }
            }
            this.tableDatas = newarr;
            this.currentPage = 1;
        },
        //#region  修改操作
        upDate(index, row) {

            this.upJobVisible = true;
            this.upData.Id = row.id
            this.upData.CompanyId = row.companyId
            this.upData.DepId = row.depId
            this.upData.JobName = row.jobName
            this.upData.Remark = row.remark
        },
        upDateBtn() {

            this.$refs.upData.validate((valid) => {
                if (valid) {
                    var that = this
                    that.upData.CreateUserId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId

                    if (that.upData.JobName == "" || that.upData.JobName == undefined) {
                        that.$message.error("部门名称不能为空");
                        return
                    }
                    if (that.upData.Remark == null || that.upData.Remark == undefined) that.upData.Remark = ''

                    var url = "/api/System/EditJobPost"

                    that.$axios({
                        method: 'post',
                        url: url,
                        headers: {
                            Authorization: 'Bearer ' + that.token
                        },
                        data: that.upData
                    }).then(function (res) {
                        console.log(res)

                        if (res.data.code == 200) {
                            that.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            that.upJobVisible = false;
                            that.jobPost();
                        } else {
                            that.$message.error('修改失败！');
                        }
                    })
                }
            })
        },
        //关闭修改框
        handleClose(done) {
            done();
        },
        //#endregion
        del(index, row) {

            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var url = "/api/System/DelJobPost"
                var that = this
                this.$axios({
                    method: 'post',
                    url: url,
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    },
                    data: {
                        Id: row.id,
                    }
                }).then(function (res) {
                    console.log(res)

                    if (res.data.code == 200) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        that.jobPost();
                    } else {
                        that.$message.error("删除失败，请稍后重试");
                    }
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '操作已取消！'
                });
            });

        },
        //#region 添加
        addDepartment() {
            this.addJobVisible = true

        },
        Addbtn() {

            this.$refs.addData.validate((valid) => {
                if (valid) {
                    var that = this
                    that.addData.CreateUserId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
                    if (that.addData.JobName == "" || that.addData.JobName == undefined) {
                        that.$message.error("岗位名称不能为空");
                        return
                    }
                    if (that.addData.Remark == null) that.addData.Remark = '';
                    var url = "/api/System/AddJobPost"
                    that.$axios({
                        method: 'post',
                        url: url,
                        headers: {
                            Authorization: 'Bearer ' + that.token
                        },
                        data: that.addData
                    }).then(function (res) {
                        console.log(res)

                        if (res.data.code == 200) {
                            that.$message({
                                message: '添加成功！',
                                type: 'success'
                            });
                            that.addJobVisible = false;
                            that.Department();
                            that.addData.CompanyId = '';
                            that.addData.DepId = '';
                            that.addData.JobName = ''
                            that.addData.CreateUserId = that.userId;
                            that.addData.Remark = "";
                            that.jobPost();
                        } else {
                            that.$message.error('添加失败！');
                        }
                    })
                }
            })
        },
        //#endregion

    },
    mounted() {

        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        console.log(this.token)
        this.company();
        this.jobPost();
        this.Department('0');
    }
}
</script>
<style>
.communal-list {
    background-color: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #0005;
    border-radius: 10px;
}

.communal-title {
    display: flex;
    font-size: 17px;
    font-weight: 600;
    color: #555;
    margin-top: 8px;
    margin-bottom: 2px;
    justify-content: space-between;
    align-items: center;
}

.communal-box {
    display: flex;
}

.communal-box>button {
    margin-left: 10px;
    padding: 8px 20px;
}
</style>