<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="网址名" prop="websiteName">
        <el-input v-model="queryParams.websiteName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
<!--      &lt;!&ndash;          type="primary"&ndash;&gt;-->
<!--      &lt;!&ndash;          plain&ndash;&gt;-->
<!--      &lt;!&ndash;          icon="el-icon-plus"&ndash;&gt;-->
<!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
<!--      &lt;!&ndash;          @click="handleAdd"&ndash;&gt;-->
<!--      &lt;!&ndash;          v-hasPermi="['business:WEBSITE:add']"&ndash;&gt;-->
<!--      &lt;!&ndash;        >新增</el-button>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
<!--      &lt;!&ndash;          type="primary"&ndash;&gt;-->
<!--      &lt;!&ndash;          plain&ndash;&gt;-->
<!--      &lt;!&ndash;          icon="el-icon-edit"&ndash;&gt;-->
<!--      &lt;!&ndash;          :disabled="single"&ndash;&gt;-->
<!--      &lt;!&ndash;          @click="handleUpdate"&ndash;&gt;-->
<!--      &lt;!&ndash;          v-hasPermi="['business:WEBSITE:edit']"&ndash;&gt;-->
<!--      &lt;!&ndash;        >修改</el-button>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--      <el-col :span="1.5">-->
<!--        <el-button type="primary" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['business:WEBSITE:edit']">修改</el-button>-->
<!--&lt;!&ndash;        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['business:WEBSITE:remove']">删除</el-button>&ndash;&gt;-->
<!--      </el-col>-->
<!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
<!--      &lt;!&ndash;          type="warning"&ndash;&gt;-->
<!--      &lt;!&ndash;          plain&ndash;&gt;-->
<!--      &lt;!&ndash;          icon="el-icon-download"&ndash;&gt;-->
<!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
<!--      &lt;!&ndash;          @click="handleExport"&ndash;&gt;-->
<!--      &lt;!&ndash;          v-hasPermi="['business:WEBSITE:export']"&ndash;&gt;-->
<!--      &lt;!&ndash;        >导出</el-button>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table  :data="WEBSITEList" @selection-change="handleSelectionChange" height="calc(100vh - 300px)">
      <el-table-column label="网址名字" align="center" prop="websiteName" />
      <!-- <el-table-column label="背景图片" align="center" prop="websiteUrl">
        <template slot-scope="scope">
          <img
            :src="scope.row.websiteUrl"
            alt="背景图片"
            style="max-width: 100px; max-height: 100px"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:WEBSITE:edit']">修改</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['business:WEBSITE:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改网站管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="链接名称" prop="websiteName">
              <el-input v-model="form.websiteName" placeholder="请输入链接名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="顶部背景图" prop="topPicture">
          <cropper v-model="form.topPicture" :fixed_number="[4, 2]"></cropper>
        </el-form-item>
        <el-form-item label="底部背景图" prop="bottomPicture">
          <cropper v-model="form.bottomPicture" :fixed_number="[34, 2]"></cropper>
        </el-form-item>
<!--        <el-form-item label="顶部背景图" prop="websiteUrl">-->
<!--          <el-upload action="#" list-type="picture-card" :limit="1" :auto-upload="true" :http-request="httprequest" :before-upload="beforeupload" :file-list="fileList">-->
<!--            <i slot="default" class="el-icon-plus"></i>-->
<!--            <div slot="file" slot-scope="{ file }">-->
<!--              <img class="el-upload-list__item-thumbnail" :src="file.url" />-->

<!--              &lt;!&ndash; 放大预览 &ndash;&gt;-->
<!--              <span class="el-upload-list__item-actions">-->
<!--                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">-->
<!--                  <i class="el-icon-zoom-in"></i>-->
<!--                </span>-->
<!--                &lt;!&ndash; 删除图片 &ndash;&gt;-->
<!--                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">-->
<!--                  <i class="el-icon-delete"></i>-->
<!--                </span>-->
<!--              </span>-->
<!--            </div>-->
<!--          </el-upload>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="底部背景图" prop="websiteUrl">-->
<!--          <el-upload action="#" list-type="picture-card" :limit="1" :auto-upload="true" :http-request="httprequest" :before-upload="beforeupload2" :file-list="fileList2">-->
<!--            <i slot="default" class="el-icon-plus"></i>-->
<!--            <div slot="file" slot-scope="{ file }">-->
<!--              <img class="el-upload-list__item-thumbnail" :src="file.url" />-->

<!--              &lt;!&ndash; 放大预览 &ndash;&gt;-->
<!--              <span class="el-upload-list__item-actions">-->
<!--                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">-->
<!--                  <i class="el-icon-zoom-in"></i>-->
<!--                </span>-->
<!--                &lt;!&ndash; 删除图片 &ndash;&gt;-->
<!--                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove2(file)">-->
<!--                  <i class="el-icon-delete"></i>-->
<!--                </span>-->
<!--              </span>-->
<!--            </div>-->
<!--          </el-upload>-->
<!--        </el-form-item>-->
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" fullscreen append-to-body @close="dialogVisible = false">
      <img width="100%" :src="form.topPicture" alt="" />
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible2" fullscreen append-to-body @close="dialogVisible2 = false">
      <img width="100%" :src="form.bottomPicture" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { listWEBSITE, getWEBSITE, delWEBSITE, updateWEBSITE, upload, downloadpic } from '@/api/portal/WEBSITE/WEBSITE';

export default {
  name: 'WEBSITE',
  data() {
    return {
      fileList: [],
      fileList2: [],
      dialogVisible: false,
      dialogVisible2: false,
      disabled: false,
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网站管理表格数据
      WEBSITEList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        topPicture: null,
        bottomPicture: null,
        websiteName: null,
        id: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        topPicture: [{ required: true, message: '顶部背景图不能为空', trigger: 'blur' }],
        bottomPicture: [{ required: true, message: '底部背景图不能为空', trigger: 'blur' }],
        websiteName: [{ required: true, message: '网站昵称不能为空', trigger: 'blur' }],
        id: [{ required: true, message: '主键id不能为空', trigger: 'blur' }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    httprequest() {},
    //预览顶部背景图
    handlePictureCardPreview(file) {
      this.form.topPicture = file.url;
      this.dialogVisible = true;
    },

    //预览底部背景图
    handlePictureCardPreview2(file) {
      this.form.bottomPicture = file.url;
      this.dialogVisible2 = true;
    },
    //删除顶部背景图
    handleRemove(file) {
      this.form.topPicture = '';
      this.fileList = [];
    },
    //删除底部背景图
    handleRemove2(file) {
      this.form.bottomPicture = '';
      this.fileList2 = [];
    },
    //上传顶部背景图
    beforeupload(file) {
      let formData = new FormData();
      formData.append('file', file);
      upload(formData).then(res => {
        this.form.topPicture = res.fileName;
      });
    },
    //上传底部背景图
    beforeupload2(file) {
      let formData = new FormData();
      formData.append('file', file);
      upload(formData).then(res => {
        this.form.bottomPicture = res.fileName;
      });
    },
    /** 查询网站管理列表 */
    getList() {
      this.loading = true;
      listWEBSITE(this.queryParams).then(response => {
        this.WEBSITEList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.fileList = [];
      this.fileList2 = [];
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        websiteUrl: null,
        websiteName: null,
        id: null
      };
      this.fileList = [];
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.websiteUrl);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加网站管理';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWEBSITE(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改网站管理';
        this.fileList.push({ name: 'xxx', url: this.baseUrl+response.data.topPicture});
        this.fileList2.push({ name: 'xxx', url: this.baseUrl+response.data.bottomPicture});
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateWEBSITE(this.form).then(response => {
            this.$modal.msgSuccess('修改成功');
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const websitePictures = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除网站管理编号为"' + websitePictures + '"的数据项？')
        .then(function () {
          return delWEBSITE(websitePictures);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'business/WEBSITE/export',
        {
          ...this.queryParams
        },
        `WEBSITE_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
