<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="bannerTitle">
        <el-input
          v-model="queryParams.bannerTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="轮播类型" prop="type">
         <el-select v-model="queryParams.type" placeholder="请选择轮播类型">
            <el-option
              v-for="dict in dict.type.tb_index_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="显示状态" prop="bannerStatus">
        <el-select v-model="queryParams.bannerStatus" placeholder="请选择轮播类型">
            <el-option
              v-for="dict in bannerStatusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

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

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange" @sort-change="changeTableSort">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="轮播图" align="center" prop="bannerId" /> -->
      <el-table-column label="标题" align="center" prop="bannerTitle" />
      <el-table-column label="轮播图片" align="center" prop="bannerImg" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.bannerImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="轮播类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_index_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="轮播排序" align="center" prop="bannerSort" />
      <el-table-column label="是否跳转" align="center" prop="jumpType">
         <template slot-scope="scope">
          {{ selectDictLabel(bannerJumpTypeOptions, scope.row.jumpType) }}
         </template>
      </el-table-column>
      <el-table-column label="跳转链接" align="center" prop="bannerUrl" :show-overflow-tooltip="true"/>
      <el-table-column label="显示状态" align="center" prop="bannerStatus">
        <template slot-scope="scope">
         {{ selectDictLabel(bannerStatusOptions, scope.row.bannerStatus) }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"  show-overflow-tooltip/>
      <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="['hezhi:banner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hezhi:banner: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="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="bannerTitle">
          <el-input v-model="form.bannerTitle" placeholder="请输入标题" />
        </el-form-item>
         <el-form-item label="轮播类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择轮播类型">
            <el-option
              v-for="dict in dict.type.tb_index_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="轮播图片" prop="bannerImg">
          <image-upload v-model="form.bannerImg" :limit="1" :width="2840" :height="1060"/>
        </el-form-item>
        <el-form-item label="是否跳转" prop="jumpType">
          <el-select v-model="form.jumpType" placeholder="请选择是否跳转">
            <el-option
              v-for="dict in bannerJumpTypeOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转链接" prop="bannerUrl" v-if="form.jumpType == 1">
          <el-input v-model="form.bannerUrl" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="轮播排序" prop="bannerSort">
          <el-input-number v-model="form.bannerSort" :step="1" :min="0" step-strictly />
        </el-form-item>
        <el-form-item label="状态" prop="bannerStatus">
          <el-radio-group v-model="form.bannerStatus">
            <el-radio
              v-for="dict in bannerStatusOptions"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</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>
</template>

<script>
import {addBanner, delBanner, getBanner, listBanner, updateBanner} from "@/api/hezhi/banner";
// import {bannerJumpTypeOptions, bannerStatusOptions, bannerTypeOptions} from "@/utils/optionStatusUtils";

export default {
  name: "Banner",
  dicts: ['tb_index_type'],
  data() {
    return {
      bannerStatusOptions:[
        {
          label: '显示',
          value: '0'
        },
        {
          label: '隐藏',
          value: '1'
        }
      ],
      bannerTypeOptions:[
        {
          label: '首页',
          value: '0'
        }
      ],
      bannerJumpTypeOptions:[
        {
          label: '不跳转',
          value: '0'
        },
        {
          label: '跳转',
          value: '1'
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      bannerTitles: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图表格数据
      bannerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bannerTitle: null,
        type: null,
        bannerStatus: null,
        createTime: null,
        orderType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bannerTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "轮播类型不能为空", trigger: "change" }
        ],
        bannerImg: [
          { required: true, message: "轮播图片不能为空", trigger: "blur" }
        ],
        jumpType: [
          { required: true, message: "跳转类型不能为空", trigger: "change" }
        ],
        bannerUrl: [
          { required: true, message: "跳转链接不能为空", trigger: "blur" }
        ],
        bannerSort: [
          { required: true, message: "轮播排序不能为空", trigger: "blur" }
        ],
        bannerStatus: [
          { required: true, message: "轮播状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 监听table sort change */
    changeTableSort(column){
          //获取字段名称和排序类型
         //this.queryParams.orderField = column.prop;
          var sortingType = column.order;
          //按照降序排序
          if( sortingType == "descending"){
              this.queryParams.orderType = 1;
          }
          //按照升序排序
          else if( sortingType == "ascending"){
              this.queryParams.orderType = 0;
          }

          //console.log(column,this.queryParams.orderField,this.queryParams.orderType);
          // console.log(this.tableData);
          this.handleQuery();
    },
    /** 查询轮播图列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if(this.queryParams.orderType == null){
        this.queryParams.orderType = 1;
      }
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bannerId: null,
        bannerTitle: null,
        type: null,
        bannerImg: null,
        jumpType: null,
        bannerUrl: null,
        bannerSort: null,
        bannerStatus: '0',
        createTime: null,
        updateTime: null,
        deleteTime: null,
        createBy: null,
        updateBy: null,
        deleteBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bannerId)
      this.bannerTitles = selection.map(item => item.bannerTitle)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.type = '0';
      this.title = "添加轮播图";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bannerId = row.bannerId || this.ids
      getBanner(bannerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改轮播图";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          // this.form.jumpType = '0';
          if (this.form.bannerId != null) {
            updateBanner(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBanner(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bannerIds = row.bannerId || this.ids;
      const bannerTitles = row.bannerTitle || this.bannerTitles;
      this.$modal.confirm('是否确认删除轮播图标题为"' + bannerTitles + '"的数据项？').then(function() {
        return delBanner(bannerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids = this.ids;
      this.download('hezhi/banner/export', {
        ...this.queryParams
      }, `广告位管理_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
