// 汽车调度
<template>
  <div class="container">
    <el-row>
      <el-col :span="20">
        <h2 class="bg-left title">汽车调度</h2>
      </el-col>
      <el-col :span="4" class="bg-right">
        <el-button
          class="bg-bottom"
          type="primary"
          size="small"
          @click="revert()"
          ><img
            width="6"
            height="10"
            style="vertical-align: bottom; margin-right: 3px"
            src="../../../public/img/lujing.png"
            alt=""
          />返回</el-button
        >
      </el-col>
    </el-row>
    <div class="center">
      <ws-form ref="deptBudgetList" :model="deptBudgetList" :rules="rules">
        <div class="basicInformation">
          <h2>任务编号</h2>
          <ws-info-table>
            <!-- 任务编号 -->
            <ws-form-item label="任务编号" span="1" prop="processNo">
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.processNo"
                placeholder="请输入任务编号"
                maxlength="20"
                size="small"
              />
            </ws-form-item>
            <!-- 货名 -->
            <ws-form-item label="货名" span="1">
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.goodsName"
                placeholder="请输入货名"
                maxlength="20"
                size="small"
              />
            </ws-form-item>
            <!--重量（吨）-->
            <ws-form-item
              label="重量（吨）"
              span="1"
              prop="tare"
              class="readonly"
            >
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.weight"
                placeholder="请输入重重"
                maxlength="100"
                size="small"
              />
            </ws-form-item>

            <!--发货地址-->
            <ws-form-item label="发货地址" span="1" prop="netWeight">
              {{ deptBudgetList.sendPrivate }}{{ deptBudgetList.sendCity
              }}{{ deptBudgetList.sendArea
              }}{{ deptBudgetList.sendDetailedAddress }}
            </ws-form-item>
            <!--发货人-->
            <ws-form-item label="发货人" span="1" prop="agent">
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.sender"
                placeholder="请输入发货人姓名"
                maxlength="100"
                size="small"
              />
            </ws-form-item>

            <!--发货人电话-->
            <ws-form-item label="发货人电话" span="1" prop="agent">
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.senderPhone"
                placeholder="请输入发货人电话"
                maxlength="100"
                size="small"
              />
            </ws-form-item>
            <!--收货地址-->
            <ws-form-item label="收货地址" span="1" prop="netWeight">
              {{ deptBudgetList.receivePrivate }}{{ deptBudgetList.receiveCity
              }}{{ deptBudgetList.receiveArea
              }}{{ deptBudgetList.receiveDetailedAddress }}
            </ws-form-item>
            <!--收货人-->
            <ws-form-item label="收货人" span="1" prop="receiver">
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.receiver"
                placeholder="请输入收货人"
                maxlength="100"
                size="small"
              />
            </ws-form-item>

            <!--收货人电话-->
            <ws-form-item label="收货人电话" span="1" prop="receiverPhone">
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.receiverPhone"
                placeholder="请输入收货人电话"
                maxlength="100"
                size="small"
              />
            </ws-form-item>
            <!--发货日期-->
            <ws-form-item
              label="发货日期"
              span="1"
              prop="inOutDate"
              class="deliverydate"
            >
              {{ deptBudgetList.deliveryDateStart }}
            </ws-form-item>
            <!--最晚发货日期-->
            <ws-form-item
              label="最晚发货日期"
              span="1"
              prop="inOutDate"
              class="deliverydate"
            >
              {{ deptBudgetList.deliveryDateEnd }}
            </ws-form-item>
            <!--合同编号-->
            <ws-form-item label="合同编号" span="1" prop="contractNo">
              <ws-input
                :disabled="readonly"
                v-model="deptBudgetList.contractNo"
                placeholder="请输入合同编号"
                maxlength="100"
                size="small"
              />
            </ws-form-item>
          </ws-info-table>

          <div class="small-title">派车</div>

          <div
            class="driver position"
            v-for="(item, index) in deptBudgetList.tranCarInfoList"
          >
            <ws-info-table>
              <!--司机-->
              <ws-form-item
                :label="'司机-' + (index + 1)"
                span="1"
                prop="driverName"
                class="siji"
              >
                <el-select
                  :disabled="item.disabled"
                  v-model="item.driver"
                  placeholder="请选择司机"
                  class="typeselect"
                  @change="
                    (val) => {
                      carchange(val, index)
                    }
                  "
                >
                  <el-option
                    v-for="(items, index) in carList"
                    :key="index"
                    :label="items.driverName"
                    :value="items.driverName"
                  />
                </el-select>
              </ws-form-item>
              <!--司机电话-->
              <ws-form-item
                label="司机电话"
                span="1"
                prop="impurity"
                class="siji"
              >
                <el-select
                  :disabled="item.disabled"
                  v-model="item.driverPhone"
                  placeholder="请选择司机"
                  class="typeselect"
                  @change="
                    (val) => {
                      phonechange(val, index)
                    }
                  "
                >
                  <el-option
                    v-for="(items, index) in carList"
                    :key="index"
                    :label="items.driverPhone"
                    :value="items.driverPhone"
                  />
                </el-select>
              </ws-form-item>
              <!--车牌号-->
              <ws-form-item label="车牌号" span="1" prop="carNo" class="siji">
                <ws-input
                  :disabled="readonly"
                  v-model="item.carNo"
                  placeholder=""
                  maxlength="120"
                  size="small"
                />
              </ws-form-item>
              <!--车型-->
              <ws-form-item label="车型" span="1" prop="carModel" class="siji">
                <ws-input
                  :disabled="readonly"
                  v-model="item.carModel"
                  placeholder="请输入车型"
                  maxlength="120"
                  size="small"
                />
              </ws-form-item>
              <!--车长-->
              <ws-form-item label="车长" span="1" prop="carLength" class="siji">
                <ws-input
                  :disabled="readonly"
                  v-model="item.carLength"
                  placeholder="请输入车长"
                  maxlength="120"
                  size="small"
                />
              </ws-form-item>
              <!--载重吨-->
              <ws-form-item
                label="载重（吨）"
                span="1"
                prop="loadWeight"
                class="siji zaizhong"
              >
                <ws-input
                  :disabled="readonly"
                  v-model="item.loadWeight"
                  placeholder="请输入载重（吨）"
                  maxlength="120"
                  size="small"
                />
              </ws-form-item>
            </ws-info-table>
            <img
              width="22"
              height="22"
              class="del"
              @click="del(index)"
              src="../../../public/img/del.png"
              alt=""
            />
          </div>
          <div style="text-align: right">
            合计{{ total }}/{{ deptBudgetList.weight }}
          </div>
          <el-button class="add" type="primary" size="small" @click="add()"
            >添加司机</el-button
          >
          <div style="text-align: right; padding: 10px">
            <el-button
              class="bg-bottom-up"
              type="primary"
              size="small"
              @click="submit()"
              >提交</el-button
            >
          </div>
        </div>
      </ws-form>
    </div>
  </div>
</template>
<script>
import { pullDown, addstorageputList } from '@/model/warehouse/index'
import Pagination from '@/components/Pagination'
import WsUpload from '@/components/WsUpload'
import {
  seeCat,
  nameXiala,
  arrange,
  dispatchCat,
} from '@/model/transport/index'
// import { dayjs, fmoney, EventBus } from 'base-core-lib'
import { dayjs, EventBus } from 'base-core-lib'
export default {
  name: 'viewSpareMoney',
  components: {
    WsUpload,
    Pagination,
  },
  watch: {
    // vesselId(val) {
    //   this.getList()
    // },
    isShow(val) {
      this.showType = val
    },
  },
  data() {
    return {
      // 是否显示
      showType: true,
      deptBudgetTotal: 0,
      currentPage: 1,
      pageSize: 10,
      searchType: 1,
      startDate: null,
      endDate: null,
      carList: [],
      // 提交类型
      readonly: true,
      appendixIdsAdd: '',
      onChange: {},
      gradeList: [],
      rules: {
        netWeight: [
          {
            required: true,
            type: 'number',
            message: '请输入活动名称',
            trigger: 'blur',
          },
        ],
      },
      size: 10,
      compId: sessionStorage.getItem('ws-pf_compId'),
      deptCircularPage: {},
      packtypeList: {},
      date: {
        year: dayjs().format('YYYY'),
        month: dayjs().format('MM'),
      },
      contractList: [],
      deptBudgetList: {
        tranCarInfoList: [],
      },
      historyList: [],
      pickerBeginDateBefore: {
        disabledDate: (time) => {
          return time.getTime() > Date.now()
        },
      },
      accessoryTFs: false,
    }
  },
  activated() {
    this.getList()
  },
  computed: {
    total: function () {
      var maxStorage = 0
      for (var i = 0; i < this.deptBudgetList.tranCarInfoList.length; i++) {
        maxStorage += Number(this.deptBudgetList.tranCarInfoList[i].loadWeight)
      }
      return maxStorage
    },
  },
  methods: {
    //返回按钮
    revert() {
      this.$router.push({ path: 'tranManagementVehicle' })
    },
    del(index) {
      if (this.freightspace.length > 1) {
        this.freightspace.splice(index, 1)
      }
    },
    add() {
      this.deptBudgetList.tranCarInfoList.push({
        driverName: '',
        driverPhone: '',
        carLength: 0,
        carLengthKey: '',
        loadWeight: null,
        carModel: '',
        carModelKey: '',
        carNumber: '',
        disabled: false,
        tranType: '1',
      })
    },
    selectwaterContent() {},
    //提交按钮
    submit() {
      this.$confirm(`提交成功后，任务将下发给司机，是否确定提交？`, {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning',
      })
        .then(() => {
          this.$refs.deptBudgetList.validate((valid) => {
            if (valid) {
              var tranCarInfo = {}
              tranCarInfo.id = this.deptBudgetList.id
              tranCarInfo.infoId = this.deptBudgetList.infoId
              tranCarInfo.tranCarInfoList = this.deptBudgetList.tranCarInfoList
              dispatchCat(tranCarInfo)
                .toPromise()
                .then((response) => {
                  this.$message.success('派车成功')
                  this.$router.go(-1)
                })
            } else {
              EventBus.$emit('error', this.$t('showMessage.asteriskRequired'))
              return false
            }
          })
        })
        .catch(() => {
          return false
        })
    },

    handleClose() {
      this.accessoryTFs = false
    },

    getList() {
      seeCat({ id: this.$route.query.id })
        .toPromise()
        .then((response) => {
          if (response.tranCarInfoList.length > 0) {
            for (var i = 0; i < response.tranCarInfoList.length; i++) {
              if (response.tranCarInfoList[i].id) {
                response.tranCarInfoList[i].disabled = true
              }
            }
          }
          this.deptBudgetList = response
          console.log(this.deptBudgetList)
          if (this.deptBudgetList.tranCarInfoList.length == 0) {
            this.deptBudgetList.tranCarInfoList = [
              {
                driverName: '',
                driverPhone: '',
                carLength: 0,
                carLengthKey: '',
                loadWeight: null,
                carModel: '',
                carModelKey: '',
                carNumber: '',
                disabled: false,
                tranType: '1',
              },
            ]
          }
        })
      nameXiala({ compId: this.compId })
        .toPromise()
        .then((response) => {
          this.carList = response
        })
    },
    carchange(val, index) {
      for (var i = 0; i < this.carList.length; i++) {
        if (this.carList[i].driverName == val) {
          this.deptBudgetList.tranCarInfoList[index].driverPhone =
            this.carList[i].driverPhone
          arrange({ id: this.carList[i].id })
            .toPromise()
            .then((response) => {
              console.log(this, response)
              this.deptBudgetList.tranCarInfoList[index].carNo =
                response.carNumber
              if (response.carLoad != null) {
                this.deptBudgetList.tranCarInfoList[index].loadWeight =
                  response.carLoad
              } else {
                this.deptBudgetList.tranCarInfoList[index].loadWeight = 0
              }
              this.deptBudgetList.tranCarInfoList[index].carModel =
                response.carModel
              this.deptBudgetList.tranCarInfoList[index].carLength =
                response.carLength
              this.deptBudgetList.tranCarInfoList[index].carLengthKey =
                response.carLengthKey
              this.deptBudgetList.tranCarInfoList[index].carModelKey =
                response.carModelKey
            })
        }
      }
    },
    phonechange(val, index) {
      for (var i = 0; i < this.carList.length; i++) {
        if (this.carList[i].driverPhone == val) {
          this.deptBudgetList.tranCarInfoList[index].driver =
            this.carList[i].driver
          arrange({ id: this.carList[i].id })
            .toPromise()
            .then((response) => {
              console.log(this, response)
              this.deptBudgetList.tranCarInfoList[index].carNo =
                response.carNumber
              if (response.carLoad != null) {
                this.deptBudgetList.tranCarInfoList[index].loadWeight =
                  response.carLoad
              } else {
                this.deptBudgetList.tranCarInfoList[index].loadWeight = 0
              }
              this.deptBudgetList.tranCarInfoList[index].carModel =
                response.carModel
              this.deptBudgetList.tranCarInfoList[index].carLength =
                response.carLength
              this.deptBudgetList.tranCarInfoList[index].carLengthKey =
                response.carLengthKey
              this.deptBudgetList.tranCarInfoList[index].carModelKey =
                response.carModelKey
            })
        }
      }
    },
    selecttaskType(e) {
      for (var i = 0; i < this.taskTypeList.length; i++) {
        if (this.taskTypeList[i].value == e) {
          this.searchType = this.taskTypeList[i].type
        }
      }
    },
    // fujian(row) {
    //   if (
    //     row.receiveAttachmentPath === null ||
    //     row.receiveAttachmentPath === ''
    //   ) {
    //     EventBus.$emit(
    //       'warning',
    //       this.$t('system.noticeCircular.NoInformation')
    //     )
    //   } else {
    //     this.accessoryTFs = true
    //   }
    //   this.appendixIdss = row.receiveAttachmentPath
    // },
    approve() {},
    listQuery() {},
  },
}
</script>
<style lang="scss" scoped>
/deep/.basicInformation {
  .ws-info-table {
    border: none;
  }
  .el-form-item {
    width: 33.3333%;
    border: none;
    .el-form-item__label {
      background: transparent;
      border: none;
    }
    .el-form-item__content {
      border: none;
    }
  }
}
.title {
  position: relative;
  padding-left: 10px;
}
.title::before {
  content: '';
  display: inline-block;
  width: 5px;
  height: 30px;
  background: #5473e8;
  position: absolute;
  left: 0;
}
.el-form {
  padding: 0 10%;
}
.el-button--primary {
  background-color: #5878e8;
  border-color: #5878e8;
}
.el-col {
  background: #f6f7fc;
}
.bg-right {
  text-align: right;
  padding: 16px 20px;
}
.center {
  width: 80%;
  margin: 0 auto;
}

//表格文字
/deep/.ws-info-table .el-form-item .el-form-item__label {
  text-align: left;
  font-size: 14px;
  font-weight: 400;
  color: #8890b1;
  line-height: 16px;
}
//蓝标
.small-title {
  position: relative;
  padding: 10px;
  font-weight: 600;
}
.small-title::before {
  position: absolute;
  content: '';
  display: block;
  background: #5473e8;
  width: 4px;
  height: 14px;
  left: 0px;
  top: 13px;
  padding: 4px 2px;
}
.position {
  position: relative;
}
.position .zaizhong {
  width: 16%;
}
//减号
.del {
  position: absolute;
  right: -28px;
  top: 29%;
}
.driver .el-form-item {
  width: 16.63%;
}
/deep/.driver .ws-info-table .el-form-item .el-form-item__label {
  width: 72px;
}
</style>
