<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.userName"></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="operation" slot-scope="text, record">
                <span>
                  <a @click="modifyfunction(record)">修改</a>
                  <a-divider type="vertical"/>
                  <a-popconfirm v-if="record.status == 1" title="是否不通过佣金系统？" @confirm="notThrough(record.id,0)">
                    <a>不通过佣金系统</a>
                  </a-popconfirm>
                    <a-popconfirm v-if="record.status == 0" title="是否通过佣金系统？" @confirm="notThrough(record.id,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 :labelCol="{span: 5}" :wrapperCol="{span: 9, offset: 1}" label="账号：">
                    <a-input placeholder="预实名账号" v-decorator="[ 'userName', {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="[ 'sign', {rules: [{ required: true, message: '签名不能为空！' ,validator: 'click'}]} ]"></a-input>
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
    import STable from '@/components/table';
    import {ysmUserList, ysmUserNotThrough, ysmUserInsert, ysmUserUpdate} from "@/api/school-center/ysmUser"
    import {cloneObject} from '@/utils/util';
    export default {
        name: "ysmUser",
        components: {
            STable,
        },
        data(){
            return {
                emptyText: {emptyText: '暂无数据'},
                Formtable: this.$form.createForm(this),
                modifyvisible: false,
                add: true,
                queryParam: {
                    userName  :''
                },
                modelData:{
                    id:'',
                    userName:'',
                    sign:''
                },
                columns: [
                    {dataIndex: 'userName', width: 200, title: '预实名账号'},
                    {dataIndex: 'sign', width: 200, title: '签名'},
                    {
                        dataIndex: 'operation',
                        width: 100,
                        title: '操作',
                        scopedSlots: {customRender: 'operation'},
                        fixed: "right"
                    }]
                ,
                rowdata: parameter => {
                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }
                    let obj = cloneObject(this.queryParam)
                    //表示
                    return ysmUserList(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})
            },
            notThrough(id,status) {
                ysmUserNotThrough(id,{status}).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.id=data.id
                    setTimeout(()=>{
                        this.Formtable.setFieldsValue({
                            "userName":data.userName,
                            "sign":data.sign
                        })
                    },0)
                    this.add=false;
                }
                this.modifyvisible=true;
            },
            createfunctionOk(){
                let that = this;
                that.Formtable.validateFields(['userName','sign'], {force: true}, (err, values) => {
                    if (err ) {
                        return;
                    }
                        that.modelData.userName=values.userName
                        that.modelData.sign=values.sign
                    if(this.add){
                        let obj= that.modelData;
                       delete obj.id
                        let  ysmUser= JSON.stringify(obj);
                        ysmUserInsert({ysmUser}).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  ysmUser= JSON.stringify(obj);
                        ysmUserUpdate({ysmUser}).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
                let that = this;
                that.modelData.id=''
                this.Formtable.setFieldsValue({
                    userName:'',
                    sign:''
                })
            }
        }
    }
</script>

<style scoped>

</style>