<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="Fid：">
            <el-input v-model="qo.LIKES_fid" placeholder="请输入Fid" clearable  style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="qo.pageNo=1;queryData()">搜 索</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>
      </div>
      <div>
        <el-table  :data="result.records" style="width: 100%">
          <el-table-column type="index" label="行号" width="60"></el-table-column>
          <el-table-column prop="fid" label="fid"></el-table-column>
          <el-table-column prop="labels" label="标签">
            <div slot-scope="scope">
              <template v-for="(tag,index) in scope.row.labelsArr">
                <el-tag :key="index" v-if="index < 5">{{ tag }}</el-tag>
              </template>
              <el-tag v-if="scope.row.labelsArr.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 from '@/utils/request';

var _this;
export default {
  name: "userManage",
  data() {
    return {
      qo: {
        pageNo: 1,
        pageSize: 10,
        LIKES_fid: '',
      },
      result: {
        records: [],
        total: 0
      },
      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: {
    queryData() {
      request({
        url: '/risk-user/query_pages',
        method: 'post',
        data: _this.qo
      }).then(res => {
        res.data.records.forEach(row => {
          row.labelsArr = row.labels ? row.labels.split(',') : [];
        });
        _this.result.records = res.data.records;
        _this.result.total = res.data.total;
      });
    },
    handleCurrentChange(val) {
      _this.qo.pageNo = val;
      _this.queryData();
    },
    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>
