<template>
    <div class="user">
        <Card :bordered="false" dis-hover>
            <bingo-list
                :columns="columns"
                :pageSetting="pageSetting"
                :page="true"
                :showBtnType="'all'"
                :showBtnOutNum="2"
                :showNewBtn="true"
                :toolbarNew="true"
                ref="bingoList"
                :searchList="searchList"
                :dataApi="Api"
                :queryData="queryData"
                :isSearch="true"
                :isSearchAdvance="true"
                :showSwitchBtn="false"
                :processData="processData"
                @on-query-submit="querySubmit"
                @on-add-new="onAddNew"
                @on-edit-btn="onEditBtn"
            >
            <!-- 高级搜索 -->
                <template slot="queryForm">
                    <bingo-grid-item>
                        <bingo-form-item label="产品名称" prop="nameCn">
                            <Input
                                v-model="queryData.nameCn"
                                placeholder="请输入产品名称"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="地址" prop="url">
                            <Input
                                v-model="queryData.url"
                                placeholder="请输入地址"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <!-- <bingo-grid-item>
                        <bingo-form-item label="图标" prop="icon">
                            <Input
                                v-model="queryData.icon"
                                placeholder="请输入图标"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="背景图片" prop="background">
                            <Input
                                v-model="queryData.background"
                                placeholder="请输入背景图片"
                            />
                        </bingo-form-item>
                    </bingo-grid-item> -->
                    <bingo-grid-item>
                        <bingo-form-item label="说明" prop="instructions">
                            <Input
                                v-model="queryData.instructions"
                                placeholder="请输入说明"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="邮件地址" prop="emailAddress">
                            <Input
                                v-model="queryData.nameCn"
                                placeholder="请输入邮件地址"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="邮件名" prop="emailName">
                            <Input
                                v-model="queryData.nameCn"
                                placeholder="请输入邮件名"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="备注信息" prop="remark">
                            <Input
                                v-model="queryData.remark"
                                placeholder="请输入备注信息"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                </template>
            </bingo-list>
        </Card>
        <Modal v-model="isShowNewForm" :title="isAdd==true?'新建':'修改'" @on-ok="onOk" :width="900">
            <Form ref="formAdd" :model="formAdd" label-position="right" :label-width="90">
                <Row>
                    <Col :span="12">
                        <bingo-form-item label="产品名称">
                            <Input type="text" v-model="formAdd.nameCn" placeholder="请输入产品名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="英文名称">
                            <Input type="text" v-model="formAdd.nameEn" placeholder="请输入英文名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="德文名称">
                            <Input type="text" v-model="formAdd.nameDe" placeholder="请输入德文名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="地址">
                            <Input type="text" v-model="formAdd.url" placeholder="请输入地址">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="图标">
                            <Poptip
                                placement="bottom-start"
                                width="460"
                                trigger="click"
                                @on-popper-show="popperShow"
                                transfer
                            >
                            <Input v-model="formAdd.icon" placeholder="点击选择图标" type="text" readonly>
                                <i v-if="formAdd.icon" slot="prefix" class="icon iconfont" :class="'icon'+formAdd.icon"></i>
                                <Icon v-else slot="prefix" type="ios-search"></Icon>
                            </Input>
                                <div class="api" slot="content">
                                    <IconSelect ref="iconSelect" @selected="selected" />
                                </div>
                            </Poptip>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="说明">
                            <Input type="text" v-model="formAdd.instructions" placeholder="请输入说明">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="邮件地址">
                            <Input type="text" v-model="formAdd.emailAddress" placeholder="请输入邮件地址">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="邮件名">
                            <Input type="text" v-model="formAdd.emailName" placeholder="请输入邮件名">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="备注信息">
                            <Input type="text" v-model="formAdd.remark" placeholder="请输入备注信息">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="背景图片">
                            <Upload
                            :action="uploadUrl"
                            :show-upload-list="false"
                            :on-success="handleSuccess"
                            :format="['jpg','jpeg','png']"
                            :max-size="2048"
                            :on-format-error="handleFormatError"
                            :on-exceeded-size="handleMaxSize"
                            :headers="fileHeaders"
                            class="avatar-uploader"
                            >
                            <img v-if="formAdd.background" :src="formAdd.background" class="avatar">
                            <i v-else class="icon iconiconjia iconfont"></i>
                            </Upload>
                        </bingo-form-item>
                    </Col>
                </Row>
            </Form>
        </Modal>
    </div>
</template>
<script>
    import bingoList from '@/components/bingo-list';
    import Api from '@/api/platform/product';
    import fileApi from '@/api/platform/file';
    import module from '@/mixins/module';
    import IconSelect from '@/components/IconSelect'
    import util from '@/utils/tool'
    import setting from '@/setting'
    export default {
        name:'plat-product',
        data () {
            return {
                columns: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        title: '产品名称',
                        key: 'nameCn',
                        minWidth: 130
                    },
                    {
                        title: '地址',
                        key: 'url',
                        minWidth: 130
                    },
                    {
                        title: '图标',
                        key: 'icon',
                        minWidth: 100
                    },
                    // {
                    //     title: '背景图片',
                    //     key: 'background',
                    //     width: 100
                    // },
                    {
                        title: '说明',
                        key: 'instructions',
                        minWidth: 100
                    },
                    {
                        title: '邮件地址',
                        key: 'emailAddress',
                        minWidth: 170
                    },
                    {
                        title: '邮件名',
                        key: 'emailName',
                        minWidth: 100
                    },
                    {
                        title: '备注信息',
                        key: 'remark',
                        minWidth: 100
                    },
                    {
                        title: '创建人',
                        key: 'creator',
                        minWidth: 110
                    },
                    {
                        title: '创建时间',
                        key: 'createTime',
                        minWidth: 120
                    },
                    {
                        title: '修改人',
                        key: 'modifier',
                        minWidth: 110
                    },
                    {
                        title: '修改时间',
                        key: 'modifyTime',
                        minWidth: 120
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        align: 'center',
                        width: 146
                    }
                ],
                Api: Api,
                pageSetting: {
                    pageNum: 1,
                    pageSize: 20,
                    showSizer: true
                },
                // 搜索的字段
                searchList: [
                    {
                        label: '产品名称',
                        value: 'nameCn',
                        type: 'input'
                    },
                    {
                        label: '地址',
                        value: 'url',
                        type: 'input'
                    },
                    // {
                    //     label: '图标',
                    //     value: 'icon',
                    //     type: 'input'
                    // },
                    // {
                    //     label: '背景图片',
                    //     value: 'background',
                    //     type: 'input'
                    // },
                    {
                        label: '说明',
                        value: 'instructions',
                        type: 'input'
                    },
                    {
                        label: '邮件地址',
                        value: 'emailAddress',
                        type: 'input'
                    },
                    {
                        label: '邮件名',
                        value: 'emailName',
                        type: 'input'
                    },
                    {
                        label: '备注信息',
                        value: 'remark',
                        type: 'input'
                    }
                ],
                tableData: [],
                queryData: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    background: '',
                    emailAddress: '',
                    emailName: '',
                    icon: '',
                    id: '',
                    instructions: '',
                    remark: '',
                    sn: '',
                    url: ''
                },
                queryObj: {},
                isShowNewForm: false,
                formAdd: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    background: '',
                    emailAddress: '',
                    emailName: '',
                    icon: '',
                    id: '',
                    instructions: '',
                    remark: '',
                    sn: '',
                    url: ''
                },
                grid: {
                    xl: 12,
                    lg: 13,
                    md: 13,
                    sm: 24,
                    xs: 24
                },
                isAdd: true,
                uploadUrl: setting.apiBaseURL + fileApi.addTableUrl,
                fileHeaders: {}
            };
        },
        created () {
            this.fileHeaders = this.getHeaders();
        },
        mixins: [module],
        methods: {
            // 上传成功
            handleSuccess (res, file) {
                this.formAdd.iconUrl = res.data;
            },
            // 上传格式错误警告
            handleFormatError (file) {
                this.$Message.error('文件格式不正确，请选择jpg或png');
            },
            // 上传尺寸错误警告
            handleMaxSize (file) {
                this.$Message.error('文件太大，不能超过2M');
            },
            popperShow () {
                this.$refs.iconSelect.reset();
            },
            // 选择图标
            selected (name) {
                this.formAdd.icon = name
            },
            // 弹出层查询
            querySubmit (obj) {
                if (obj) {
                    this.queryObj = obj;
                }
                this.$refs.bingoList.getTableData(this.queryObj);
            },
            // 新建
            async onAddNew () {
                this.isAdd = true;
                for (let i in this.formAdd) {
                    if (Array.isArray(this.formAdd[i])) {
                        this.formAdd[i] = [];
                    } else {
                        this.formAdd[i] = '';
                    }
                }
                this.formAdd.sn = await this.getSn();
                this.isShowNewForm = true;
            },
            // 新建确定
            onOk () {
                if (this.isAdd) {
                    Api.addTable(this.formAdd).then(res => {
                        this.$Message.success('新建成功');
                        this.$refs.bingoList.getTableData();
                    })
                } else {
                    Api.editTable(this.formAdd).then(res => {
                        this.$Message.success('修改成功');
                        this.$refs.bingoList.getTableData();
                    })
                }
            },
            // 编辑
            onEditBtn (id) {
                this.isAdd = false;
                Api.getTableById(id).then(res => {
                    this.formAdd = res;
                    this.isShowNewForm = true;
                })
            },
            // 处理数据
            processData (data) {
                data.forEach(item => {
                    if (item.createTime) {
                        item.createTime = util.format(new Date(item.createTime), 'yyyy-MM-dd');
                    }
                    if (item.modifyTime) {
                        item.modifyTime = util.format(new Date(item.modifyTime), 'yyyy-MM-dd')
                    }
                });
            }
        },
        components: { bingoList, IconSelect }
    };
</script>
