<template>
  <div>
    <i-page-header class="product_tabs" title="图片空间">
    </i-page-header>
    <Card :bordered="false" dis-hover class="ivu-mt">

      <Form ref="formValidate" :model="formValidate" :label-width="50">
        <Row type="flex"  :gutter="24">
			<Col v-bind="grid">
			  <FormItem label="图片类型："  label-for="type" label-width="100">
				  <Select v-model="formValidate.type">
					  <Option value="">全部</Option>
				  		<Option value="img">通用</Option>
				        <Option value="icon">图标</Option>
				  </Select>
			  </FormItem>
			</Col>
          <Col v-bind="grid">
            <FormItem label="搜索："  label-for="status2">
              <Input search enter-button placeholder="请输入图片备注" v-model="formValidate.title" @on-search="userSearchs"/>
            </FormItem>
          </Col>
        </Row>
        <Row type="flex">
          <Col v-bind="grid">
            <Button type="primary" @click="tapAdd"  icon="md-add">上传图片</Button>
          </Col>
        </Row>
      </Form>
      <Table :columns="columns1" :data="tableData" class="mt25" no-userFrom-text="暂无数据" no-filtered-userFrom-text="暂无筛选结果"  :loading="loading" highlight-row>
        <template slot-scope="{ row, index }" slot="url">
          <img v-lazy="row.url" :src="row.url" v-image-preview style="width: 120px;cursor: pointer;"/>
        </template>
        <template slot-scope="{ row, index }" slot="action">
          <a @click="tapEdit(row)">编辑</a>
          <Divider type="vertical"/>
          <a @click="tapDel(row)">删除</a>
        </template>
      </Table>
      <div class="acea-row row-right page">
        <Page :total="page.count" show-elevator show-total @on-change="tapPage" :page-size="page.pageSize"/>
      </div>
    </Card>
    <Modal v-model="dialogVisible" :title="title" @on-cancel="editClose">

      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="图片备注" prop="title">
          <el-input placeholder="请输入图片备注" autocomplete="off" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图片分类：" prop="type">
          <Select v-model="form.type">
			  
			  <Option value="img">通用</Option>
              <Option value="icon">图标</Option>
          </Select>
        </el-form-item>
        <el-form-item label="图片" prop="url" :rules="[{ required: true, message: '请上传图片'}]">
          <ui-upload :upUrl="upUrl" :img ="form.url" fileName="图片" :headers="upHeaders" :updata="{isz:0,code:'img'}" @onUpload="onUpload"></ui-upload>
        </el-form-item>


       <!-- <el-form-item label="是否显示" style="margin-bottom: 0;">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">隐藏</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="subAdmin">确 定</el-button>
        </span>
    </Modal>

  </div>
</template>

<script>
    import IPageHeader from "@/layouts/system/page-header/index";
    import { mapState, mapActions } from 'vuex';
    import UiUpload from "@/ui/upload/index";
    import Setting from "@/setting";
    import util from "@/libs/util";
    import {SystemUpsourceList,SystemUpsourceSave,SystemUpsourceDel} from "../../../api/system/adver";
    export default {
        name: "upsource",
        components: { UiUpload, IPageHeader},
        computed: {
        },
        data() {
            return {
                title:"图片管理",
                dialogVisible:false,
                upHeaders:[],
                upUrl:"",
                grid: {
                    xl: 7,
                    lg: 7,
                    md: 12,
                    sm: 24,
                    xs: 24
                },
                artType     : "all",
                headeAr     : [],
                headrIndex  : 0,
                headrIndex2 : 0,
                adverPage   : [],
                adverPageOne : {},
                columns1: [
                    {
                        title: '备注',
                        key: 'title',
                        maxWidth: 120
                    },
                    {
                        title: '图片',
                        key: 'url',
                        slot:"url",
                        width: 140
                    },
                    {
                        title: '类型',
                        key: 'type',
                        width: 80
                    },

                    {
                        title: '链接地址',
                        key: 'url',
                        minWidth: 150
                    },
                    {
                        title: '添加时间',
                        key: 'time',
                        width: 150
                    },
                    // {
                    //     title: '状态',
                    //     key: 'status',
                    //     slot:"status",
                    //     minWidth: 100,
                    //     fixed: 'right',
                    // },
                    {
                        title: '操作',
                        key: 'action',
                        slot: 'action',
                        fixed: 'right',
                        width: 100
                    }
                ],
                loading: true,
                tableData: [],
                page : {
                    count : 0,//一共多少数量
                    pageSize : 0,//每一页码数量
                    page : 1,//当前页码
                },
                formValidate:{title:"",type:""},
                form:{
                    title     : "",
                    url       : "",
                },
            }
        },
        mounted(){
            this.initView();
        },
        methods: {
            initView : function(){
                var upHeaders = {};
                this.upUrl = Setting.apiBaseURL + "/systemv1/upload/index";
                const token = this.$utils.util.cookies.get('system_token');
                if (token) {
                    upHeaders['SYSTEM-ACC-TOKEN'] = token;
                }
                this.upHeaders = upHeaders;
                this.getData();
            },
            /**
             * 获取数据
             */
            getData:function () {
                var data = {
                    title : this.formValidate.title,
					type:this.formValidate.type,
                };
                SystemUpsourceList(data)
                    .then(res=>{
                        this.loading = false;
                        if(res.code == 200) {
                            this.tableData = res.data.list;
                            this.page.pageSize = res.data.pageSize;
                            this.page.count = res.data.pageCount;
                        } else {
                            this.$alert(res.msg);
                        }
                    })
                    .catch(err=>{
                        console.log(err);
                        this.loading = false;
                        this.$utils.loadingClose();
                        this.$alert("网络繁忙，加载失败，请稍等片刻在尝试!", '系统提示');
                    });
            },
            userSearchs () {
                this.page.page = 1;
                this.loading = true;
                this.getData();
            },
			tapAdd:function(sassid){
			    this.dialogVisible = true;
			    this.$refs['form'].resetFields();
			    this.form = this.$utils.resetFields(this.form, {});
			},
			tapEdit:function(row){
				this.form = JSON.parse(JSON.stringify(row));
				this.dialogVisible = true;
				this.title = "编辑";
			},

            //关闭
            editClose:function(){
                this.dialogVisible = false;
            },


            /**
             * 分页
             */
            tapPage:function(index){
                this.page.page = index;
                this.getData();
            },
			
            /**
             * 修改数据
             */
            subAdmin:function(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        let loading = this.$loading("提交数据中...");
                        var data = {};
                        for(var i in this.form) {
                            data[i] = this.form[i];
                        }
                        SystemUpsourceSave(data)
                            .then(res => {
                                loading.close();
                                if (res.code == 200) {
                                    this.dialogVisible = false;
                                    this.$Message.success("操作成功");
                                    this.getData();
                                } else {
                                    this.$alert(res.msg, '系统提示');
                                }
                            })
                            .catch(err => {
                                loading.close();
                                this.$alert("网络繁忙，加载失败，请稍等片刻在尝试!", '系统提示');
                            });
                    }
                });
            },
            /**
             * 上传文件
             * @param res
             */
            onUpload:function (res) {
                if(res.code == -1) {
                    Notice.error({ title : "系统提示", content:res.msg});
                } else {
                    this.form.url = res.data.img;
                }
            },
            // 删除
            tapDel (row) {
                this.$confirm('此操作将永久删除该图片, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$utils.loading(this, "删除数据中...");
                    SystemUpsourceDel({id: row.id})
                        .then(res => {
                            this.$utils.loadingClose();
                            this.loading = true;
                            if (res.code == 200) {
                                this.getData();
                            } else {
                                this.$alert(res.msg);
                            }
                        })
                        .catch(err => {
                            this.$utils.loadingClose();
                            this.$alert("网络繁忙，加载失败，请稍等片刻在尝试!", '系统提示');
                        });
                });
            },
        }
    }
</script>
<style scoped>
  .platform-img{width: 20px;height: 20px;background: #f2f2f2; margin-right: 4px;}
  .platform-img img{ width: 100%;}
</style>
