<style lang="scss">
.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: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<template>
  <section class="content">
    <h4>牛人点评</h4>
    <el-divider></el-divider>
    <el-form label-width="80px" class="filter-form">
      <el-row>
        <el-col :span="6">
          <el-form-item label="股票名称">
            <el-input
              clearable
              @clear="getData()"
              v-model="queryParams.stock_name"
              placeholder="请输入股票名称/代码"
              size="mini"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="10" style="margin-left: 10px">
            <el-button type="primary" @click="getData" size="mini"
              >筛选</el-button
            >
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增点评</el-button
            >
            <!-- <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleFastAdd"
              >新增选手</el-button
            > -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%; margin-top: 10px"
      height="50vh"
    >
      <el-table-column align="center" prop="stock_name" label="股票名称" />
      <el-table-column align="center" prop="stock_code" label="股票代码" />
      <el-table-column label="图片">
        <template slot-scope="scope">
          <el-image
            style="width: 80px; height: 60px"
            :src="scope.row.img"
            :preview-src-list="[scope.row.img]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="content" label="点评内容">
        <template slot-scope="scope">
          {{ scope.row.content.substring(0, 20) }}...
        </template>
      </el-table-column>
      <el-table-column align="center" prop="ctime" label="创建时间" />
      <el-table-column align="center" prop="date" label="操作" width="320">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row.id)" size="mini" type="warning"
            >编辑</el-button
          >
          <el-button @click="del(scope.row.id)" size="mini" type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Page
      ref="pageButton"
      :current="form.page"
      :page_size="form.page_size"
      :total="total"
      @pageChange="gopage"
    />
    <!-- 新增用户 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="股票" prop="stock_id">
          <el-select
            @change="changeStock"
            v-model="form.stock_id"
            placeholder="请输入股票名称"
            filterable
            remote
            :remote-method="remoteMethod"
          >
            <el-option
              v-for="item in stockList"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/uploadfile"
            :data="{ watermark: 0 }"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.img" :src="form.img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="点评内容" prop="content">
          <el-input
            clearable
            v-model="form.content"
            placeholder="请输入点评内容"
            type="textarea"
            rows="10"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import Page from "../../components/Page";
export default {
  components: {
    Page,
  },
  data() {
    return {
      loading: false,
      queryParams: {
        page: 1,
      },
      form: {},
      form1: {},
      form2: {},
      list: [{}, {}],
      total: 0,
      title: "新增用户",
      open: false,
      stockList: [],
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        player_type: [
          {
            required: true,
            message: "请选择用户类型",
            trigger: "change",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入用户手机号",
            trigger: "blur",
          },
        ],
        avatar: [
          {
            required: true,
            message: "请输入用户手机号",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    del(id) {
      this.$confirm("删除用户会删除相关数据,确定删除吗?", "提示", {
        type: "warning",
      }).then(() => {
        this.$api
          .delStockNBComments({
            id: id,
          })
          .then((res) => {
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.getData();
          });
      });
    },
    gopage(size) {
      if (size) {
        this.queryParams.page_size = size;
      }
      this.queryParams.page = this.$refs.pageButton.page;
      this.getData();
    },
    getData() {
      this.loading = true;
      this.$api.getStockNBCommentsList(this.queryParams).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    handleAdd() {
      this.open = true;
      this.title = "新增牛人点评";
      this.form = {};
    },
    edit(id) {
      this.title = "编辑牛人点评";
      this.$api
        .getStockNBCommentsInfo({
          id: id,
        })
        .then((res) => {
          this.form = res.data.data;
          this.open = true;
        });
    },
    black(row) {
      this.$api
        .commentsBlack({
          id: row.id,
        })
        .then((res) => {
          this.getData();
        });
    },
    handleAvatarSuccess(res, file) {
      this.form.img = res.data.url;
      console.log(this.form.img);
    },
    beforeAvatarUpload(file) {
      return;
      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;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$api.editStockNBComments(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          } else {
            this.$api.addStockNBComments(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          }
        }
      });
    },
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        this.$api
          .getStockSearchList({
            name: query,
          })
          .then((res) => {
            this.stockList = res.data.data;
            this.$set(this.stockList, res.data.data);
            this.loading = false;
          });
      } else {
        this.stockList = [];
      }
    }
  },
  created() {
    this.getData();
  },
};
</script>