<template>
  <div class="app-container">
    <!--操作区-->
    <el-button
      type="primary"
      icon="el-icon-plus"
      @click="create('add')"
      class="margin-bottom"
      >添加查获地点</el-button
    >
    <!--搜索区-->
    <div class="float">
      <el-input
        v-model="page.data.name"
        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.name }}
        </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">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="deleteFun(scope.row.safeThingsAddressId)"
            >删除</el-button
          >
          <!-- <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="dialogTitle[dialogType]"
      :visible.sync="dialogVisible"
      width="900px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="90px"
        label-position="left"
      >
        <el-form-item label="查获地点" class="form-item-style" prop="name">
          <el-input placeholder="请输入查获地点" v-model.trim="form.name" />
        </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 UploadImg from "@/components/UploadImg";
import {
  addressInsert,
  addressgetPageList,
  addressDelete,
} from "@/api/securityIncident";

export default {
  components: { Pagination, UploadImg },
  filters: {},
  computed: {},
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          name: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      // 是否显示对话框
      dialogVisible: false,
      // 对话框标题
      dialogTitle: {
        add: "添加查获地点",
        update: "编辑查获地点",
      },
      dialogType: "add",
      form_copy: null,
      // 添加角色参数
      form: {
        name: null,
      },
      // 验证规则
      rules: {
        name: [{ required: true, message: "请输入", trigger: "change" }],
      },
      type_list: [
        { id: 0, name: "未实名" },
        { id: 1, name: "已实名" },
      ],
    };
  },
  created() {
    this.fetchData();
    this.form_copy = JSON.parse(JSON.stringify(this.form));
  },
  watch: {},
  methods: {
    // 获取列表数据
    fetchData() {
      this.listLoading = true;
      addressgetPageList(this.page).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    // 打开添加角色对话框
    async create(type, row) {
      if (type === "add") {
        this.form = JSON.parse(JSON.stringify(this.form_copy));
      } else {
        this.form = Object.assign(this.form, row);
      }
      this.dialogType = type;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    // 确定添加/编辑
    addOrUpdate() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          let res;
          res = await addressInsert(this.form);
          if (res.code == 200) {
            this.dialogVisible = false;
            this.fetchData();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          }
        }
      });
    },
    // 删除
    deleteFun(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        addressDelete(id).then((res) => {
          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>
