<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew">
          <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"
          :disabled="multipleSelection.length == 0"
          @click="delData"
          >删除</el-button
        >
      </div>
      <el-form label-width="100px" :inline="true">
        <!-- <el-form-item prop="state_value">
          <el-select
            class="mr10"
            clearable
            filterable
            v-model="state_value"
            placeholder="全部"
            @change="getListData"
          >
            <el-option
              v-for="item in state_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <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
      v-loading="loading"
      :data="applyTable"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      fit
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="80">
      </el-table-column>
      <el-table-column label="会议室名称" show-overflow-tooltip prop="hys_mc">
      </el-table-column>
      <el-table-column label="可容纳人数" prop="hys_rs"></el-table-column>
      <el-table-column
        label="会议室设备"
        show-overflow-tooltip
        prop="hys_sb"
      ></el-table-column>
      <el-table-column label="操作" width="180" align="right">
        <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="581px">
      <el-form
        :model="dialogForm"
        :ref="dialogForm"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="会议室名称" prop="hys_mc">
          <el-input v-model="dialogForm.hys_mc" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="可容纳人数" prop="hys_rs">
          <el-input v-model="dialogForm.hys_rs" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="会议室设备" prop="hys_sb">
          <el-input v-model="dialogForm.hys_sb" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit(dialogForm)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { useUserStore } from "@/stores/user";

const { real_name, dept_ids } = useUserStore();
import { hys_list, hys_add, hys_edit,hys_del } from "./api";
export default {
  name: "index",
  data() {
    return {
      loading: false,
      keyword: "",
      title: "",
      applyTable: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        hys_mc: "",
        hys_rs: "",
        hys_sb: "",
      },
      hys_id: "",
      rules: {
        hys_mc: [
          { required: true, message: "请填写会议室名称", trigger: "submit" },
        ],
        hys_rs: [
          {
            required: true,
            message: "请填写会议室可容纳人数",
            trigger: "submit",
          },
        ],
        hys_sb: [
          { required: true, message: "请填写会议室设备", trigger: "submit" },
        ],
      },
      isEdit: false,
      multipleSelection:[],
      // timestamp: Date.now(),
      // userList: [],
    };
  },
  methods: {
    getListData() {
      this.loading = true;
      let data = {
        site: "jnmf",
        page: this.cur_page,
        limit: this.limit,
        hys_mc: this.keyword,
      };
      hys_list(data).then((res) => {
        if (res.code == "1") {
          this.applyTable = res.data.page_data;
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
          this.loading = false;
        }
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].hys_id);
      }
    },
    createNew() {
      this.isEdit = false;
      this.title = "新建会议室";
      this.dialogForm = {
        hys_mc: "",
        hys_rs: "",
        hys_sb: "",
      };
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.isEdit = true;
      this.hys_id = item.hys_id;
      this.title = "编辑会议室";
      Object.keys(this.dialogForm).forEach((key) => {
        this.dialogForm[key] = item[key];
      });
      // this.timestamp = Date.now();
      this.dialogFormVisible = true;
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            site: "jnmf",
            issubmit: "1",
            jdbg_hy_hys: {
              hys_mc: this.dialogForm.hys_mc,
              hys_rs: this.dialogForm.hys_rs,
              hys_sb: this.dialogForm.hys_sb,
            },
          };
          if (!this.isEdit) {
            hys_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 {
            Object.assign(data, { hys_id: this.hys_id });
            hys_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",
                });
              }
            });
          }
        }
      });
    },
    delData() {
      this.$confirm("确认删除这些会议室?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            site: "jnmf",
            hys_id: this.multipleSelection,
          };
          hys_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>