<template>
  <div class="bodyBoxs">
    <div class="boxs">
      <el-table
        :data="tableData"
        v-loading="loading"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
          width="60">
        </el-table-column>
        <el-table-column
          prop="week"
          :show-overflow-tooltip="true"
          label="日期">
        </el-table-column>
        <el-table-column
          prop="path"
          label="产品卡图片">
          <template slot-scope="scope">
            <el-image style="width: 150px;" :src="scope.row.path"></el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="code"
          label="基金代码">
        </el-table-column>
        <el-table-column
          prop="since_establishment_date"
          label="基金成立日期">
        </el-table-column>
<!--        <el-table-column-->
<!--          prop="d_date"-->
<!--          label="更新日期">-->
<!--        </el-table-column>-->
        <el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button @click="exitData(scope.row)" type="primary" size="mini">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[10, 20, 30, 50]"
        :page-size=pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
      </el-pagination>
      <el-dialog
        title="编辑"
        :visible.sync="showData"
        width="40%"
        :before-close="handleClose">
        <el-form ref="form" :model="from" label-width="100px">
          <el-form-item label="产品卡">
            <el-upload
              class="avatar-uploader"
              action="https://jingji-h5.ecps.com.cn/3428_4b332f4d/index.php?s=admin/videoFileUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="from.path" :src="from.path" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="基金代码">
            <el-input v-model="from.code"></el-input>
          </el-form-item>
          <el-form-item label="基金成立日期">
            <el-input v-model="from.since_establishment_date"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="addSubmitAward">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      title: '',
      tableData: [],
      page: 1,
      pageSize: 10,
      total: 0,
      currentPage4: 1,
      from: {
        id: 0,
        path: '',
        code: '',
        since_establishment_date: ''
      },
      showData: false,
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleAvatarSuccess(res) {
      this.from.path = res.data.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    addSubmitAward() {
      this.$api.updateJjConfig(this.from).then((res) => {
        this.$message({
          message: res.message,
          type: 'success'
        });
        this.showData = false
        this.getData()
      });
    },
    editRand() {
      this.$api.editRand(this.fromRand).then((res) => {
        this.$message({
          message: res.message,
          type: 'success'
        });
        this.showDataRand = false
        this.getData()
      });
    },
    exitRand(row) {
      this.showDataRand = true
      this.fromRand.id = row.id;
      this.fromRand.rand = parseInt(row.rand);
      console.log(this.fromRand.rand)
    },
    exitData(row) {
      this.title = '编辑' + row.title
      this.showData = true
      this.from.code = row.code
      this.from.id = row.id
      this.from.path = row.path
      this.from.since_establishment_date = row.since_establishment_date
    },
    handleClose() {
      this.showData = false
      this.from = {
        id: 0,
        path: '',
        code: '',
        since_establishment_date: ''
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData()
    },
    handleCurrentChange(val) {
      console.log('点击了');
      this.page = val;
      this.getData();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    getData() {
      this.loading = true;
      this.$api.getJjConfigList({
        page: this.page,
        size: this.pageSize,
        search: this.search,
      }).then((res) => {
        this.loading = false;
        this.loading = false;
        this.tableData = res.data.data;
        this.total = res.data.total;
        this.currentPage4 = res.data.current_page;
      });
    },
  }
}
</script>
<style scoped>
.bodyBoxs {
  padding: 40px;
}

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

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

.avatar {
  width: 150px;
  height: auto;
  display: block;
}
</style>
