<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" 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="prompt" label="提示词" :show-overflow-tooltip="true"/>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state == 0">生成中</el-tag>
            <el-tag v-if="scope.row.state == 1" type="success">已完成</el-tag>
            <el-tag v-if="scope.row.state == 2" type="danger">生成失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="failMsg" 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 prop="mjAction"  label="操作类型" />
        <el-table-column prop="params"  label="绘制参数" />
        <el-table-column label="是否公开">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.isPublic" @change="switchChange($event,scope.row)"  active-color="#13ce66"  inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="绘制时间" />
      </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: {
    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() {
      this.loading = true;
      this.$api.getMjList({
        page: this.page,
        size: this.pageSize,
        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>
