<template>
  <div class="abClass">
    <!-- 搜索 -->
    <el-form inline :model="searchForm" size="small" ref="searchForm">
      <el-form-item label="名称">
        <el-input v-model="searchForm.typeName" name="searchForm.strategyName" placeholder="名称" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.state" filterable 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="init()">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="reset()">重置</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="infoShow = true">新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="名称" prop="typeName" align="center"></el-table-column>
      <el-table-column label="标识" prop="tagName" align="center"></el-table-column>
      <el-table-column label="描述" prop="content" align="center"></el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch :value="scope.row.state" :active-value="1" :inactive-value="0"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{getTimeStr(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="editClick(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="removeInfo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination  class="mt10" layout="total, sizes, prev, pager, next, jumper" :total="total" :current-page="searchForm.pageNo" :page-size="searchForm.pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    <!-- 新增类型 -->
    <el-dialog :title="edit===0?'新建类型':'编辑类型'" :visible.sync="infoShow" append-to-body lock-scroll :close-on-click-modal="false" @close="dialogClose">
      <el-form :model="infoForm" :rules="rules" style="width:60%" ref="ruleForm" label-width="100px">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="infoForm.typeName" />
        </el-form-item>
        <el-form-item label="类型标识" prop="tagName">
          <el-input v-model="infoForm.tagName" />
        </el-form-item>
        <el-form-item label="类型描述" prop="content">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-model="infoForm.content" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-switch v-model="infoForm.state" active-text="生效" :active-value="1" inactive-text="无效" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="infoShow = false">取 消</el-button>
        <el-button type="primary" @click="edit===0?createTest():editTest()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { abTypeList, createABclass, removeABclass, editABclass } from './ABclassApi.js'
export default {
  name: 'Index',
  data () {
    return {
      edit: 0,  //是否为编辑状态标识
      //  搜索相关内容
      searchForm: {
        typeName: '',
        state: null,
        pageNo: 1,
        pageSize: 10,
      },
      total: 0, //数据总条数
      // 表格数据
      loading: true,
      tableData: [],
      infoShow: false,
      infoForm: {
        content: "",
        state: 1,
        tagName: "",
        typeName: ""
      },
      rules: {
        typeName: [{ required: true, message: "请填写类型名称", trigger: "blur" }],
        tagName: [{ required: true, message: "请填写类型标识", trigger: "blur" }],
        content: [{ required: true, message: "请填写类型描述", trigger: "blur" }],
      },
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    // 时间格式转换
    getTimeStr (timeStamp) {
      var now = new Date(timeStamp),
        y = now.getFullYear(),
        m = ("0" + (now.getMonth() + 1)).slice(-2),
        d = ("0" + now.getDate()).slice(-2);
      return y + "-" + m + "-" + d + " " + now.toTimeString().substr(0, 8);
    },
    // 获取列表
    init () {
      this.loading = true
      abTypeList(this.searchForm).then(res => {
        console.log(res)
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.loading = false
      })
    },
    // 重置
    reset () {
      this.searchForm = {
        name: null,
        state: null,
        pageNo: 1,
        pageSize: 10,
      },
        this.init();
    },
    // 编辑数据
    editClick (item) {
      this.infoForm = { ...item };
      this.edit = 1
      this.infoShow = true
    },
    // 编辑保存
    editTest () {
      this.$confirm("此操作将修改该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        editABclass(this.infoForm).then(res => {
          this.infoShow = false
          this.$message.success('操作成功!')
          this.init()
        })
      })

    },
    // 删除数据
    removeInfo (id) {
      this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeABclass({ id: id }).then(res => {
          this.$message.success('删除成功!')
          this.init()
        })
      })
    },
    // 创建字典类型
    createTest () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createABclass(this.infoForm).then(res => {
            this.$message.success('创建成功!')
            this.infoShow = false
            this.init()
          })
        }
      });
    },

    // 列表选择每页数量
    handleSizeChange (val) {
      this.searchForm.pageNo = 1
      this.searchForm.pageSize = val
      this.init();
    },
    // 列表选择当前页
    handleCurrentChange (val) {
      this.searchForm.pageNo = val
      this.init();
    },
    // 清除弹窗值
    dialogClose () {
      this.edit = 0
      this.infoForm = {
        content: "",
        state: 0,
        tagName: "",
        typeName: ""
      }
    }
  }
}
</script>

<style lang="scss">
.abClass {
  padding: 20px;
}
</style>
