<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>
          <p class="page-inquire-common-style-button" style="margin: 0 10px" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="tableButton(2)"
             v-hasPermiRouter="['cabinet:info:add']"
          >新增</p>
        </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="validBeginTime" width="300">
            <template slot-scope="scope">
              <span>{{scope.row.validBeginTime}}-{{scope.row.validEndTime}}</span>
            </template>
          </el-table-column>
          <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.createByName}}</span>
                <el-switch
                  @click.native="tableButton(4,scope.row)"
                  class="switch captcha-img"
                  :active-value="1"
                  :inactive-value="0"
                  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="操作" width="230" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['door:stock:list']"
                >详情</p>
                <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
                             v-hasPermiRouter="['cabinet:info:edit','cabinet:info:del']">
                  <p class="table-button-p">更多>></p>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="2" v-hasPermiRouter="['cabinet:info:edit']">编辑</el-dropdown-item>
                    <el-dropdown-item command="3" v-hasPermiRouter="['cabinet:info:del']">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <p class="table-button-null"></p>
              </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>
  </div>
</template>

<script>
import {
  systemRotationChartDelete,
  systemRotationChartDetail, systemRotationChartEdit,
  systemRotationChartList
} from '@/api/systemManagement/index'
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([]),
      tableDropdownType:this.hasPermiDom([]),
      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: {
    tableButton(type,row){
      let self = this;
      if(type == 1){

      }else if(type == 2){
        //新增
        this.$set(this,'pageType',2);
        this.$set(this,'propsData','');
      }else if(type == 3){
        //详情
        systemRotationChartDetail(row.id).then(response => {
          this.$set(this,'propsData',response.data);
          this.$set(this,'pageType',3);
        })
      }else if (type == 4) {
        //启用&停用
        let text = row.imgType==0 ? '停用' : '启用'
        this.$confirm('是否确认' + text + '?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
        }).then(() => {
          systemRotationChartEdit({id:row.id,imgType:row.imgType==0?1:0}).then(response => {
            self.msgSuccess(response.message)
            self.getList()
          })
        }).catch(() => {
        })
      }else if(type == 'out'){
        //返回并刷新
        this.$set(this,'pageType',1);
        this.getList();
      }
    },
    //更多
    handleCommand(val,row){
      let self = this;
      if(val == '1'){

      }else if(val == '2'){
        //编辑
        systemRotationChartDetail(row.id).then(response => {
          this.$set(this,'propsData',response.data);
          this.$set(this,'pageType',2);
        })
      }else if(val == '3'){
        //删除
        this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        }).then(() => {
          systemRotationChartDelete({id:row.id}).then(response => {
            self.msgSuccess(response.message)
            self.getList();
          });
        }).catch(() => {});
      }
    },
    //开启关闭切换
    statusCaptcha(row){
      let self = this;
      let text = row.imgType==1?'关闭':(row.imgType==2?'开启':'')
      this.$confirm('是否确认'+text+'?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        systemRotationChartDetail(row.id).then( response => {
          self.msgSuccess('操作成功');
          row.imgType = response.data;
        });
      }).then(() => {
      }).catch(() => {});
    },
    // 选项卡切换
    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
      }
      systemRotationChartList(this.queryParams).then( response => {
        this.chartList =  response.data.records;
        this.total =  response.data.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
    },
    /** 导出按钮操作 */
    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>
