<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
  >
    <el-form :model="formData" ref="formData" :rules="rules">
      <!-- <el-form-item label="选择时间段:" prop=""> -->
      <!-- <el-date-picker
          v-model="dateValue"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker> -->
      <!-- </el-form-item> -->
      <el-row>
        <el-col :span="11">
          <el-form-item label="选择值班人员:" prop="userId">
            <el-select
              style="width: 220px"
              v-model="formData.userId"
              filterable
              placeholder="请选择"
              @change="selectChange"
            >
              <el-option
                v-for="item in workerManArr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="11">
          <el-form-item label="选择开始时间:" prop="beginTime">
            <el-date-picker
              v-model="formData.beginTime"
              type="datetime"
              placeholder="选择开始时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="选择结束时间:" prop="endTime">
            <el-date-picker
              v-model="formData.endTime"
              type="datetime"
              placeholder="选择结束时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button :loading="okLoading" type="primary" @click="ok"
        >确 定
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import moment from "moment";
import { getInspectorLocations } from "@/api/inspectorLocation/location";

export default {
  props: {
    userId: {
      type: Number,
    },
    title: {
      type: String,
    },
    target: {
      type: Object,
    },
    gaodeMap: {
      type: Object,
    },
  },

  components: {},
  data() {
    return {
      dialogVisible: false,
      dateValue: "",
      okLoading: false,
      formData: {
        userId: null,
      },
      workerManArr: [],

      rules: {
        userId: [
          { required: true, message: "请选择值班人员", trigger: "blur" },
        ],
        beginTime: [
          { required: true, message: "请输入开始时间", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "请输入结束时间", trigger: "blur" },
        ],
      },
    };
  },

  created() {
    this.formData.userId = this.userId;
    this.workerManArr = this.gaodeMap.workerManArr.map((item) => ({
      label: item.userName,
      value: item.userId,
    }));
  },

  methods: {
    // 下拉框选项
    selectChange(e) {
      console.log(e);
      this.formData.userId = e;
    },
    ok() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.okLoading = true;
          this.formData.beginTime = moment(this.formData.beginTime).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          this.formData.endTime = moment(this.formData.endTime).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          console.log(this.formData);

          // 找到哪个值班人员
          const target = this.gaodeMap.workerManMarkArr.filter((item) => {
            // console.log("userId",item.getExtData().userId)
            return item.getExtData().userId == this.formData.userId;
          })[0];
          console.log("target.moveMarker", target.moveMarker);
          getInspectorLocations(this.formData).then((res) => {
            if (res.code == 200) {
              // 如果这个时间段没有移动轨迹
              if (res.data.length < 1) {
                this.$message({
                  type: "warn",
                  // center:true,
                  offset: 100,
                  message: "该时间段无移动轨迹",
                });
                this.okLoading = false;
                return;
              }
              let arr = res.data.map((res) => {
                return [res.longitude, res.latitude];
              });
              arr = arr.sort((a, b) => {
                return -1;
              });
              let data = res.data.sort((a, b) => {
                return -1;
              });
              this.okLoading = false;
              this.$message({
                type: "success",
                // center:true,
                offset: 100,
                message: res.msg,
              });
              this.dialogVisible = false;
              this.gaodeMap.trackBack(target, arr,data)
            }
          });
        }
      });
    },
    async requeset(id, data) {
      id ? console.log("修改") : console.log("新增");
      return id ? updatePipe(data) : addPipe(data);
    },
    show() {
      this.dialogVisible = true;
    },
    handleClose(done) {
      done();
    },
  },
};
</script>

<style lang="scss">
</style>

