<template>
  <div>
    <el-row class="handle-box" style="margin-bottom: 10px">
      <el-col :span="20">
        <el-input v-model="linkName" class="ch-input ch-input-size" placeholder="链接名称" size="small" @keyup.enter.native="handleSearch()" />
        <el-button class="ch-button" size="small" @click="handleSearch()"><i class="el-icon-search" />&nbsp;搜索</el-button>
        <el-button class="ch-button-warning" size="small" @click="handleReset()"><i class="el-icon-search" />&nbsp;重置</el-button>
      </el-col>
      <el-col :span="4">
        <el-button class="ch-button" size="small" style="float: right" @click="handleAdd()"><i class="el-icon-menu" />&nbsp;新增</el-button>
      </el-col>
    </el-row>
    <el-row class="handle-box">
      <el-col :span="24">
        <el-table v-loading="loading" :data="AllData" row-class-name="g_table_row" stripe>
          <el-table-column type="index" width="60" />
          <el-table-column align="center" header-align="center" label="简图" width="200">
            <template scope="scope">
              <img :src="$constant.BASE_URI + '/FileController/viewImg/'+scope.row.linkImage" style="height: 50px;margin-top: 4px">
            </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" label="下载" width="200">
            <template scope="scope">
              <a :href="$constant.BASE_URI + '/FileController/download/'+scope.row.linkImage">{{ scope.row.linkName }}</a>
            </template>
          </el-table-column>
          <el-table-column label="链接名" prop="linkName" />
          <el-table-column label="链接地址" prop="linkUrl" />
          <el-table-column label="排序号" prop="linkOrder" />
          <el-table-column header-align="center" label="操作" width="180">
            <template scope="scope">
              <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="mini" type="info" @click="handleDel(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-page">
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :total="allpage"
            background
            layout="total, prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="dialogTitle" class="statistic_base" :visible.sync="dialogVisible" top="50px" width="75%" @open="dlgOpen" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" style="width: 100%;padding: 5px">
        <el-row>
          <el-col style="padding-bottom: 10px">
            <span class="card_title">基本信息</span>
            <el-card shadow="always" style="padding: 15px 5px 5px 15px">
              <el-row>
                <el-col :span="6">
                  <div class="info-label">
                    <p class="logo-title">链接图</p>
                    <p style="font-size: 12px;font-family: STFangsong;margin-bottom: 5px">Click Image To Change</p>
                  </div>
                  <el-form-item>
                    <el-upload
                      :http-request="uploadLinkImage"
                      :on-success="logoHandleSuccess"
                      :show-file-list="false"
                      action="http://localhost:9002/FileController/upload"
                      class="avatar-uploader"
                      method="post"
                    >
                      <img v-if="form.linkImage" :src="$constant.BASE_URI + '/FileController/viewImg/'+form.linkImage" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon" />
                    </el-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="18">
                  <el-row>
                    <el-col :span="22" class="col-txt">&nbsp;</el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="5" class="col-txt"><span>链接名</span></el-col>
                    <el-col :span="17" class="col-input">
                      <el-form-item>
                        <el-input v-model="form.linkName" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="5" class="col-txt"><span>链接地址</span></el-col>
                    <el-col :span="17" class="col-input">
                      <el-form-item prop="linkUrl">
                        <el-input v-model="form.linkUrl" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="5" class="col-txt"><span>排序号</span></el-col>
                    <el-col :span="17" class="col-input">
                      <el-form-item>
                        <el-input v-model="form.linkOrder" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </el-card>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Base from '../base/base'
import BaseData from '../base/baseData'
import { upload } from '../../static/utils/channel'
import { validateURL } from '../../static/utils/validate'

export default {
    name: 'HomeLink',
    mixins: [Base, BaseData],
    data() {
        const validateURI = (rule, value, callback) => {
            if (validateURL(value)) {
                callback()
            } else {
                callback(new Error('请输入正确的域名或IP地址，需包含‘http://’或‘https://’'))
            }
        }
        return {
            // 列表相关
            linkName: '',
            AllData: [],
            loading: false,
            // 弹框相关
            dialogVisible: false,
            dialogTitle: '新增',
            isAdd: true,
            form: {
                id: '',
                linkImage: '',
                linkUrl: '',
                linkName: '',
                linkOrder: ''
            },
            rules: {
                linkUrl: [{ required: true, trigger: 'blur', validator: validateURI }]
            }
        }
    },
    mounted() {
        this.getData()
    },
    methods: {
        getData: function() {
            const _this = this
            _this.loading = true
            _this.AllData = []

            const postData = this.getPostData()
            this.baseRequest('list', postData).then((res) => {
                if (res.data.rows) {
                    res.data.rows.forEach(function(item) {
                        const json = _this.getItemJson(item)
                        _this.AllData.push(json)
                    })
                    _this.allpage = res.data.total
                }
                _this.loading = false
            }).catch(() => {
            })
        },
        handleSearch: function() {
            this.getData()
        },
        handleReset: function() {
            this.linkName = ''
        },
        getPostData: function() {
            return {
                linkName: this.linkName,
                pageNum: this.currentPage,
                pageSize: this.pageSize
            }
        },
        getItemJson: function(item) {
            const json = {
                linkImage: item.linkImage,
                linkUrl: item.linkUrl,
                linkName: item.linkName,
                linkOrder: item.linkOrder,
                id: item.id
            }
            return json
        },
        dlgOpen: function() {
            const _this = this

            if (_this.form.id !== '') {
                const postData = {
                    id: _this.form.id
                }
                this.baseRequest('getById', postData).then((res) => {
                    if (res.data) {
                        _this.form.linkImage = res.data.linkImage
                        _this.form.linkUrl = res.data.linkUrl
                        _this.form.linkName = res.data.linkName
                        _this.form.linkOrder = res.data.linkOrder
                        _this.form.id = res.data.id
                    }
                }).catch(() => {
                })
            }
        },
        /* 编辑*/
        handleEdit: function(val) {
            this.isAdd = false
            this.form.id = val.id
            this.dialogVisible = true
            this.dialogTitle = '编辑'
        },
        /* 新增*/
        handleAdd: function() {
            this.form = {
                id: '',
                linkImage: '',
                linkUrl: '',
                linkName: '',
                linkOrder: ''
            }
            this.isAdd = true
            this.dialogVisible = true
            this.dialogTitle = '新增'
        },
        confirmSubmit: function() {
            const _this = this

            this.$refs.form.validate(valid => {
                if (valid) {
                    let soaUrl = 'edit'
                    const postData = {
                        linkImage: _this.form.linkImage,
                        linkUrl: _this.form.linkUrl,
                        linkName: _this.form.linkName,
                        linkOrder: _this.form.linkOrder,
                        id: _this.form.id
                    }

                    if (this.isAdd) {
                        soaUrl = 'add'
                    }
                    this.opRecord(postData, soaUrl)
                } else {
                    //console.log('error submit!!')
                    return false
                }
            })
        },
        uploadLinkImage: function(param) {
            upload(param, true, true).then((res) => {
                this.form.linkImage = res
            })
        },
        baseRequest: function(opUrl, postData) {
            return this.$channel.globeRequest('SysHomeLinkController', opUrl, postData, 'project')
        }
    }
}
</script>

<style scoped>
    .ch-input .el-input__inner {
        border-radius: 0px;
        border-color: #32323A;
    }
    .ch-input-size {
        width: 150px;
    }
    .ch-button {
        border-radius: 0px;
        border-color: #32323A;
        background-color: #32323A;
        color: #fff;
    }
    .ch-button-warning {
        margin-left: 10px;
        border-radius: 0px;
        border-color: #E6A23C;
        background-color: #E6A23C;
        color: #fff;
    }
</style>
<style>
    .el-table th, .el-table td {
        padding: 5px 0px;
    }
    .avatar-uploader {
        text-align: center;
    }
    .avatar-uploader .el-upload {
        border: 2px dashed #d9d9d9;
        padding: 2px;
        border-radius: 3px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        width: 128px;
        height: 128px;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        line-height: 128px;
        text-align: center;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #94b131;
    }
    .avatar {
        position: absolute;
        margin: auto;
        top: 0px;
        right: 0px;
        bottom: 0px;
        left: 0px;
        max-width:100%;
        max-height:100%
    }
    .info-label{
        text-align: center;
    }
    .logo-title {
        text-align: center;
        font-weight: bold;
        font-size: 18px;
        padding-bottom: 5px;
    }
</style>
