<template>
  <el-container>
    <el-header v-if="isSearch">
      <el-form style="margin: 10px 0" :inline="true">
        <el-form-item label="类目名称：">
          <el-input type="text" v-model="search.categoriesName" />
        </el-form-item>
        <el-form-item>
          <el-button
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-search"
            @click="initList"
          >
            搜索
          </el-button>
          <el-button class="commonBtn" icon="el-icon-refresh" @click="reset"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </el-header>

    <el-main>
      <div class="headerTop">
        <div>
          <el-button type="primary" class="el-icon-plus" @click="doAdd(1,'')">
            新增</el-button
          >
        </div>
        <div>
          <right-toolbar  @queryTable="initList" :search="true" @update:showSearch="isSearch = !isSearch"></right-toolbar>
        </div>
      </div>

      <el-table
        :data="list"
        height="calc(100% - 80px)"
        :row-style="{ height: '0px' }"
        :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
        :cell-style="{ 'text-align': 'center', padding: '2px' }"
        style="width: 100%"
        row-key="id"
        :tree-props="{
          children: 'trainingPlanClassificationDtos',
          hasChildren: 'hasChildren'
        }"
      >
        <el-table-column width="80" type="index" label="序号" />
        <el-table-column
          prop="categoriesName"
          show-overflow-tooltip
          label="类目名称"
          align="center"
        />
        <!-- <el-table-column
          prop="classificationSort"
          show-overflow-tooltip
          label="排序"
          align="center"
        /> -->
        <el-table-column
          prop="createDate"
          show-overflow-tooltip
          label="创建时间"
          align="center"
        />
        <el-table-column
          prop="userName"
          show-overflow-tooltip
          label="创建人"
          align="center"
        />

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-show="scope.row.trainingPlanClassificationDtos" type="text" @click="doAdd(1, scope.row)">
              新增
            </el-button>
            <el-button type="text" @click="doAdd(2, scope.row)">
              编辑
            </el-button>
            <!-- <el-button type="text" @click="doWatch(scope.row)">
              查看
            </el-button> -->
            <el-button
              type="text"

              @click="doDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>

    <!-- 新增/编辑 的dialog -->
    <el-dialog
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      width="600px"
      :title="isEdit"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="类目名称：" prop="categoriesName">
          <el-input
            v-model="ruleForm.categoriesName"
            maxlength="100"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="排序：" prop="classificationSort">
          <el-input
            v-model="ruleForm.classificationSort"
            type="number"
            on-keypress="return(/^[0-9]*$/.test(String.fromCharCode(event.keyCode)))"
          ></el-input>
        </el-form-item> -->
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="submit">提 交</el-button>
      </span>
    </el-dialog>

    <!-- 查看 的dialog -->
    <el-dialog :visible.sync="LookdialogVisible" title="查看" width="700px">
      <el-row>
        <el-col :span="12">类目名称： {{ watchObj.categoriesName }} </el-col>
        <el-col :span="12">排序： {{ watchObj.classificationSort }} </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">创建时间： {{ watchObj.createDate }} </el-col>
        <el-col :span="12">创建人： {{ watchObj.createUserId }} </el-col>
      </el-row>
    </el-dialog>
  </el-container>
</template>

<script>
import {
  getTrainingPlanClassificationList,
  saveTrainingPlanClassification,
  delTrainingPlanClassificationById
} from '@/api/planClass/planClass.js'
export default {
  name: 'RuoyiPlanClass',

  data() {
    return {
      isSearch: false, //控制搜索框是否显示
      search: {
        categoriesName: ''
      },

      list: [],
      watchObj: {}, // 查看的接收
      dialogVisible: false, //新增/修改的dialog
      LookdialogVisible: false, //查看的dialog
      isEdit: '',

      ruleForm: {
        categoriesName: '',
        classificationSort: '',
        parantId: ''
      },
      rules: {
        categoriesName: [
          { required: true, message: '请输入类别名称', trigger: 'blur' }
        ],
        classificationSort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ]
      },
      loading: false // loading 效果
    }
  },

  mounted() {
    this.initList() // 列表初始化
  },

  methods: {
    // 列表初始化
    initList() {
      try {
        this.loading = true
        getTrainingPlanClassificationList(this.search).then((res) => {
          if (res.code === 200) {
            this.list = res.data
          } else {
            this.$message.error(res.msg)
          }
          this.loading = false
        })
      } catch (error) {
        this.loading = false
      }
    },
    reset(){
      this.search.categoriesName = ''
      this.initList()
    },
    doAdd(type, val) {
      if (type === 1) {
        this.isEdit = '新增'
        if(val) {
          this.ruleForm.parantId = val.id || "";
        }
      } else {
        this.isEdit = '编辑'
        this.ruleForm = {...val}
      }
      this.dialogVisible = true
    },
    // 取消按钮
    cancle() {
      this.$nextTick(() => {
        this.ruleForm = {
          categoriesName: '',
          classificationSort: ''
        }
        this.$refs.ruleFormRef.resetFields()
      })
      this.ruleForm = {
        categoriesName: '',
        classificationSort: '',
        parantId: ''
      }
      this.dialogVisible = false
    },
    // 确定按钮
    submit() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (valid) {
          const res = await saveTrainingPlanClassification(this.ruleForm)
          if (res.code !== 200) return this.$message.error(res.msg)
          this.initList()
          this.$message.success(`${this.isEdit}成功!`)
          this.ruleForm = {
            categoriesName: '',
            classificationSort: '',
            parantId: ''
          }
          this.dialogVisible = false
        } else {
          this.$message.warning("必传项不能为空!")
          return false
        }
      })
    },
    // 查看
    doWatch(val) {
      this.watchObj = val
      this.LookdialogVisible = true
    },
    // 删除
    doDelete(val) {
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delTrainingPlanClassificationById(val.id)
          if (res.code !== 200) return this.$message.error(res.msg)
          this.initList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message.warning('已取消删除')
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}

::v-deep .el-loading-mask {
  background-color: rgba(0, 222, 240, 0.2) !important;
}
::v-deep .el-loading-spinner {
  top: 65% !important;
}
::v-deep .el-loading-spinner i {
  font-size: 0.1513rem !important;
}
</style>
