<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    ref="workView"
  >
    <el-form :model="formData" ref="formData" :rules="rules">
      <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>
          <el-form-item label="请选择时间段" prop="changeTime">
            <el-radio-group v-model="formData.changeTime" @change="radioChange">
              <el-radio :label="1">最近2小时</el-radio>
              <el-radio :label="2">最近24小时</el-radio>
              <el-radio :label="3">自定义时间段</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="11">
          <el-form-item label="选择开始时间:" prop="beginTime">
            <el-date-picker
              :disabled="formData.changeTime != 3"
              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
              :disabled="formData.changeTime != 3"
              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,
        changeTime: 1,
        beginTime: "",
        endTime: "",
      },
      changeTime: null,
      workerManArr: [],

      rules: {
        userId: [
          { required: true, message: "请选择值班人员", trigger: "blur" },
        ],
        changeTime: [
          {
            required: true,
            message: "选择查询时间",
            trigger: ["blur", "change"],
          },
        ],
        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.nickName,
      value: item.userId,
    }));
    this.formData.beginTime = moment().format("YYYY-MM-DD HH:mm:ss");
    this.formData.endTime = moment().add(2, "h").format("YYYY-MM-DD HH:mm:ss");
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        document.body.removeChild(this.$refs.workView.$el);
      }
    },
  },
  methods: {
    // 下拉框选项
    selectChange(e) {
      console.log(e);
      this.formData.userId = e;
    },
    radioChange(e) {
      if (e == 1) {
        this.formData.beginTime = moment().format("YYYY-MM-DD HH:mm:ss");
        this.formData.endTime = moment()
          .add(2, "h")
          .format("YYYY-MM-DD HH:mm:ss");
      } else if (e == 2) {
        this.formData.beginTime = moment().format("YYYY-MM-DD HH:mm:ss");
        this.formData.endTime = moment()
          .add(1, "d")
          .format("YYYY-MM-DD HH:mm:ss");
      } else {
        this.formData.beginTime = "";
        this.formData.endTime = "";
      }
    },
    ok() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          if (
            moment(this.formData.beginTime).valueOf() >
            moment(this.formData.endTime).valueOf()
          ) {
            this.msgError("开始时间不能大于结束时间");
            this.formData.endTime = "";
            return;
          }

          this.okLoading = true;
          if (this.formData.changeTime != 3) {
            this.radioChange(this.formData.changeTime);
          } else {
            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>

