<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="关键字" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="昵称/ID" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['life:vest:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="vestList">
      <el-table-column label="马甲号ID" align="center" prop="id" width="100"/>
      <el-table-column label="头像" align="center" width="80">
        <template slot-scope="scope">
          <el-image v-if="scope.row.avatar" :src="scope.row.avatar" style="width:40px;height:40px;border-radius:50%" fit="cover"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName"/>
      <el-table-column label="简介" align="center" prop="intro" show-overflow-tooltip/>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">启用</el-tag>
          <el-tag v-else type="info">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['life:vest:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-video-camera" @click="goVideo(scope.row)" v-hasPermi="['life:video:list']">内容配置</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['life:vest:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" width="520px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="2-10个字" maxlength="10"/>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <image-upload v-model="form.avatar" :limit="1"/>
        </el-form-item>
        <el-form-item label="简介" prop="intro">
          <el-input v-model="form.intro" type="textarea" :rows="3" placeholder="选填"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLifeVest, getLifeVest, addLifeVest, updateLifeVest, delLifeVest } from '@/api/life/vest'

export default {
  name: 'LifeVest',
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      vestList: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: null
      },
      form: {},
      rules: {
        nickName: [
          { required: true, message: '昵称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度需为2-10字', trigger: 'blur' }
        ],
        avatar: [{ required: true, message: '头像不能为空', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listLifeVest(this.queryParams).then(res => {
        this.vestList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: null,
        nickName: null,
        avatar: null,
        intro: null,
        status: 1
      }
      this.resetForm('form')
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增马甲号'
    },
    handleUpdate(row) {
      this.reset()
      getLifeVest(row.id).then(res => {
        this.form = res.data
        this.open = true
        this.title = '编辑马甲号'
      })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        const req = this.form.id != null ? updateLifeVest(this.form) : addLifeVest(this.form)
        req.then(() => {
          this.msgSuccess('操作成功')
          this.open = false
          this.getList()
        })
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除马甲号「' + row.nickName + '」吗？', '警告', { type: 'warning' }).then(() => {
        return delLifeVest(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    goVideo(row) {
      this.$router.push({ path: '/live/life/video', query: { vestId: row.id } })
    }
  }
}
</script>
