<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建类别</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="keyword"
            clearable
            @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索</el-button
        >
      </el-form>
    </div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column
        label="类别名称"
        show-overflow-tooltip
        prop="kylb_title"
        width="auto"
      >
      </el-table-column>
      <el-table-column label="创建人" width="150" prop="kylb_cjr">
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="200"
        prop="kylb_cjrq"
      ></el-table-column>
      <el-table-column label="审核人员" prop="kylb_shry" width="150">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑</el-button>
          <el-button type="text" @click="delData(scope.row.kylb_id)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="580px">
      <el-form :model="dialogForm" label-width="80px">
        <el-form-item label="类别名称" prop="lbmc">
          <el-input
            v-model="dialogForm.lbmc"
            placeholder="请输入类别名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="创建时间" prop="cjrq">
          <el-date-picker
            v-model="dialogForm.cjrq"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="审核人员" prop="shry">
          <el-input
            v-model="dialogForm.shry"
            placeholder="请输入审核人员"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useUserStore } from "@/stores/user";
const { real_name } = useUserStore();
import { kylb_list, kylb_add, kylb_edit, kylb_del } from "./api";

export default {
  name: "index",
  data() {
    return {
      keyword: "",
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        lbmc: "",
        cjrq: "",
        shry: "",
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
    };
  },
  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      kylb_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建类别";
      this.dialogForm = {
        lbmc: "",
        cjrq: "",
        shry: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑类别";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.kylb_id;
      this.dialogForm.lbmc = item.kylb_title;
      this.dialogForm.cjrq = item.kylb_cjrq;
      this.dialogForm.shry = item.kylb_shry;
    },

    formSubmit() {
      let data = {
        kylb_title: this.dialogForm.lbmc,
        kylb_cjr: real_name,
        kylb_cjrq: this.dialogForm.cjrq,
        kylb_shry: this.dialogForm.shry,
      };
      if (!this.isEdit) {
        kylb_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "类别添加成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        data.kylb_id = this.id;
        kylb_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "类别编辑成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        kylb_id: id,
      };
      this.$confirm("确认删除该流程?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          kylb_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "流程删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>

<style scoped>
</style>