<template>
    <div class="flex-container">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <!-- 搜索区域 -->
            <a-form layout="inline">
                <a-row :gutter="24">
                    <a-col :md="4" :sm="12">
                        <a-form-item  label="套餐名称">
                            <a-input  placeholder="套餐名称" v-model="queryParam.name"></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="add()" 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">
            <template slot="operation" slot-scope="text,record">
                <a  @click="modifyfunction(record)">编辑</a>
                <a-divider type="vertical"/>
                <a-popconfirm title="是否删除？" @confirm="delet(record.id)">
                    <a>删除</a>
                </a-popconfirm>
            </template>
        </s-table>


        <!--编辑----弹框-->
        <a-modal :title="titleName" :visible="modifyvisible"  @ok="createfunctionOk" @cancel="createfunctionCancel" html-type="submit" width="840px">
            <a-form :form="Formtable">
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="套餐名称">
                    <a-input placeholder="套餐名称" v-decorator="[ 'packageName', {rules: [{ required: true, message: '套餐名称不能为空！' ,whitespace:true}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="套餐全称">
                    <a-auto-complete placeholder="套餐全称" v-decorator="[ 'name', {rules: [{ required: true, message: '县分不能为空！' ,whitespace:true}]} ]">
                        <template slot="dataSource">
                            <a-select-option v-for="website in autoCompleteResult" :key="website">
                                {{ website }}
                            </a-select-option>
                        </template>
                    </a-auto-complete>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="卡品类型">
                    <a-radio-group  v-decorator="['isXbCard']"  :defaultValue="0">
                        <a-radio :value="0">小白卡</a-radio>
                        <a-radio :value="1">预制卡</a-radio>
                    </a-radio-group>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="月基本费用">
                    <a-input placeholder="月基本费用" v-decorator="[ 'monthFee', {rules: [{ required: true, message: '月基本费用不能为空！' ,whitespace:true}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="流量">
                    <a-input placeholder="流量" v-decorator="[ 'flow', {rules: [{ required: true, message: '流量不能为空！' ,whitespace:true}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="语音">
                    <a-input placeholder="语音" v-decorator="[ 'voice', {rules: [{ required: true, message: '语音不能为空！' ,whitespace:true}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="小白卡套餐id">
                    <a-input placeholder="小白卡套餐id" v-decorator="[ 'xbId', {rules: [{ required: true, message: '小白卡套餐id不能为空！' ,whitespace:true}]} ]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="温馨提示">
                    <a-input placeholder="温馨提示" v-decorator="[ 'warmTip']"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="简介">
                    <a-input placeholder="简介" v-decorator="[ 'explains', {rules: [{ required: true, message: '简介不能为空！' ,whitespace:true}]}]"></a-input>
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="背景图">
                    <img  style="width: 150px">
                </a-form-item>
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="小图">
                    <img  style="width: 150px">
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
    import STable from '@/components/table';
    import {findByList,modify,insert,packageAllList,delet} from "@/api/school-center/packageManagementAPI"
    import {cloneObject} from '@/utils/util';
    export default {
        name: "package",
        components: {
            STable,
        },
        data: function () {
            return {
                Formtable: this.$form.createForm(this, {name: 'Formtable'}),
                //输入框列表
                autoCompleteResult:[],
                modifyForm: {
                    id:"",
                    packageName:"",
                    name:"",
                    isXbCard:"",
                    monthFee:"",
                    flow:"",
                    voice:"",
                    xbId:"",
                    warmTip:"",
                    explains:"",
                },
                queryParam: {
                    name: '',
                },
                modifyvisible:false,
                titleName:"",
                columns: [
                    {dataIndex: 'packageName', width: 200, title: '套餐名称', align: "center"},
                    {dataIndex: 'monthFee', width: 200, title: '标准月租', align: "center"},
                    {dataIndex: 'flow', width: 200, title: '流量', align: "center"},
                    {dataIndex: 'voice', width: 200, title: '语音', align: "center"},
                    {
                        dataIndex: 'operation',
                        width: 100,
                        title: '操作',
                        align: "center",
                        scopedSlots: {customRender: 'operation'},
                        fixed: "right"
                    }]
                ,
                rowdata: parameter => {
                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }
                    //表示
                    let obj = cloneObject(this.queryParam)
                    return findByList(Object.assign(params,obj)).then(res => {
                        let data = {}
                        if (res.state !== 'success') {
                            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})
            },
            delet(id){
                let parent={
                    id:id
                }
                delet(parent).then(res=>{
                    if (res.state=="success") {
                        this.$message.success(res.data ? res.data : '修改成功！', 5);
                        this.search();
                        return null;
                    }
                    this.$message.error(res.data ? res.data : '修改失败！', 5);
                })
            },
            add(){
                this.modifyvisible=true
                this.modifyForm.id=""
                this.titleName="添加"
                packageAllList().then(res=>{
                    if (res.state=="success"){
                        this.autoCompleteResult=res.data
                    }
                })
                setTimeout(()=>{
                    this.Formtable.setFieldsValue({
                        isXbCard:0,
                    })
                },0)
            },
            modifyfunction(data){
                this.modifyForm.id=data.id
                this.titleName="编辑"
                this.modifyvisible=true
                setTimeout(()=>{
                    this.Formtable.setFieldsValue({
                        packageName:data.packageName,
                        name:data.name,
                        isXbCard:data.isXbCard,
                        monthFee:data.monthFee,
                        flow:data.flow,
                        voice:data.voice,
                        xbId:data.xbId,
                        warmTip:data.warmTip,
                        explains:data.explains,
                    })
                },0)
                packageAllList().then(res=>{
                    if (res.state=="success"){
                        this.autoCompleteResult=res.data
                    }
                })
            },
            //提交表单
            createfunctionOk(){
                this.Formtable.validateFields(['packageName', 'name',"isXbCard","monthFee", 'flow',"voice", 'xbId',"warmTip" ,'explains'], {force: true}, (err, values)=>{
                    if (!err){
                        this.modifyForm.packageName=values.packageName
                        this.modifyForm.name=values.name
                        this.modifyForm.isXbCard=values.isXbCard
                        this.modifyForm.monthFee=values.monthFee
                        this.modifyForm.flow=values.flow
                        this.modifyForm.voice=values.voice
                        this.modifyForm.xbId=values.xbId
                        this.modifyForm.warmTip=values.warmTip
                        this.modifyForm.explains=values.explains
                        //插入数据
                        if (this.modifyForm.id==""){
                            insert(this.modifyForm).then(res=>{
                                if (res.state=="success") {
                                    this.$message.success(res.data ? res.data : '添加成功！', 5);
                                    this.search();
                                    return null;
                                }
                                this.$message.error(res.data ? res.data : '添加失败！', 5);
                            })
                            return null;
                        }
                        //修改数据
                        modify(this.modifyForm).then(res=>{
                            if (res.state=="success") {
                                this.$message.success(res.data ? res.data : '修改成功！', 5);
                                this.search();
                                return null;
                            }
                            this.$message.error(res.data ? res.data : '修改失败！', 5);
                        })
                    }

                })
            },
            createfunctionCancel(){
                this.modifyvisible=false
                this.autoCompleteResult=[]
                this.Formtable.setFieldsValue({
                    packageName:"",
                    name:"",
                    isXbCard:0,
                    monthFee:"",
                    flow:"",
                    voice:"",
                    xbId:"",
                    warmTip:"",
                    explains:"",
                })
            }
        }

        }
</script>

<style scoped>

</style>