<template>
  <el-form ref="addOrUpdate" :rules="rules" label-width="130px" :model="form" label-position='top'>
    <el-row :gutter="20">
      <el-col :span='6'>
        <el-form-item label="订单名" prop="orderId">
          <el-select v-model="form.orderId" placeholder="请选择" style="width: 100%;" filterable>
            <el-option
              v-for="item in orderList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span='6'>
        <el-form-item label="发货单名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span='6'>
        <el-form-item label="发货时间" prop="deliveryTime">
          <el-date-picker
            v-model="form.deliveryTime"
            type="datetime"
            format='yyyy-MM-dd HH:mm:ss'
            value-format="timestamp"
            style="width: 100%;"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span='6'>
        <el-form-item label="发货单号" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span='6'>
        <el-form-item label="发货负责人" prop="deliveryPersonId">
          <el-select v-model="form.deliveryPersonId" placeholder="请选择" style="width: 100%;" filterable>
            <el-option
              v-for="item in personList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span='6'>
        <el-form-item label="运输负责人" prop="principal">
          <el-input v-model="form.principal"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span='6'>
        <el-form-item label="运输联系方式" prop="principalCall">
          <el-input v-model="form.principalCall"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span='6'>
        <el-form-item label="运输费用" prop="principalCost">
          <el-input-number v-model="form.principalCost" :min="0" :max='9999999999' :precision='2' style='width: 100%;'></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span='24'>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getWorkerList } from '@/api/base/worker'
import { deliveryLogCreate, deliveryLogUpdate, getDeliveryLog, listOrderList } from '@/api/base/delivery'
export default {
  name: 'AddOrUpdate',
  data() {
    return {
      form: {
        id: '',
        orderId: '',
        name: '',
        code: '',
        deliveryTime: null,
        deliveryPersonId: '',
        principal: '',
        principalCall: '',
        principalCost: 0.00,
        remark: ''
      },
      isEdit: false,
      rules: {
        orderId: [{ required: true, message: "订单名不能为空", trigger: "change" }],
        name: [{ required: true, message: "发货单名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "发货单号不能为空", trigger: "blur" }]
      },
      orderList: [],
      personList: []
    }
  },
  methods: {
    init(id) {
      this.getSelectList()
      if (id) {
        this.form.id = id
        this.isEdit = true
        getDeliveryLog({id}).then(res => {
          this.form = res.data || {}
        })
      }
    },
    getSelectList() {
      listOrderList().then(res => {
        this.orderList = res.data || []
      })
      getWorkerList().then(res => {
        this.personList = res.data || []
      })
    },
    submitForm() {
      this.$refs['addOrUpdate'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          if (this.isEdit) {
            //编辑
            deliveryLogUpdate({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            deliveryLogCreate({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.addOrUpdate.resetFields()
      this.form.principalCost = 0.00
      this.isEdit = false
    }
  }
}
</script>
