<template>
  <!-- 位置作业页面 -->
  <div class="homeworkPath">
    <div class="top">
      <el-input  class="el-input" placeholder="请输入物资名称和外轮船名" v-model="input" clearable> </el-input>
       <!-- <el-date-picker v-model="startTime" type="datetime" placeholder="选择日期"></el-date-picker>
      <span>至</span>
      <el-date-picker v-model="endTime" type="datetime" placeholder="选择日期"></el-date-picker> -->
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button type="primary" @click="refresh()">
        <i class="el-icon-refresh"></i>刷新
      </el-button>
    </div>
    <el-table :data="selectionList" border highlight-current-row fit>
      <el-table-column type="index" width="50" label="序号" align="center">
      </el-table-column>
      <el-table-column
        v-for="(item, i) in tableTop"
        :key="i"
        :prop="item.prop"
        :label="item.label"
        align="center"
        :width="item.width"
        show-overflow-tooltip
      >
        <template slot="scope" v-if="item.label !== '船舶位置'">
          <span>{{ item.label }}</span>
        </template>
        <template slot="scope" v-if="item.label !== '位置状态'">
          <span>{{ item.label }}</span>
        </template>
        <!-- 插入输入框 -->
        <template slot-scope="scope">
          <template v-if="item.slot">
            <!-- 船舶位置 -->
            <template v-if="item.prop == 'shipLocation'">
              <el-input
                v-model.number="scope.row.shipLocation"
                style="width: 100px"
                @blur="onblur(scope)"
              ></el-input>
            </template>
            <!-- 位置状态 -->
            <template v-if="item.prop == 'locationStatus'">
              <el-select
                v-model.number="scope.row.locationStatus"
                @change="click(scope.row)"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, i) in options"
                  :key="i"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </template>
           
          </template>

          <template v-else>
            <span>{{ scope.row[item.prop] }}</span>
          </template>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
import { sjTime } from "@/utils/sharedJsFile";
import { getCookie } from "@/utils/util.js";
export default {
  name: "homeworkPath",
  data() {
    return {
      options: [
        {
          label: "未到闸船舶",
          value: "未到闸船舶",
        },
        {
          label: "等闸船舶",
          value: "等闸船舶",
        },
        {
          label: "已过闸船舶",
          value: "已过闸船舶",
        },
        {
          label: "到港待卸船舶",
          value: "到港待卸船舶",
        },
      ],
      value: null,
      // 车辆表格表头
      tableTop: [
        {
          prop: "foreignShipName",
          label: "外轮船名",
          width: "140",
        },
        {
          prop: "materialName",
          label: "物资名称",
          width: "140",
        },
        {
          prop: "capacityName",
          label: "江船船名",
          width: "140",
        },
        {
          prop: "locationRouteTime",
          label: "离港日期",
          width: "100",
        },
        {
          prop: "startPortName",
          label: "起运港",
          width: "140",
        },
        {
          prop: "shipLocation",
          label: "船舶位置",
          width: "130",
          slot: true,
        },
        {
          prop: "locationStatus",
          label: "位置状态",
          width: "180",
          slot: true,
        },

        {
          prop: "arrivePortName",
          label: "到达港",
          width: "140",
        },
        {
          prop: "planWeight",
          label: "计划吨",
          width: "100",
        },
        {
          prop: "realWeight",
          label: "实际吨",
          width: "100",
        },
        {
          prop: "locationMemo",
          label: "备注",
        },
      ],
      selectionList: [],
       startTime: null,
      endTime: null,
      input:"",
      
    };
  },
  created() {
    this.initialization();
  },
  methods: {
    onblur(scope) {
      this.axios
        .post(
          "/api/v1/tms/getShipLocationStatus?shipLocation=" +
            scope.row.shipLocation
        )
        .then((res) => {
          console.log("locationStatus：",res.data.data);
          try {
            scope.row.locationStatus = res.data.data.locationStatus;
          } catch (error) {
            
          }
          this.click(scope.row);
        });
    },
    initialization() {
      this.axios.post("/api/v1/tms/getShipLocationList").then((res) => {
        if (res.data.code == "200") {
          this.selectionList = res.data.data;
        }
      });
    },
    onclick() {
     let startTime = null;
      let endTime = null;
      if (this.startTime) {
        startTime = sjTime(this.startTime);
      }
      if (this.endTime) {
        endTime = sjTime(this.endTime);
      }
      if(startTime && endTime && startTime < endTime){
         this.axios.post("/api/v1/tms/getShipLocationList?con=" + this.input+
                "&startTime=" +
                startTime +
                "&endTime=" +
                endTime).then((res) => {
            if (res.data.code == "200") {
              this.selectionList = res.data.data;
            }
          });
      }else{
         this.axios.post("/api/v1/tms/getShipLocationList?con=" + this.input).then((res) => {
            if (res.data.code == "200") {
              this.selectionList = res.data.data;
            }
          });
      }
     
    },
    click(row) {
      this.axios.post("/api/v1/tms/updateShipLocation", row).then((res) => {
        if (res.data.code == "200") {
          this.$message({
            type: "success",
            message: "保存成功!",
          });
          this.initialization();
        }
      });
    },
    refresh() {
      this.$router.go(0);
    },
  },
};
</script>
<style lang="scss">
.homeworkPath {
  .top {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 40rpx;
    }
    .btn {
      width: 5.5%;
      margin-left: 0.25rem;
    }
  }
}
</style>
