<template>
  <div>
    <div class="menu-title">
      用户管理
    </div>
    <div class="menu-content">
      <div>
        <el-form :inline="true" :model="qo" class="demo-form-inline">
          <el-form-item label="唯一标识：">
            <el-input v-model="qo.LIKES_fid" placeholder="请输入唯一标识" clearable style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="queryDataById()">搜 索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryData(-1)">上一页</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryData(1)">下一页</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="margin: 0px 0 20px 0">
        <el-button type="primary" @click="createRow()" icon="el-icon-plus">添 加</el-button>
        <el-button type="primary" @click="downloadUser()" icon="el-icon-download">导出用户</el-button>
        <el-button type="primary" @click="showImportUserPage()" icon="el-icon-upload2">导入用户</el-button>
      </div>
      <div>
        <el-table :data="result.records" style="width: 100%" v-loading="loading">
          <el-table-column type="index" label="行号" width="60"></el-table-column>
          <el-table-column prop="fid" label="唯一标识"></el-table-column>
          <el-table-column prop="realname" label="姓名"></el-table-column>
          <el-table-column prop="labels" label="标签">
            <div slot-scope="scope">
              <template v-for="(tag,index) in scope.row.labelArr">
                <el-tag :key="index" v-if="index < 5">{{ tag }}</el-tag>
              </template>
              <el-tag v-if="scope.row.labelArr.length > 5">...</el-tag>
            </div>
          </el-table-column>
          <el-table-column label="操作" width="240">
            <template slot-scope="scope">
              <el-button @click.native.prevent="linkUser(scope.row)" type="text" size="small">
                关注用户
              </el-button>
              <el-button @click.native.prevent="similarUser(scope.row)" type="text" size="small">
                相似
              </el-button>
              <el-button @click.native.prevent="recommendItem(scope.row)" type="text" size="small">
                查看推荐
              </el-button>
              <el-button @click.native.prevent="modifyRow(scope.row)" type="text" size="small">
                编辑
              </el-button>
              <el-button @click.native.prevent="removeRow(scope.row)" type="text" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--      <div class="page-box">-->
      <!--        <el-pagination background-->
      <!--                       @current-change="handleCurrentChange"-->
      <!--                       :current-page="qo.pageNo"-->
      <!--                       :page-size="qo.pageSize"-->
      <!--                       layout="total, prev, pager, next"-->
      <!--                       :total="result.total">-->
      <!--        </el-pagination>-->
      <!--      </div>-->
    </div>
    <el-dialog
        :title="dialogName"
        :visible.sync="cmdDialogVisible"
        width="40%">
      <el-form ref="cmd" label-width="100px" :rules="rules" :model="cmd">
        <el-form-item label="fid" prop="fid">
          <el-input v-model="cmd.fid" placeholder="请输入fid"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cmdDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request, {getBaseUrl} from '@/utils/request';

var _this;
var _index = 0;
export default {
  name: "userManage",
  data() {
    return {
      qo: {
        pageNo: 1,
        pageSize: 10,
        LIKES_fid: '',
      },
      result: {
        records: [],
        total: 0
      },
      loading: false,
      cursorArr: [''], // 分页游标
      dialogName: '编辑',
      cmdDialogVisible: false,
      cmd: {},
      rules: {
        fid: [
          {required: true, message: '请输入fid'}
        ],
      },
    }
  },
  mounted() {
    _this = this;
    let _qo = _this.$route.query.qo;
    if (_qo) {
      _this.qo = JSON.parse(_qo);
    }
    _this.queryData();
  },
  methods: {
    queryDataById() {
      if (this.qo.LIKES_fid.length > 0) {
        this.loading = true;
        request({
          url: '/gorse/query_user/' + this.qo.LIKES_fid,
          method: 'get',
        }).then(res => {
          let user = res.data;
          if (!user) {
            _this.result.records = [];
          } else {
            if (user.labels) {
              user.labelArr = user.labels.split(',');
            } else {
              user.labelArr = [];
            }
            _this.result.records = [user];
          }

          this.loading = false;
        });
      } else {
        this.cursorArr = [''];
        this.queryData(1);
      }
    },
    queryData(index) {
      this.loading = true;
      if (index < 0 && this.cursorArr.length >= 2) {
        this.cursorArr = this.cursorArr.slice(0, this.cursorArr.length - 2);
        this.cursorArr = this.cursorArr.length === 0 ? [''] : this.cursorArr;
      }
      request({
        url: '/gorse/query_users?cursor=' + this.cursorArr[this.cursorArr.length - 1],
        method: 'get',
      }).then(res => {
        this.cursorArr.push(res.data.cursor);
        res.data.users.forEach(row => {
          row.labelArr = row.labels ? row.labels.split(',') : [];
        });
        _this.result.records = res.data.users;
        this.loading = false;
      });
    },
    handleCurrentChange(val) {
      _this.qo.pageNo = val;
      _this.queryData();
    },
    showImportUserPage() {
      _this.$router.push({
        path: "importUser"
      });
    },
    downloadUser() {
      window.open(getBaseUrl() + '/bulk/get_bulk/users');
    },
    linkUser(item) {
      _this.$router.push({
        path: "linkUser", query: {
          userId: item.fid,
          qo: JSON.stringify(_this.qo)
        }
      });
    },
    similarUser(item) {
      _this.$router.push({
        path: "similarUser", query: {
          userId: item.fid,
          qo: JSON.stringify(_this.qo)
        }
      });
    },
    recommendItem(item) {
      _this.$router.push({
        path: "recommendItem", query: {
          userId: item.fid,
          qo: JSON.stringify(_this.qo)
        }
      });
    },
    removeRow(item) {
      _this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: '/risk-user/remove/' + item.id,
          method: 'post'
        }).then(res => {
          _this.$message.success("删除成功");
          _this.queryData();
        });
      }).catch(() => {
      });
    },
    createRow() {
      _this.dialogName = '新建';
      _this.cmdDialogVisible = true;
      _this.$nextTick(_ => {
        _this.$refs['cmd'].clearValidate();
        _this.cmd = {
          fid: '',
        };
      })
    },
    modifyRow(item) {
      _this.dialogName = '编辑';
      _this.cmdDialogVisible = true;
      _this.$nextTick(_ => {
        _this.$refs['cmd'].clearValidate();
        _this.cmd = JSON.parse(JSON.stringify(item));
      })
    },
    submitModify() {
      this.$refs.cmd.validate((valid) => {
        if (valid) {
          if (_this.cmd.id) {
            request({
              url: '/risk-user/modify',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("编辑成功");
              _this.queryData();
              _this.cmdDialogVisible = false;
            });
          } else { // 新建
            request({
              url: '/risk-user/create',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("添加成功");
              _this.cmdDialogVisible = false;
            });
          }
        }
      });
    }
  }
}
</script>

<style lang="scss">
</style>
