<template>
  <div>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="40px"
      >
        <!-- <el-form-item label="图片相对路径" prop="imageUrl">
        <el-input
          v-model="queryParams.imageUrl"
          placeholder="请输入图片相对路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <!-- <el-form-item label="图片服务器地址" prop="linkUrl">
        <el-input
          v-model="queryParams.linkUrl"
          placeholder="请输入图片服务器地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <el-form-item>
          <el-button type="primary" size="mini" @click="handleQuery"
            ><el-icon><Search /></el-icon>搜索</el-button
          >
          <el-button size="mini" @click="resetQuery"
            ><el-icon><RefreshRight /></el-icon>重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:carousel:add']"
            ><el-icon><Plus /></el-icon>新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:carousel:edit']"
            ><el-icon><Edit /></el-icon>修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:carousel:remove']"
            ><el-icon><DeleteFilled /></el-icon>删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:carousel:export']"
            ><el-icon><UploadFilled /></el-icon>导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="carouselList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" />
        <el-table-column
          label="序号"
          type="index"
          align="center"
          prop=""
          width="55"
        />

        <el-table-column label="标题" align="center" prop="title" />
        <el-table-column label="编码" align="center" prop="code" />

        <el-table-column label="图片相对路径" align="center" prop="imageUrl" />
        <el-table-column label="图片服务器地址" align="center" prop="linkUrl" />
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="类型" align="center" prop="type">
          <template #default="{ row }">
            <el-tag>
              {{ row.type === 0 ? "外部" : "内部" }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:carousel:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:carousel:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="margin-top: 20px; float: right"
        v-show="total > 0"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        :page="queryParams.pageNum"
        :limit="queryParams.pageSize"
        @size-change="handlePagination"
        @current-change="handleCurrentChange"
      />

      <!-- 添加或修改轮播图数据对话框 -->
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="图片" prop="imageUrl">
            <!-- <el-input v-model="form.imageUrl" placeholder="请输入图片相对路径" /> -->
            <el-upload
              ref="uploadRef"
              class="upload-demo"
              action="/common/upload"
              :auto-upload="false"
              :limit="1"
              @change="handleFileChange"
            >
              <template #trigger>
                <el-button type="primary"
                  ><el-icon><Upload /></el-icon> 上传图片</el-button
                >
                <div style="color: red; font-size: 14px; margin-left: 20px">
                  (只能上传一张图片)
                </div>
              </template>
            </el-upload>
          </el-form-item>

          <el-form-item label="链接" prop="linkUrl">
            <el-input
              v-model="form.linkUrl"
              placeholder="请输入图片服务器地址"
            />
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" />
          </el-form-item>
          <el-form-item
            label="编码"
            prop="code
        "
          >
            <el-input v-model="form.code" placeholder="请输入编码" />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <!-- <el-input v-model="form.title" placeholder="请输入标题" /> -->
            <el-radio-group v-model="radio1" class="ml-4">
              <el-radio label="0" size="large">内部链接</el-radio>
              <el-radio label="1" size="large">外部链接</el-radio>
            </el-radio-group>
          </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>
    </div>
  </div>
</template>

<script>
import {
  listCarousel,
  getCarousel,
  delCarousel,
  addCarousel,
  updateCarousel,
} from "@/api/system/carousel";

export default {
  name: "Carousel",
  data() {
    return {
      radio1: "1",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图数据表格数据
      carouselList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imageUrl: null,
        linkUrl: null,
        title: null,
        sort: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      // rules: {
      //   type: [
      //     {
      //       required: true,
      //       message: "请选择类型",
      //       trigger: "change",
      //     },
      //   ],
      // },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询轮播图数据列表 */
    getList() {
      this.loading = true;
      listCarousel(this.queryParams).then((response) => {
        this.carouselList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handlePagination(newPage) {
      //console.log("newPage", newPage);
      this.queryParams.pageSize = newPage;
      this.getList();
    },
    handleCurrentChange(val) {
      //console.log(val);
      this.queryParams.pageNum = val;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        imageUrl: null,
        linkUrl: null,
        title: null,
        sort: null,
        type: null,
        createTime: null,
        createBy: 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;
      getCarousel(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改轮播图数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCarousel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarousel(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 delCarousel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "oa/carousel/export",
        {
          ...this.queryParams,
        },
        `carousel_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
