<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" 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>
        <el-button type="danger" size="medium" @click="delData" :disabled="multipleSelection.length == 0">
          <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" ref="multipleTable" @selection-change="handleSelectionChange" 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="ry_name" width="200px">
      </el-table-column>
      <el-table-column label="创建人" width="" prop="create_user_name"></el-table-column>
      <el-table-column label="创建时间" width="" prop="create_dateline"></el-table-column>
      <!-- <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </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" ref="dialogForm" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="ry_name">
          <el-input v-model="dialogForm.ry_name" 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>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" :disabled="isButton" @click="formSubmit('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useUserStore } from "@/stores/user";
const { real_name, token } = useUserStore();
import { rygl_list, rygl_add, rygl_del } from "./api";
export default {
  name: "index",
  data() {
    return {
      isButton: false,
      time: "",
      keyword: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        ry_name: "",
        create_user_name: "",

      },
      rules: {
        ry_name: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ]
      },


      title: "",
      multipleSelection: [],
      id: "",
      loading: false,
      isEdit: false,
    };
  },
  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      rygl_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.isButton = false;
      this.dialogForm = {
        ry_name: "",
        create_user_name: "",
        setting: "",

      };
      this.isEdit = false;

      this.dialogFormVisible = true;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isButton = true;
          let data = {
            ry_name: this.dialogForm.ry_name,
            create_user_name: real_name,
            // create_user_name: this.dialogForm.create_user_name,

          };
          rygl_add(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "添加成功！",
                type: "success",
              });

              this.dialogFormVisible = false;
              this.getListData();
            } else {
              this.isButton = false;
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },

    formCancel() {
      this.dialogFormVisible = false;
    },
    delData() {
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            ry_id: this.multipleSelection,
          };

          rygl_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => { });
    },

    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].ry_id);
      }
    },

  },
  mounted() {
    this.getListData();
  },
};
</script>

<style scoped>
::v-deep .el-dialog__body {
  border-top: 1px solid #f0f3f8;
}
</style>