<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="标签名称" prop="labelName">
        <el-input
          v-model="queryParams.labelName"
          placeholder="请输入标签名称"
          clearable v-no-whitespace
          @keyup.enter.native="handleSearch"
        />
      </el-form-item>
      <!-- 是否启用 -->
      <el-form-item label="是否启用" prop="isEnabled">
        <el-select v-model="queryParams.isEnabled" placeholder="请选择是否启用" clearable>
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="handleDialog" >新建标签</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table :data="tableData" fit highlight-current-row style="width: 100%" @selection-change="selectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column
        v-for="item in tableHeader"
        :key="item.prop"
        :prop="item.prop"
        :label="item.label"
        align="center"
      >
        <template v-slot="scope" v-if="item.prop === 'isEnabled'">
          <el-switch
            v-model="scope.row.isEnabled"
            active-value="1"
            inactive-value="0"
            @change="changeStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleDialog(scope.row)"  icon="el-icon-edit">
            重命名
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleDelete(scope.row)"

            icon="el-icon-delete"
            style="color: #e47470"
          >
            删除
          </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="dialogTitle" :visible.sync="dialogVisible" width="25%">
      <el-form :model="dialogForm" ref="dialogForm" size="small" label-width="110px" :rules="rules">
        <el-form-item label="标签名称" prop="labelName">
          <el-input v-model="dialogForm.labelName" placeholder="请输入标签名称" clearable maxlength="10"  v-no-whitespace/>
        </el-form-item>
        <!-- 是否启用-switch -->
        <el-form-item label="是否启用" prop="isEnabled">
          <el-switch
            v-model="dialogForm.isEnabled"
            active-text="启用"
            inactive-text="禁用"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="handleSave" v-preventReClick="5000">确 定</el-button>
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  labelLibraryList,
  addLabel,
  editLabel,
  delLabel,
  editEnabled,
} from '@/api/educationalTraining/resourceAdministration'

export default {
  data() {
    return {
      // 是否显示搜索
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 标签名称
        labelName: '',
        // 是否启用
        isEnabled: '',
      },
      // 表格数据 - 表头
      tableHeader: [
        { prop: 'labelName', label: '标签名称' },
        { prop: 'isEnabled', label: '是否启用' },
        { prop: 'createUserName', label: '创建人' },
        { prop: 'createTime', label: '创建时间' },
      ],
      // 列表数据
      tableData: [],
      // 弹框标题
      dialogTitle: '',
      // 弹框
      dialogVisible: false,
      // 弹框表单
      dialogForm: {
        labelName: '',
        isEnabled: '1',
      },
      // 弹框表单验证
      rules: {
        labelName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
      },
      isEdit: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 搜索
    handleSearch() {
      console.log('搜索', this.queryParams)
      this.getList()
    },
    // 重置
    handleReset() {
      console.log('重置')
      // 重置查询参数 resetFields
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList() {
      console.log('获取列表')
      try {
        const res = await labelLibraryList(this.queryParams)
        console.log('🚀 ~ getList ~ res-标签库-列表:', res)
        this.tableData = res.rows
        this.total = res.total
      } catch (error) {
        console.log('error', error)
      }
    },
    // 导出
    handleExport() {
      console.log('导出')
    },
    // 重命名
    async handleDialog(row) {
      console.log('重命名', row)
      // 打开弹框 修改课件名
      await (this.dialogVisible = true)
      // 重置
      this.$refs.dialogForm.resetFields()
      if (row.labelId) {
        this.isEdit = true
        this.dialogTitle = '重命名'
        this.dialogForm.labelId = row.labelId
        this.dialogForm.labelName = row.labelName
        this.dialogForm.isEnabled = row.isEnabled
        console.log('🚀 ~ handleDialog ~ this.dialogForm.labelName:', this.dialogForm.labelName)
      } else {
        this.isEdit = false
        this.dialogTitle = '新建标签'
        this.dialogForm.labelName = ''
      }
    },
    // 删除
    handleDelete(row) {
      console.log('删除', row)
      // 弹框提示
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 删除
          console.log('删除', row)
          const params = {
            labelId: row.labelId,
            type: 3,
          }
          delLabel(params)
            .then(res => {
              console.log('🚀 ~ delLabel ~ res:', res)
              if (res.code === 200) {
                this.$message.success('删除成功')
                this.getList()
              }
            })
            .catch(() => {
              console.log('删除失败')
              this.getList()
            })
        })
        .catch(() => {
          // 取消
        })
    },
    // 多选框选中数据
    selectionChange(sel) {
      console.log('多选框选中数据', sel)
    },
    // 保存修改
    handleSave() {
      console.log('保存修改')
      // 表单验证
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          // 保存修改
          console.log('保存修改', this.dialogForm)
          if (this.isEdit) {
            // 编辑
            const params = {
              labelId: this.dialogForm.labelId,
              labelName: this.dialogForm.labelName,
              isEnabled: this.dialogForm.isEnabled,
              type: 2,
            }
            editLabel(params).then(res => {
              console.log('🚀 ~ handleSave ~ res:', res)
              // 刷新列表
              this.getList()
              // 关闭弹框
              this.dialogVisible = false
            })
          } else {
            // 新建
            const params = {
              labelName: this.dialogForm.labelName,
              isEnabled: this.dialogForm.isEnabled,
              type: 1,
            }
            addLabel(params).then(res => {
              console.log('🚀 ~ handleSave ~ res:', res)
              // 刷新列表
              this.getList()
              // 关闭弹框
              this.dialogVisible = false
            })
          }
        }
      })
    },
    // 切换状态
    changeStatus(row) {
      console.log('切换状态', row)
      const params = {
        labelId: row.labelId,
        isEnabled: row.isEnabled,
        type: 4,
      }
      editEnabled(params)
        .then(res => {
          console.log('🚀 ~ changeStatus ~ res:', res)
          if (res.code === 200) {
            this.$message.success('操作成功')
            this.getList()
          }
        })
        .catch(() => {
          console.log('操作失败')
          setTimeout(() => {
            this.getList()
          }, 200)
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.tableScopeSwitch .el-switch__label {
  position: absolute;
  display: none;
  color: #fff;
}
/*打开时文字位置设置*/
.tableScopeSwitch .el-switch__label--right {
  z-index: 1;
  right: 6px; /*不同场景下可能不同，自行调整*/
}
/*关闭时文字位置设置*/
.tableScopeSwitch .el-switch__label--left {
  z-index: 1;
  left: 6px; /*不同场景下可能不同，自行调整*/
}
/*显示文字*/
.tableScopeSwitch .el-switch__label.is-active {
  display: block;
}
.tableScopeSwitch.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 75px !important; /*开关按钮的宽度大小*/
}
</style>
