<template>
    <div class='container'>
        <div class='handle-box'>
            <el-form :inline='true'>
                <el-form-item>
                    <el-input
                        v-model='searchForm.name'
                        placeholder='请输入项目名称'
                        clearable>
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button @click='loadData'>搜索</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type='primary' @click='dialogVisible = true' v-if="hasAuth('sys:user:save')">新增
                    </el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table
            ref='inspectionProjectTable'
            :data='tableData'
            tooltip-effect='dark'
            style='width: 100%'
            border
            stripe>

            <el-table-column
                type='index'
                label='序号'
                width='50'>
            </el-table-column>

            <el-table-column
                prop='name'
                label='项目名称'
                :show-overflow-tooltip='true'>
            </el-table-column>

            <el-table-column
                prop='createTime'
                label='创建时间'>
            </el-table-column>

            <el-table-column
                prop='updateTime'
                label='修改时间'>
            </el-table-column>

            <el-table-column
                prop='icon'
                fixed='right'
                width='180px'
                label='操作'>
                <template slot-scope='scope'>
                    <el-button type='text' @click='editHandle(scope.row)'>编辑</el-button>
                    <el-divider direction='vertical'></el-divider>
                    <template>
                        <el-popconfirm title='确定删除这条数据吗？' @confirm='delHandle(scope.row.id)'>
                            <el-button type='text' slot='reference'>删除</el-button>
                        </el-popconfirm>
                    </template>
                </template>
            </el-table-column>

        </el-table>

        <el-pagination
            @size-change='handleSizeChange'
            @current-change='handleCurrentChange'
            layout='total, sizes, prev, pager, next, jumper'
            :page-sizes='[10, 20, 50, 100]'
            :current-page='current'
            :page-size='size'
            :total='total'>
        </el-pagination>


        <!--新增对话框-->
        <el-dialog
            title='提示'
            :visible.sync='dialogVisible'
            width='60%'
            :before-close='handleClose'>

            <el-form :model='editForm' :rules='editFormRules' ref='editForm'>
                <el-row>
                    <el-col :span='12'>
                        <el-form-item label='项目名称:' prop='name' label-width='100px'>
                            <el-input v-model='editForm.name' autocomplete='off' placeholder='请输入项目名称'></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span='12'>

                    </el-col>
                </el-row>
            </el-form>
            <div slot='footer' class='dialog-footer'>
                <el-button @click="resetForm('editForm')">取 消</el-button>
                <el-button type='primary' @click="submitForm('editForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'inspectionProjectManage',
    data() {
        return {
            searchForm: {
                name: ''
            },
            delBtlStatu: true,
            total: 0,
            size: 10,
            current: 1,
            dialogVisible: false,
            editForm: {
                name: ''
            },
            tableData: [],
            editFormRules: {
                name: [
                    { required: true, message: '请输入项目名称', trigger: 'blur' }
                ]
            }
        };
    },
    created() {
        this.loadData();
    },
    methods: {
        loadData() {
            this.$axios.get('/api/inspection/project/list', {
                params: {
                    name: this.searchForm.name,
                    current: this.current,
                    size: this.size
                }
            }).then(res => {
                this.tableData = res.data.data.records;
                this.size = res.data.data.size;
                this.current = res.data.data.current;
                this.total = res.data.data.total;
            });
        },
        handleClose() {
            this.resetForm('editForm');
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
            this.editForm = {};
        },
        submitForm(formName) {
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.$axios.post('/api/inspection/project/' + (_this.editForm.id ? 'updateBean' : 'addBean'), _this.editForm)
                        .then(res => {
                            _this.loadData();
                            _this.$notify({
                                showClose: true,
                                message: '恭喜你，操作成功',
                                type: 'success'
                            });
                            _this.resetForm(formName);
                        });
                } else {
                    return false;
                }
            });
        },
        editHandle(row) {
            this.editForm = {
                id: row.id,
                name: row.name,
            };
            this.dialogVisible = true;
        },
        delHandle(id) {
            this.$axios.post('/api/inspection/project/deleteById?id=' + id).then(res => {
                this.loadData();
                this.$notify({
                    showClose: true,
                    message: '恭喜你，操作成功',
                    type: 'success'
                });
            });
        },
        handleSizeChange(val) {
            this.size = val;
            this.loadData();
        },
        handleCurrentChange(val) {
            this.current = val;
            this.loadData();
        }
    }
};
</script>

<style scoped>

</style>
