// 巡逻点位管理
<template>
  <div class="app-container">
    <el-form :inline="true" :model="page.data">
      <el-form-item class="margin-left">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addOrUpdate('添加巡逻点')"
          >添加巡逻点</el-button
        >
      </el-form-item>
      <el-form-item class="margin-left input" v-if="checkRole([0])">
        <el-select
          v-model="page.data.accountId"
          filterable
          clearable
          placeholder="请选择所属账号"
        >
          <el-option
            v-for="item in option"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-select
          v-model="page.data.positionId"
          filterable
          clearable
          placeholder="请选择巡逻点"
        >
          <el-option
            v-for="item in option1"
            :key="item.id"
            :label="item.positionName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="margin-left">
        <el-button type="primary" @click="fetchData" icon="el-icon-search"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 主体表格 -->
    <el-table
      :data="tableData"
      v-loading="listLoading"
      border
      fit
      highlight-current-row
      class="table"
    >
      <el-table-column align="center" width="80" label="序号">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="accountName"
        label="所属账号"
        v-if="checkRole([0])"
      >
      </el-table-column>
      <el-table-column align="center" prop="positionName" label="巡逻点名称">
      </el-table-column>
      <!-- <el-table-column align="center" prop="expressNumber" label="NFC码">
      </el-table-column> -->
      <el-table-column align="center" prop="createTime" label="创建时间">
      </el-table-column>
      <el-table-column align="center" label="操作区" width="180">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            @click="addOrUpdate('编辑巡逻点', row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="delOrder(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      <!--分页栏-->
    </el-table>
    <pagination
      :total="total"
      :page.sync="page.pageNumber"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!-- 添加/编辑 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="570px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addOrUpdateForm"
        :rules="rules"
        ref="formRef"
        label-position="left"
        label-width="120px"
      >
        <el-form-item v-if="checkRole([0])" label="所属账号" prop="accountId">
          <el-select
            v-model="addOrUpdateForm.accountId"
            filterable
            clearable
            placeholder="请选择所属账号"
          >
            <el-option
              v-for="item in option"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="巡逻点名称" prop="positionName">
          <el-input
            v-model="addOrUpdateForm.positionName"
            placeholder="请输入巡逻点名称"
            maxlength="20"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { checkRole } from "@/utils/checkRole";
import {
  doGetPositionlist,
  doEditPosition,
  doDeletePosition,
  doGetPosition,
} from "@/api/orderRecord";
import { getUserlist } from "@/api/user";
export default {
  components: { Pagination },
  data() {
    return {
      dialogVisible: false, //新增编辑
      dialogTitle: "",
      listLoading: false,
      option: [], //账号列表
      option1: [], //巡逻点列表
      isEdit: false, //判断是否是编辑
      // 添加参数
      addOrUpdateForm: {
        accountId: "", //所属账号
        positionName: "", //点位名称
      },
      tableData: [],
      total: 0,
      // 页面
      page: {
        // 搜索表单
        data: {
          positionId: "", //所属账号
          positionName: "", //点位名称
        },
        pageNumber: 1,
        pageSize: 10,
      },
      rules: {
        accountId: [
          {
            required: true,
            message: "请选择所属账号",
            trigger: "change",
          },
        ],
        positionName: [
          {
            required: true,
            message: "请输入巡逻点名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.fetchData();
    if (checkRole([0])) {
      this.getUserList();
    }
    this.getSelectPosition();
  },
  methods: {
    checkRole,
    fetchData() {
      this.listLoading = true;
      doGetPositionlist(this.page).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    getUserList() {
      getUserlist().then((res) => {
        this.option = res.data;
      });
    },
    // 查询巡逻点
    getSelectPosition() {
      doGetPosition().then((res) => {
        this.option1 = res.data;
      });
    },
    // 添加编辑订单
    addOrUpdate(dialogTitle, edit) {
      this.dialogVisible = true;
      this.dialogTitle = dialogTitle;
      this.$nextTick(() => {
        this.$refs.formRef.resetFields();
        if (edit) {
          // 修改
          this.addOrUpdateForm = JSON.parse(JSON.stringify(edit));
          this.isEdit = true;
        } else {
          // 添加
          this.isEdit = false;
        }
      });
    },
    // 确认按钮
    submit() {
      this.$refs.formRef.validate(async (pass) => {
        if (!pass) {
          return false;
        }
        await doEditPosition({
          ...this.addOrUpdateForm,
          id: this.isEdit ? this.addOrUpdateForm.id : undefined,
        });
        await this.fetchData();
        await this.getSelectPosition();
        this.$message.success(`${this.dialogTitle}成功`);
        this.dialogVisible = false;
      });
    },
    // 删除订单
    delOrder(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        doDeletePosition(id).then((res) => {
          this.fetchData();
          this.getSelectPosition();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNumber - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>

<style lang="scss" scoped>
.margin-left {
  margin: 10px 0 0 10px;
}
.input {
  width: 150px;
}
.flex {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-bottom: 10px;
  &:last-child {
    margin-bottom: 0px;
  }
}
.content {
  display: flex;
  justify-content: space-between;
}
.shopInfo {
  width: 410px;
}
</style>
