<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="姓名" min-width="100" sortable>
            </el-table-column>
            <el-table-column prop="phone" label="电话" width="200" sortable>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="rules" ref="editForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="editForm.sex">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="editForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="editForm.address"></el-input>
                </el-form-item>
                <el-form-item label="身份证号" prop="id_card_num">
                    <el-input v-model="editForm.id_card_num" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="导游证号" prop="guide_card_num">
                    <el-input v-model="editForm.guide_card_num" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="部门" prop="party_id">
                    <el-select v-model="editForm.party_id" placeholder="请选择部门">
                        <el-option v-for="item in departments" :label="item.name" :value="item.id" :key="item.id"> {{ item.name }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="职务" prop="duties_id">
                    <el-select v-model="editForm.duties_id" placeholder="请选择职务">
                        <el-option v-for="item in jobs" :label="item.name" :value="item.id" :key="item.id"> {{ item.name }}</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="公司" prop="company_id">
                    <el-select v-model="editForm.company_id" placeholder="请选择公司">
                        <el-option v-for="item in companies" :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="rules" ref="addForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="addForm.sex">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="addForm.address"></el-input>
                </el-form-item>
                <el-form-item label="身份证号" prop="id_card_num">
                    <el-input v-model="addForm.id_card_num" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="导游证号" prop="guide_card_num">
                    <el-input v-model="addForm.guide_card_num" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="部门" prop="party_id">
                    <el-select v-model="addForm.party_id" placeholder="请选择部门">
                        <el-option v-for="item in departments" :label="item.name" :value="item.id" :key="item.id"> {{ item.name }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="职务" prop="duties_id">
                    <el-select v-model="addForm.duties_id" placeholder="请选择职务">
                        <el-option v-for="item in jobs" :label="item.name" :value="item.id" :key="item.id"> {{ item.name }}</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="公司" prop="company_id">
                    <el-select v-model="addForm.company_id" placeholder="请选择公司">
                        <el-option v-for="item in companies" :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import {
            getUserListPage,
            removeUser,
            batchRemoveUser,
            editUser,
            addUser,
            getDepartmentList,
            getJobList,
            getCompanyList
    } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    name: ''
                },
                users: [],
                departments: [],
                jobs: [],
                companies: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: 0,
                    name: '',
                    sex: -1,
                    address: '',
                    phone: '',
                    id_card_num: '',
                    guide_card_num: '',
                    party_id: '',
                    duties_id: '',
                    company_id: '',
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    sex: -1,
                    address: '',
                    phone: '',
                    id_card_num: '',
                    guide_card_num: '',
                    party_id: '',
                    duties_id: '',
                    company_id: '',
                },
                rules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入电话', trigger: 'blur'},
                        {pattern: /^1\d{10}$/, message: '请输入正确的电话', trigger: 'blur'}
                    ],
                    address: [{required: true, message: '请输入地址', trigger: 'blur'}],
                    id_card_num: [
                        {required: true, message: '请输入身份证', trigger: 'blur'},
                        {pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确的身份证', trigger: 'blur'}
                    ],
                    guide_card_num: [
                        {required: true, message: '请输入导游证号', trigger: 'blur'},
                        {pattern: /D-\d{4}-\d{6}/, message: '请输入正确的导游证号', trigger: 'blur'}
                    ],
                    party_id: [
                        {required: true, message: '请选择部门', trigger: 'change'}
                    ],
                    duties_id: [
                        {required: true, message: '请选择职务', trigger: 'change'}
                    ],
                    company_id: [
                        {required: true, message: '请选择公司', trigger: 'change'}
                    ]
                }

            }
        },
        methods: {
            //性别显示转换
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            //获取用户列表
            getUsers() {
                let para = {
                    page: this.page,
                    name: this.filters.name
                };
                this.listLoading = true;
                //NProgress.start();
                getUserListPage(para).then((res) => {
                    this.total = res.data.total;
                    this.users = res.data.users;
                    this.listLoading = false;
                    //NProgress.done();
                });
            },
            getDepartments() {
                getDepartmentList().then((res) => {
                    this.departments = res.data.data;
                });
            },
            getJobs() {
                getJobList().then((res) => {
                    this.jobs = res.data.data;
                });
            },
            getCompanies() {
                getCompanyList().then((res) => {
                    this.companies = res.data.data;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {id: row.id};
                    removeUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    sex: -1,
                    address: '',
                    phone: '',
                    id_card_num: '',
                    guide_card_num: '',
                    party_id: '',
                    duties_id: '',
                    company_id: '',
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            editUser(para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            console.log(para)
                            addUser(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {ids: ids};
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            },
        },
        mounted() {
            this.getUsers();
            this.getDepartments();
            this.getJobs();
            this.getCompanies();
        }
    }

</script>

<style scoped>

</style>