<template>
  <div class="app-container">
    <!--搜索区-->
    <div class="float">
      <el-input
        v-model="page.data.areaName"
        placeholder="请输入区域"
        clearable
        class="margin-left input"
      ></el-input>
      <el-input
        v-model="page.data.description"
        placeholder="请输入区域描述"
        clearable
        class="margin-left input"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="fetchData"
        class="margin-left"
        >搜索</el-button
      >
    </div>
    <!--主表格-->
    <el-table
      class="table"
      ref="table_form"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column label="区域名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.areaName }}
        </template>
      </el-table-column>
      <el-table-column label="区域描述" align="center">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" width="120" v-if="role == 2">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="create('update', scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!--对话框-->
    <el-dialog
      title="编辑区域"
      :visible.sync="dialogVisible"
      width="700px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
        label-position="left"
      >
        <el-form-item
          label="区域描述"
          class="form-item-style"
          prop="description"
        >
          <el-input
            maxlength="250"
            show-word-limit
            placeholder="区域描述"
            v-model.trim="form.description"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="addOrUpdate"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { passAreaEdit, gpassAreaGetPageList } from "@/api/user_manage";
export default {
  components: { Pagination },
  filters: {},
  computed: {},
  data() {
    return {
      role: this.$store.getters.role,
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          areaName: null,
          description: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      // 添加角色参数
      form: {
        areaName: null,
        description: null,
      },
      // 验证规则
      rules: {
        areaName: [{ required: true, message: "请输入", trigger: "change" }],
        description: [{ required: true, message: "请输入", trigger: "change" }],
      },
      // 是否显示对话框
      dialogVisible: false,
      unit_list: [],
    };
  },
  created() {
    this.fetchData();
  },
  watch: {},
  methods: {
    // 获取列表数据
    fetchData() {
      this.listLoading = true;
      gpassAreaGetPageList(this.page).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    // 打开添加角色对话框
    create(type, row) {
      this.form = Object.assign(this.form, row);
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    // 确定添加/编辑
    addOrUpdate() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          passAreaEdit(this.form).then((res) => {
            this.dialogVisible = false;
            this.fetchData();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
        }
      });
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>
<style scoped lang="scss">
</style>
