<template>
  <div class="container">
    <div>
      <el-form :inline="true" :model="typeQurey">
        <el-form-item label="类别名称">
          <el-input
            v-model="typeQurey.typeName"
            placeholder="类别名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="列别">
          <el-select v-model="typeQurey.typeCategory" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="qurey()"
            >查询</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-refresh-right"
            @click="reset()"
            >重置</el-button
          >
          <el-button
            type="success"
            icon="el-icon-circle-plus-outline"
            @click="addtype()"
            >添加类型</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="conter">
      <el-table
        :data="typeList"
        style="width: 100%; text-align: center"
        border
      >
        <el-table-column label="序号" width="50px">
          <template slot-scope="scope">
            {{
              (typeQurey.pageNum - 1) * typeQurey.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="类别名称" />
        <el-table-column label="类别种类" >
          <template slot-scope="scope">
            <el-tag
            :type="scope.row.typeCategory == '0' ? 'success' :  'danger' "
            >
            {{scope.row.typeCategory == '0' ? '收入类别' :  '支出类别'}}
          </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button style="margin:5px"  size="mini" icon="el-icon-edit" type="info" @click="updataType(scope.row)">修改信息</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="footer">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="typeQurey.pageNum"
        :page-size="typeQurey.pageSize"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { select } from "@/api/type";

export default {
  data() {
    return {
      typeQurey: {
        typeName: null,
        typeCategory: null,
        companyId: null,
        pageNum: 1,
        pageSize: 10,
      },
      options: [
        {
          value: "0",
          label: "收入类别",
        },
        {
          value: "1",
          label: "支出类别",
        },
      ],
      total:0,
      typeList:[]
    };
  },
  created() {
    this.typeQurey.companyId = this.$store.getters.companyId
    this.getTypeList()
  },
  methods: {
    //获取种类
    getTypeList(){
      select(this.typeQurey).then((res)=>{
        if(res.success){
          this.typeList = res.data.list
          this.total = res.data.total
        }else{
          this.$message.error(res.msg)
        }
      }).catch((error)=>{
        this.$message.error("查询失败")
      })
    },
     //重置
    reset() {
      this.typeQurey = {
        typeName: null,
        typeCategory: null,
        companyId: null,
        pageNum: 1,
        pageSize: 10,
      }
      this.typeQurey.companyId = this.$store.getters.companyId
      this.getTypeList()
    },
    //添加类型
    addtype() {
      this.$router.push('/typeAdd')
    },
    //修改类型
    updataType(data){
      this.$router.push({
        path: '/typeUpdata',
        query:{
          id:data.typeId,
        }
      })
    },
    //搜索
    qurey(){
      this.typeQurey.pageNum = 1
      this.getTypeList()
    },
    //换页
    handleCurrentChange(val){
      this.typeQurey.pageNum = val
      this.getTypeList()
    }
  },
};
</script>


<style scoped>
.container {
  padding: 10px;
}
.conter {
  margin-bottom: 10px;
}
.footer {
  text-align: right;
}
</style>