<template>
  <div class="app-container char-page">
    <div class="char-max-page" v-if="pageType == 1">
      <div class="top-button-max-box">
        <div class="top-button-box" @click="buttonPageType(1)">
          <p :class="queryParams.tagCard == 1 ? 'p-color':''">展示中</p>
          <p :class="queryParams.tagCard == 1 ? 'p-back':''"></p>
        </div>
        <div class="top-button-box" @click="buttonPageType(2)">
          <p :class="queryParams.tagCard == 2 ? 'p-color':''">待展示</p>
          <p :class="queryParams.tagCard == 2 ? 'p-back':''"></p>
        </div>
        <div class="top-button-box" @click="buttonPageType(3)">
          <p :class="queryParams.tagCard == 3 ? 'p-color':''">已过期</p>
          <p :class="queryParams.tagCard == 3 ? 'p-back':''"></p>
        </div>
      </div>
      <div class="char-page-min">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="关键字" prop="searchValue">
            <el-input
              v-model="queryParams.searchValue"
              placeholder="标题/创建人"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="展开时间" label-width="90px">
            <el-date-picker
              :clearable="false"
              v-model="dateRange"
              size="small"
              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 style="float: right;">
            <el-col :span="1.5">
              <p class="add-button-one-120"
                 @click="buttonClick(2)"
                 v-hasPermi="['laboratory:chart:add']"
              ><i class="el-icon-plus"></i>新增</p>
            </el-col>
          </el-form-item>
          <el-form-item>
            <p class="inquire-button-one" style="margin:0 20px;" @click="handleQuery">查询</p>
            <p class="reset-button-one" @click="resetQuery">重置</p>
          </el-form-item>
        </el-form>
        <el-table style="margin:20px 0;" v-loading="loading" border :data="chartList" @selection-change="handleSelectionChange">
          <el-table-column label="内容" align="left" prop="imgUrl" width="180">
            <template slot-scope="scope">
              <img style="width:100px;height:74px;display: block;margin:0 auto;" :src="scope.row.imgUrl">
            </template>
          </el-table-column>
          <el-table-column label="标题" align="left" prop="imgName"/>
          <el-table-column label="类型" align="left" prop="imgCategory" width="140px">
            <template slot-scope="scope">{{scope.row.imgCategory == 1?'轮播图':(scope.row.imgCategory == 2?'文化图':'')}}</template>
          </el-table-column>
          <el-table-column label="展示范围" align="left" prop="imgRangeName" width="200"/>
          <el-table-column label="展示时间" align="left" prop="validTimeStr" width="300"/>
          <el-table-column label="创建人" align="left" prop="imgName" width="200">
            <template slot-scope="scope">
              <div style="display: flex">
                <span style="height:23px;width:90px;margin-right:10px;overflow:hidden">{{scope.row.createBy}}</span>
                <el-switch
                  @click.native="statusCaptcha(scope.row)"
                  class="switch captcha-img"
                  :active-value="1"
                  :inactive-value="2"
                  active-color="#0183FA"
                  inactive-color="#999"
                  v-model="scope.row.imgType"
                  active-text="打开"
                  inactive-text="关闭"
                  disabled
                ></el-switch>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="button-box">
                <p class="table-min-button" @click="buttonClick(3,scope.row)">查看</p>
                <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)">
                  <span class="table-min-button">
                    更多<i class="el-icon-d-arrow-right el-icon--right"></i>
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="1" v-hasPermiAnd="['laboratory:chart:query','laboratory:chart:edit']">编辑</el-dropdown-item>
                    <el-dropdown-item command="2" v-hasPermi="['laboratory:chart:remove']">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <add-page v-if="pageType == 2" :propsData="propsData"></add-page>
    <info-page v-if="pageType == 3" :propsData="propsData"></info-page>
    <!-- 添加或修改轮播图对话框 -->
    <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="图片名称" prop="imgName">
          <el-input v-model="form.imgName" placeholder="请输入图片名称" />
        </el-form-item>
        <el-form-item label="上传图片" prop="imgUrl">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/gif,image/png"
            :on-success="handleAvatarSuccess"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" style="height:80px;width:80px;">
            <i v-if="!form.imgUrl" class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="轮播图类型" prop="imgType">
          <template>
            <el-radio v-model="form.imgType" label="1">轮播</el-radio>
            <el-radio v-model="form.imgType" label="2">文化图</el-radio>
          </template>
        </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 { listChart, getChart, delChart, addChart, updateChart, editRotationType } from "@/api/laboratory/chart";
import { getToken } from "@/utils/auth";
import addPage from "./addPage.vue"
import infoPage from "./infoPage.vue"

export default {
  name: "Chart",
  components: {
    addPage,
    infoPage
  },
  data() {
    return {
      pageType:1,
      tableButtonType:this.hasPermiDom(['laboratory:chart:query','laboratory:chart:edit','laboratory:chart:remove']),
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图表格数据
      chartList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        tagCard:1,
        pageNum: 1,
        pageSize:20,
        searchValue: null,
        validBeginTime:null,
        validEndTime:null,
      },
      // 表单参数
      form: {
        imgType: '1'
      },

      // 表单校验
      rules: {
        imgName: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { required: true, message: "请输入名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        imgUrl: [
          { required: true, message: "请上传图片", trigger: "blur" },
        ],
        imgType: [
          { required: true, message: "请选择轮播图类型", trigger: "blur" },
        ],
      },
      dateRange:[],
      propsData:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //开启关闭切换
    statusCaptcha(row){
      let self = this;
      let text = row.imgType==1?'关闭':(row.imgType==2?'开启':'')
      this.$confirm('是否确认'+text+'?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        editRotationType(row.id).then( response => {
          self.msgSuccess('操作成功');
          row.imgType = response.data;
        });
      }).then(() => {
      }).catch(() => {});
    },
    buttonClick(type,row){
      if(this.pageType!=type){
        if(type == 1){
          this.pageType = type
          this.getList();
          this.propsData = {};
        }else if(type == 3){
          const id = row.id || this.ids
          getChart(id).then( response => {
            this.$set(this,'propsData',response.data)
            this.pageType = 3
          });
        }else{
          this.pageType = type
        }
      }
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "1":
          this.handleUpdate(row);
          break;
        case "2":
          this.handleDelete(row);
          break;
        default:
          break;
      }
    },
    // 选项卡切换
    buttonPageType(type){
      if(this.queryParams.tagCard != type){
        this.queryParams.tagCard = type;
        this.queryParams.pageNum = 1;
        this.getList();
      }
    },
    //上传
    handleAvatarSuccess(res, file) {
      this.form.imgUrl = res.data.url;
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      let type = false;
      console.log('file',file);
      if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/gif') {
        type = true;
      }else{
        this.$message.error('只能上传png/jpeg/gif格式图片');
        type = false;
      }
      return type;
    },
    /** 查询轮播图列表 */
    getList() {
      this.loading = true;
      if(this.dateRange&&this.dateRange.length>0) {
        this.queryParams.validBeginTime=this.dateRange[0]
        this.queryParams.validEndTime=this.dateRange[1]
      } else {
        this.queryParams.validBeginTime=null;
        this.queryParams.validEndTime=null
      }
      listChart(this.queryParams).then( response => {
        this.chartList =  response.rows;
        this.total =  response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        imgName: null,
        imgUrl: null,
        createBy: null,
        createTime: null,
        imgType: '1',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.validBeginTime = null;
      this.queryParams.validEndTime = null;
      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
      getChart(id).then( response => {
        this.$set(this,'propsData',response.data)
        this.pageType = 2
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChart(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChart(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delChart(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/chart/export', {
        ...this.queryParams
      }, `laboratory_chart.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .char-page{
    flex:1;
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    *{
      margin:0;
    }
    .char-max-page{
      flex:1;
      display: flex!important;
      flex-direction: column;
      .top-button-max-box{
        display: flex;
        padding:6px 10px 0;
        border-bottom:1px solid #D8D8D8;
        .top-button-box{
          height:73px;
          padding:0 25px;
          p:nth-child(1){
            line-height:68px;
            text-align: center;
            font-weight:500;
            cursor: pointer;
          }
          p:nth-child(2){
            height:5px;
            border-radius:2px;
          }
          .p-color{
            color:#0045AF;
          }
          .p-back{
            background: #0045AF;
          }
        }
      }
      .char-page-min{
        flex:1;
        display: flex!important;
        flex-direction: column;
        padding:20px!important;
        overflow: hidden;
      }
    }
    .button-box{
      width:190px;
      display: flex;
      margin:0 auto;
    }
  }
</style>
<style lang="scss">
  .char-page{
    .char-page-min{
      .switch .el-switch__label {
        position: absolute;
        display: none;
        color: #fff !important;
      }
      .switch .el-switch__label--right {
        z-index: 1;
      }
      .switch .el-switch__label--right span{
        margin-left: 10px;
      }
      .switch .el-switch__label--left {
        z-index: 1;
      }
      .switch .el-switch__label--left span{
        margin-left: 24px;
      }
      .switch .el-switch__label.is-active {
        display: block;
      }
      .switch.el-switch .el-switch__core,
      .el-switch .el-switch__label {
        width: 64px !important;
        margin: 0;
      }
    }
  }
</style>
