<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>类别名称:</span>
        <el-input
          v-model="listQuery.className"
          placeholder="类别名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>状态:</span>
        <el-select v-model="listQuery.status" style="width: 120px" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="1" label="启用" />
          <el-option :value="2" label="停用" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" @click="dialogFormVisible = true"> 添加类别 </el-button>
        <el-button type="primary" @click="getList"> 一键排序 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" :height="tableHeight">
      <el-table-column label="序号" align="center" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="className" label="咨询类别名称" align="center" />
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 2" type="danger" size="small">已停用</el-tag>
          <el-tag v-else type="success" size="small">已启用</el-tag>
          <el-button v-if="row.status === 2" type="primary" size="mini" @click="handleShangeStatus(row.id, row.status)">
            启用
          </el-button>
          <el-button v-else type="primary" size="mini" @click="handleShangeStatus(row.id, row.status)">
            停用
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="排序" width="80" align="center">
        <template slot-scope="{ row }">
          <el-input v-model="row.sortNumber" maxlength="4" minlength="1" @blur="consultSort(row)" />
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="创建时间" align="center">
        <template slot-scope="{ row }">
          {{ row.createdTime | parseTime }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleEdit(row)"> 编辑 </el-button>
          <!-- <el-button type="danger" size="mini" style="margin: 2px" @click="handleDelete(row)"> 删除 </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[10, 20, 30, 100]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchRecordList"
    />
    <!-- 添加咨询类别 -->
    <el-dialog title="咨询类别添加" :visible.sync="dialogFormVisible" width="500px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="120px">
        <el-form-item label="咨询类别名称:" prop="className">
          <el-input v-model="form.className" type="text" placeholder="咨询类别名称" maxlength="50" />
        </el-form-item>
        <el-form-item label="咨询类别排序:" prop="sortNumber">
          <el-input v-model="form.sortNumber" type="text" placeholder="咨询类别排序" maxlength="10" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSaveConsult">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getConsultList, saveConsult, deleteConsult, consultSort } from '@/api/user/consult/consult'
const defaultListQuery = {
  className: '', // 类别名称
  status: '', // 状态 1启用，2停用，默认1
  pageNum: 1,
  pageSize: 20
}
export default {
  name: 'GenreList',
  filters: {},
  data() {
    return {
      isLoading: true,
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      dialogFormVisible: false,
      form: {
        className: '',
        sortNumber: ''
      },
      rules: {
        className: [{ required: true, message: '请填写咨询类别名称', trigger: 'blur' }],
        sortNumber: [{ required: true, message: '请填写咨询类别排序', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取咨询类别列表
    getList() {
      this.listQuery.pageNum = 1
      this.getConsultList()
    },

    // 获取咨询类别列表
    async getConsultList() {
      try {
        this.isLoading = true
        const res = await getConsultList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 操作启用停用
    handleShangeStatus(id, status) {
      let confirmTxt = ''
      let paramsStatus
      if (status === 1) {
        confirmTxt = '确定停用该类别？'
        paramsStatus = 2
      } else {
        confirmTxt = '确定启用该类别？'
        paramsStatus = 1
      }
      this.$confirm(confirmTxt)
        .then((_) => {
          this.getConsultStatus(id, paramsStatus)
        })
        .catch(() => {
          console.log('用户取消操作')
        })
    },
    // 上下线轮播图
    async getConsultStatus(id, paramsStatus) {
      try {
        await saveConsult({ id: id, status: paramsStatus })
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    // 确认保存类别
    async handleSaveConsult() {
      try {
        await this.$refs.dataForm.validate()
        this.saveConsult(this.form)
      } catch (error) {
        console.log(error)
      }
    },
    // 保存咨询类别
    async saveConsult(params) {
      await saveConsult(params)
      this.$message.success('保存成功')
      this.closeDialog()
      this.getList()
    },
    // 添加标签取消
    closeDialog() {
      this.$refs.dataForm.resetFields()
      this.dialogFormVisible = false
    },
    // 编辑类别
    handleEdit(row) {
      this.form = { ...this.form, ...row }
      this.dialogFormVisible = true
    },
    // 操作删除类别
    async handleDelete(row) {
      try {
        await this.$confirm('确定删除该类别吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.deleteConsult(row)
      } catch (error) {
        this.$message.info('已取消删除操作')
      }
    },
    // 调用删除类别
    async deleteConsult(row) {
      try {
        await deleteConsult(row.id)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 咨询类别排序
    async consultSort(row) {
      try {
        await consultSort({ id: row.id, sortNumber: row.sortNumber })
        this.getList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style></style>
