<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="SYSTEMList" @selection-change="handleSelectionChange">
      <el-table-column label="系统名称" align="center" prop="systemName">
        <template slot-scope="scope">
          {{ scope.row.systemName }}
        </template>
      </el-table-column>
      <el-table-column label="系统背景图" align="center" prop="systemPicture">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="baseUrl + scope.row.systemPicture" :preview-src-list="[baseUrl + scope.row.systemPicture]"></el-image>
        </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:SYSTEM:edit']">修改</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="系统名称">
          <el-input v-model="form.systemName" placeholder="请输入系统名称" maxlength="13"></el-input>
        </el-form-item>
        <el-form-item label="系统背景图">
          <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" />
              <!-- 放大预览 -->
              <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>
                <!-- 删除图片 -->
                <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>
      <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.systemPicture" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { listSYSTEM, getSYSTEM, delSYSTEM, addSYSTEM, updateSYSTEM, upload } from '@/api/portal/sys/sys.js';

export default {
  name: 'SYSTEM',
  data() {
    return {
      dialogVisible: false,
      fileList: [],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      disabled: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统管理表格数据
      SYSTEMList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deptId: null,
        systemName: null,
        systemPicture: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: '主键id不能为空', trigger: 'blur' }],
        delFlag: [{ required: true, message: '删除标记不能为空', trigger: 'blur' }],
        deptId: [{ required: true, message: '部门id不能为空', trigger: 'blur' }],
        systemName: [{ required: true, message: '系统名称不能为空', trigger: 'blur' }],
        systemPicture: [{ required: true, message: '系统背景图不能为空', trigger: 'blur' }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    httprequest() {},
    beforeupload(file) {
      let formData = new FormData();
      formData.append('file', file);
      upload(formData).then(res => {
        this.form.systemPicture = res.fileName;
      });
    },
    handleRemove(file) {
      this.form.systemPicture = '';
      this.fileList = [];
    },
    /** 查询系统管理列表 */
    getList() {
      this.loading = true;
      listSYSTEM(this.queryParams).then(response => {
        this.SYSTEMList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.fileList = [];
      this.form = {
        id: null,
        delFlag: null,
        deptId: null,
        systemName: null,
        systemPicture: null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      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;
      getSYSTEM(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改系统管理';
        this.fileList.push({
          name: 'xxx',
          url: this.baseUrl + response.data.systemPicture
        });
      });
    },
    handlePictureCardPreview(file) {
      this.form.systemPicture = file.fileName;
      this.dialogVisible = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSYSTEM(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addSYSTEM(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除系统管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSYSTEM(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'business/SYSTEM/export',
        {
          ...this.queryParams
        },
        `SYSTEM_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
