<!--
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-10-08 09:26:24
 * @LastEditors: SunTao 328867980@qq.com
 * @LastEditTime: 2024-12-19 15:31:19
 * @FilePath: \znxjxt-ui\src\views\xj\route\index.vue
 * @Description: 路线管理-道路线
-->

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="路线编码" prop="routeCode">
        <el-input
          v-model="queryParams.routeCode"
          placeholder="请输入路线编码，唯一"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路线名称" prop="routeName">
        <el-input
          v-model="queryParams.routeName"
          placeholder="请输入路线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路线全称" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入路线全称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xj:route:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xj:route:remove']"
          >删除
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xj:route:export']"
          >导出
        </el-button>
      </el-col> -->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="routeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="路线编码" align="center" prop="routeCode" />
      <el-table-column label="路线名称" align="center" prop="routeName" />
      <el-table-column label="路线全称" align="center" prop="fullName" />
      <el-table-column label="方向" align="center" prop="direction" />
      <el-table-column label="起点桩号" align="center" prop="startMilepost" />
      <el-table-column label="止点桩号" align="center" prop="endMilepost" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updatedBy" />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updatedAt"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xj:route:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xj:route:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改道路线对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="80%"
      append-to-body
    >
      <template v-if="dialogVisible">
        <div style="display: flex">
          <!-- 左侧表单部分 -->
          <div style="flex: 1; padding-right: 40px">
            <el-form
              ref="form"
              :model="lineForm"
              :rules="rules"
              label-width="5rem"
            >
              <el-form-item>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="路线编码" prop="routeCode">
                      <el-input v-model="lineForm.routeCode"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="路线名称" prop="routeName">
                      <el-input v-model="lineForm.routeName"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item>
                <el-row :gutter="24">
                  <!-- <el-col :span="12">
                    <el-form-item label="路线全称" prop="fullName">
                      <el-input v-model="lineForm.fullName"></el-input>
                    </el-form-item>
                  </el-col> -->
                  <el-col :span="24">
                    <el-form-item label="方向" prop="direction">
                      <el-select
                        v-model="lineForm.direction"
                        placeholder="请选择方向"
                        style="width: 100%"
                        clearable
                      >
                        <el-option
                          v-for="dict in dict.type.bas_direction"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form-item>

              <el-form-item>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="起点桩号" prop="startMilepost">
                      <el-input v-model="lineForm.startMilepost"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="止点桩号" prop="endMilepost">
                      <el-input v-model="lineForm.endMilepost"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item>
                <el-row :gutter="24">
                  <el-col :span="24">
                    <el-form-item label="坐标点" prop="coordinates">
                      <el-input
                        type="textarea"
                        :disabled="true"
                        v-model="lineForm.coordinates"
                        rows="4"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item>
                <el-row :gutter="24">
                  <el-col :span="24">
                    <el-form-item label="备注" prop="remark">
                      <el-input v-model="lineForm.remarks"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-form>
          </div>

          <!-- 右侧地图部分 -->
          <div style="flex: 1; position: relative">
            <!-- <div id="add_map" ref="mapContainer" class="no-hand-cursor"></div> -->
            <fssm-map
              :showLine="true"
              @endEoordinateLine="getEndEoordinate"
              :editCoordinatesLine="editCoordinatesLine"
              class="no-hand-cursor"
              ref="mapContainer"
            ></fssm-map>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRoute,
  getRoute,
  delRoute,
  addRoute,
  updateRoute,
} from "@/api/xj/route";
// 地图
import FssmMap from "@/components/map/fssm-map.vue";
export default {
  name: "Route",
  dicts: ["bas_road_type", "bas_direction"],
  components: {
    FssmMap,
  },
  data() {
    return {
      menuOpen: false,
      markers: [],
      dialogVisible: false, // 控制dialog显示
      routeForm: {
        routeCode: "",
        routeName: "",
        routeFullName: "",
        direction: "",
        startStake: "",
        endStake: "",
        coordinates: "",
        remark: "",
      },
      lines: [], // 保存所有添加的连线
      map: null,
      markerLayer: null,
      previousPoint: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 道路线表格数据
      routeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        routeCode: null,
        routeName: null,
        fullName: null,
      },
      // 表单参数
      lineForm: {
        coordinates: "",
      },
      // 表单校验
      rules: {
        routeCode: [
          {
            required: true,
            message: "路线编码，唯一不能为空",
            trigger: "blur",
          },
        ],
        routeName: [
          { required: true, message: "路线名称不能为空", trigger: "blur" },
        ],
        direction: [
          { required: true, message: "方向不能为空", trigger: "change" },
        ],
        startMilepost: [
          { required: true, message: "起点桩号不能为空", trigger: "blur" },
        ],
        endMilepost: [
          { required: true, message: "止点桩号不能为空", trigger: "blur" },
        ],
        coordinates: [
          { required: true, message: "坐标点不能为空", trigger: "blur" },
        ],
      },
      // 编辑坐标点
      editCoordinatesLine: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * @description: 查询道路线列表
     * @return {*}
     */
    getList() {
      this.loading = true;
      listRoute(this.queryParams).then((response) => {
        this.routeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /**
     * @description: 获取线段坐标点
     * @param {coordinates}
     * @return {*}
     */
    getEndEoordinate(coordinates) {
      this.$set(this.lineForm, "coordinates", coordinates.join(";"));
    },

    /**
     * @description: 取消按钮
     * @return {*}
     */
    cancel() {
      this.dialogVisible = false;
      this.lineForm = {};
      this.editCoordinatesLine = [];
    },

    /**
     * @description: 搜索按钮操作
     * @return {*}
     */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /**
     * @description: 重置按钮操作
     * @return {*}
     */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /**
     * @description: 多选框选中数据
     * @param {selection}
     * @return {*}
     */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /**
     * @description: 新增按钮操作
     * @return {*}
     */
    handleAdd() {
      this.dialogVisible = true;
      this.title = "添加道路线";
    },

    /**
     * @description: 修改按钮操作
     * @param {row}
     * @return {*}
     */
    handleUpdate(row) {
      const id = row.id || this.ids;
      getRoute(id).then((response) => {
        this.lineForm = response.data;
        this.editCoordinatesLine = response.data.coordinates
          .split(";")
          .map((item) => {
            return item.split(",");
          });
        this.dialogVisible = true;
        this.title = "修改道路线";
      });
    },

    /**
     * @description: 提交按钮
     * @return {*}
     */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.lineForm.id != null) {
            updateRoute(this.lineForm).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.dialogVisible = false;
              this.getList();
            });
          } else {
            addRoute(this.lineForm).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.dialogVisible = false;
              this.getList();
            });
          }
        }
      });
    },

    /**
     * @description: 删除按钮操作
     * @param {row}
     * @return {*}
     */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除道路线编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRoute(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /**
     * @description: 导出按钮操作
     * @return {*}
     */
    handleExport() {
      this.download(
        "xj/route/export",
        {
          ...this.queryParams,
        },
        `route_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  width: 100%;
  height: calc(100vh - 5.3rem);
  padding: 1rem;
  overflow-y: auto;
}
.no-hand-cursor {
  cursor: default !important;
  width: 100%;
  height: 500px;
}
</style>
