<template>
    <div class="flex-container">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <!-- 搜索区域 -->
            <a-form layout="inline">
                <a-row :gutter="24">
                    <a-col :md="6" :sm="12">
                        <a-form-item  label="正则名称">
                            <a-input  placeholder="正则名称"  v-model="queryParam.regularName" ></a-input>
                        </a-form-item>
                    </a-col>
                    <span class="table-page-search-submitButtons" style="float: left; overflow: hidden;">
                        <a-col :md="6" :sm="24">
                             <a-button @click="search()" type="primary">查询</a-button>
                             <a-divider type="vertical"/>
                            <a-button @click="modifyfunction()" type="primary">添加</a-button>
                        </a-col>
                    </span>
                </a-row>
            </a-form>
        </div>

        <!--列表-->
        <s-table :columns="columns" :data="rowdata" bordered ref="table" size="small" class="flex-filling"  :locale="emptyText">
            <!--拦截器-->
            <template slot="productLastUpdateTime" slot-scope="text">
                {{ text | dayjs}}
            </template>
            <!--拦截器-->
            <template slot="productCreateTime" slot-scope="text">
                {{ text | dayjs}}
            </template>
            <template slot="productState" slot-scope="text">
                <p v-if="text=='0'">正常</p>
                <p v-else=""></p>
            </template>
            <template slot="operation" slot-scope="text,record">
                <span>
                  <a @click="modifyfunction(record)">编辑</a>
                  <a-divider type="vertical"/>
                  <a-popconfirm title="确定不显示吗" v-if="record.isShow == 1" @confirm="remove(record.regularId,0)">
                    <a>隐藏</a>
                  </a-popconfirm>
                  <a-popconfirm title="确定显示吗" v-if="record.isShow == 0" @confirm="remove(record.regularId,1)">
                    <a>显示</a>
                  </a-popconfirm>
                </span>
            </template>
        </s-table>
        <!----弹框-->
        <a-modal :title="add ?'添加正则配置': '编辑正则配置'" :visible="modifyvisible"  @ok="createfunctionOk" @cancel="createfunctionCancel" html-type="submit" width="1024px">
            <a-form :form="Formtable">
                <a-form-item v-show="false" :labelCol="{span: 5}" :wrapperCol="{span: 9, offset: 1}" label="">
                    <a-input placeholder="regularId" v-decorator="[ 'regularId', {rules: [{ required: true, message: 'regularId' ,validator: 'click'}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 9, offset: 1}" label="正则名称：">
                    <a-input placeholder="正则名称" v-decorator="[ 'regularName', {rules: [{ required: true, message: '学校名称不能为空！' ,validator: 'click'}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 9, offset: 1}" label="正则表达示：">
                    <a-input placeholder="正则表达示" v-decorator="[ 'regular', {rules: [{ required: true, message: '表达示不能为空！' ,validator: 'click'}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 9, offset: 1}" label="错误提示：">
                    <a-input placeholder="错误提示" v-decorator="[ 'tips', {rules: [{required: true, message: '装机地址不能为空！' ,validator: 'click'}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 9, offset: 1}" label="输入框默认值：">
                    <a-input placeholder="输入框默认值" v-decorator="[ 'placeholder', {rules: [{ required: true, message: '外线方式不能为空！' ,validator: 'click'}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 9, offset: 1}" label="是否显示：">
                    <a-select v-decorator="[ 'isShow' ]" style="width: 330px">
                        <a-select-option key="1">是</a-select-option>
                        <a-select-option key="0">否</a-select-option>
                    </a-select>
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
    import STable from '@/components/table';
    import {regularList,regularDelete,regularInsert,regularUpdate} from "../../../api/school-center/other/verCodesAPI"
    import {cloneObject} from '@/utils/util';
    export default {
        name: "codeManage",
        components: {
            STable,
        },
        data(){
            return {
                emptyText: {emptyText: '暂无数据'},
                Formtable: this.$form.createForm(this),
                modifyvisible: false,
                add: true,
                queryParam: {
                    regularName :'',
                },
                modelData:{
                    regularId: '',
                    regularName:'',
                    regular:'',
                    tips:'',
                    placeholder:'',
                    isShow: '1'
                },
                columns: [
                    {dataIndex: 'regularName', width: 70, title: '正则名称'},
                    {dataIndex: 'regular', width: 100, title: '正则表达示 '},
                    {dataIndex: 'tips', width: 70, title: '错误提示'},
                    {dataIndex: 'placeholder', width: 70, title: '输入框默认提示 '},
                    {
                        dataIndex: 'operation',
                        width: 26,
                        title: '操作',
                        scopedSlots: {customRender: 'operation'},
                        fixed: "right"
                    }]
                ,
                rowdata: parameter => {
                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }
                    let obj = cloneObject(this.queryParam)
                    //表示
                    return regularList(Object.assign(params,obj)).then(res => {
                        let data = {}
                        if (res.state !== 'success') {
                            this.$message.error("查询失败！", 5);
                            this.emptyText.emptyText = '查询失败！'
                            data = {
                                data: [],
                                pageSize: parameter.pageSize,
                                pageNo: 1,
                                totalCount: 0
                            }
                        } else {
                            data = {
                                data: res.data.records,
                                pageSize: parameter.pageSize,
                                pageNo: parameter.pageNo,
                                totalCount: res.data.total
                            }
                        }
                        return data
                    })
                }
            }
        },
        methods:{
            search() {
                this.$refs.table.refresh({search: true})
            },
            remove(regularId,isShow) {
                regularDelete({'regularId':regularId,'isShow':isShow}).then((res) => {
                    if(res.state=="success"){
                        this.$notification.success({message: res.data, description: '', duration: 4})
                    }
                    this.$refs.table.refresh({search: true})
                })
            },
            modifyfunction(data){
                if(data != undefined ){
                    this.modelData.regularId=data.regularId
                    setTimeout(()=>{
                        this.Formtable.setFieldsValue({
                            "regularId":data.regularId,
                            "regularName":data.regularName,
                            "regular":data.regular,
                            "tips":data.tips,
                            "placeholder":data.placeholder,
                            "isShow":data.isShow
                        })
                    },0)
                    this.add=false;
                }
                this.modifyvisible=true;
            },
            createfunctionOk(){
                let that = this;
                that.Formtable.validateFields(['regularName','regular','tips','placeholder','isShow'], {force: true}, (err, values) => {
                    if (err ) {
                        return;
                    }
                    that.modelData.regularName=values.regularName
                    that.modelData.regular=values.regular
                    that.modelData.tips=values.tips
                    that.modelData.placeholder=values.placeholder
                    that.modelData.isShow=values.isShow
                    if(this.add){
                        let obj= that.modelData;
                        delete obj.id
                        let  regular= JSON.stringify(obj);
                        regularInsert({regular}).then((res) => {
                            this.modifyvisible=false;
                            this.clearmodel()
                            if(res.state=="success"){
                                this.$notification.success({message: res.data, description: '', duration: 4})
                            }
                            this.search()
                        })
                    }else {
                        let obj= that.modelData;
                        let  regular= JSON.stringify(obj);
                        regularUpdate({regular}).then((res) => {
                            this.modifyvisible=false;
                            this.clearmodel()
                            if(res.state=="success"){
                                this.$notification.success({message: res.data, description: '', duration: 4})
                            }
                            this.search()
                        })
                    }
                })
            },
            createfunctionCancel(){
                this.modifyvisible=false;
                this.clearmodel()
                this.add=true;
            },
            //清除弹窗信息
            clearmodel() {
                this.add=true
                this.modelData.regularId=''
                this.Formtable.setFieldsValue({
                    "regularName":'',
                    "regular":'',
                    "tips":'',
                    "placeholder":'',
                    "isShow":'',
                })
            }
        }
    }
</script>

<style scoped>

</style>