<template>

  <div class="bodyBoxs">
    <div style="margin-bottom: 15px;width: 100%">
      <el-row>
        <el-col :span="8">
          <el-row>
            <el-col :span="6">
              <div style="color: #666666;margin-left: 15px;line-height: 36px;">
                日期:
              </div>
            </el-col>
            <el-col :span="18">
              <el-date-picker style="width: 100%;" v-model="searchDate" value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="8">
          <el-input :clearable="true" placeholder="请输入提示词内容" v-model="search" class="input-with-select">
            <el-button style="width: 80px;" slot="append" icon="el-icon-search" @click="getData">搜索</el-button>
          </el-input>
        </el-col>
      </el-row>
    </div>
    <div class="boxs">
      <el-table :data="tableData" @sort-change="handleSortChange" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="编号" width="80" />
        <el-table-column prop="user.nick" label="昵称" />
        <el-table-column label="头像">
          <template slot-scope="scope">
            <el-image v-if="scope.row.user.head" style="width: 40px;border-radius: 50%;"
              :src="scope.row.user.head"></el-image>
            <span v-else>暂未授权</span>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="提示词" :show-overflow-tooltip="true" />
        <el-table-column label="预览图片">
          <template slot-scope="scope">
            <el-image :preview-src-list="srcList" v-if="scope.row.previewImage" style="max-width: 80px;"
              :src="scope.row.previewImage"></el-image>
            <span v-else>生成失败</span>
          </template>
        </el-table-column>
        <el-table-column label="所属类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type == 2" type="success">SD</el-tag>
            <el-tag v-if="scope.row.type == 1" type="danger">MJ</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="params" label="绘制参数" />
        <el-table-column sortable="custom" label="排序">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.sort" @blur="updateSort($event,scope.row)" :maxlength="10" :minlength="1" placeholder="请输入排序值"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="公开时间" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="deleteImage(scope.row.id)" type="danger" icon="el-icon-delete" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4"
        :page-sizes="[15, 20, 30, 50]" :page-size=pageSize layout="total, sizes, prev, pager, next, jumper"
        :total=total>
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
  data() {
    return {
      searchDate: '',
      tableData: [],
      loading: false,
      page: 1,
      pageSize: 15,
      total: 0,
      currentPage4: 1,
      search: '',
      model: '',
      srcList: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleSortChange({ prop, order }) {
        this.page = 1;
        if(order == 'descending'){
          this.getData('desc')
        }else{
          this.getData('asc')
        }
      },
    updateSort(event,row){
      this.$confirm('确定更改公开图片的排序值，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        this.$api.updateSort({
          id: row.id,
          sort:row.sort
        }).then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.$message({
              message: res.message,
              type: 'success'
            });
          } else {
            this.$message({
              message: res.message,
              type: 'error'
            });
          }
        }).catch((res) => {
          this.loading = false;
        });
      }).catch((e) => {
        this.$message({
          type: 'warning',
          message: '已取消'
        });
      });
    },
    deleteImage(id) {
      this.$confirm('确定删除该公开图片，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        this.$api.deleteImagePublic({
          id: id,
        }).then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.$message({
              message: res.message,
              type: 'success'
            });
          } else {
            this.$message({
              message: res.message,
              type: 'error'
            });
          }
          this.getData()
        }).catch((res) => {
          this.loading = false;
        });
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消'
        });
      });

    },
    switchChange(res, item) {
      this.loading = true;
      this.$api.updateMjData({
        id: item.id,
        state: res
      }).then((res) => {
        this.loading = false;
        this.$message({
          message: res.message,
          type: 'success'
        });
      }).catch((res) => {
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData()
    },
    handleCurrentChange(val) {
      console.log('点击了');
      this.page = val;
      this.getData();
    },
    getData(order='') {
      this.loading = true;
      this.$api.getPublicList({
        page: this.page,
        size: this.pageSize,
        order: order,
        search: this.search,
        searchDate: this.searchDate,
      }).then((res) => {
        this.loading = false;
        if (res.code == 0) {
          this.tableData = res.data.data;
          this.total = res.data.total;
          this.currentPage4 = res.data.current_page;
          this.srcList = res.data.srclist
        } else {
          this.$message({
            message: res.message,
            type: 'success'
          });
        }
      }).catch((res) => {
        this.loading = false;
      });
    },
  }
}
</script>
<style scoped>
.bodyBoxs {
  padding: 15px;
}

.boxs {
  padding: 15px;
  width: 100%;
  background: #fff;
  border-radius: 15px;
}

.input-with-select {
  width: 100%;
  margin-left: 15px;
}
</style>
