<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="jslx_name"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="创建人"
        width="200"
        prop="jslx_cjr"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        width="200"
        prop="jslx_date"
      ></el-table-column>
      <el-table-column label="审核人员" multiple prop="jslx_shry" width="200">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="delData(scope.row.jslx_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="cjsj">
          <el-date-picker
            v-model="dialogForm.cjsj"
            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 { sblb_list, sblb_add, sblb_edit, sblb_del } from "./api";
import { useUserStore } from "@/stores/user";
const { real_name } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      keyword: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        lbmc: "",
        cjsj: "",
        cjr: "",
        shry: "",
      },
      sh_list: [
        {
          label: "李静",
          value: "1",
        },
        {
          label: "周桂英",
          value: "2",
        },
        {
          label: "吴晗",
          value: "3",
        },
        {
          label: "杨丽梅",
          value: "4",
        },
      ],
      title: "",
      isEdit: false,
      id: "",
      loading: false,
    };
  },
  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      sblb_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: "",
        cjsj: "",
        cjr: "",
        shry: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑类别";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.jslx_id;
      this.dialogForm.lbmc = item.jslx_name;
      this.dialogForm.cjsj = item.jslx_date;
      this.dialogForm.cjr = item.jslx_cjr;
      this.dialogForm.shry = item.jslx_shry;
    },
    formSubmit() {
      let data = {
        jslx_name: this.dialogForm.lbmc,
        jslx_date: this.dialogForm.cjsj,
        jslx_cjr: real_name,
        jslx_shry: this.dialogForm.shry,
      };
      if (!this.isEdit) {
        sblb_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.jslx_id = this.id;
        sblb_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 = {
        jslx_id: id,
      };
      this.$confirm("确认删除该设备类别?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sblb_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>