<template>
  <ElDialog v-model="showDialog"
    :close-on-click-modal="false"
    destroy-on-close
    width="fit-content"
    @close="dialogCloseHandler">
    <ElForm :model="formData"
      :rules="rules"
      ref="transForm"
      class="form-panel"
      label-width="auto">
      <ElFormItem label="Id" prop="id">
        <ElInput
          v-model="formData.id"

          placeholder="Id"/>
      </ElFormItem>
      <ElFormItem label="车次" prop="trainNum">
        <ElInput
          v-model="formData.trainNum"

          placeholder="车次"/>
      </ElFormItem>
      <ElFormItem label="订单 Id" prop="orderId">
        <ElInput
          v-model="formData.orderId"

          placeholder="订单 Id"/>
      </ElFormItem>
      <ElFormItem label="车道名称" prop="lane">
        <ElInput
          v-model="formData.lane"

          placeholder="车道名称"/>
      </ElFormItem>
      <ElFormItem label="指派司机时间" prop="assignmentDriverTime">
        <ElInput
          v-model="formData.assignmentDriverTime"

          placeholder="指派司机时间"/>
      </ElFormItem>
      <ElFormItem label="司机确认接单时间" prop="driverConfirmTime">
        <ElInput
          v-model="formData.driverConfirmTime"

          placeholder="司机确认接单时间"/>
      </ElFormItem>
      <ElFormItem label="开始运输时间" prop="transTime">
        <ElInput
          v-model="formData.transTime"

          placeholder="开始运输时间"/>
      </ElFormItem>
      <ElFormItem label="完结时间" prop="finishTime">
        <ElInput
          v-model="formData.finishTime"

          placeholder="完结时间"/>
      </ElFormItem>
      <ElFormItem label="看料员 Id；sys_user.id" prop="checkerId">
        <ElInput
          v-model="formData.checkerId"

          placeholder="看料员 Id；sys_user.id"/>
      </ElFormItem>
      <ElFormItem label="勘料状态；字典代码：check_status，Wu-->无需勘料、YiKanLiao-->已勘料、WeiKanLiao-->未勘料" prop="checkStatus">
        <ElInput
          v-model="formData.checkStatus"

          placeholder="勘料状态；字典代码：check_status，Wu-->无需勘料、YiKanLiao-->已勘料、WeiKanLiao-->未勘料"/>
      </ElFormItem>
      <ElFormItem label="勘料时间" prop="checkTime">
        <ElInput
          v-model="formData.checkTime"

          placeholder="勘料时间"/>
      </ElFormItem>
      <ElFormItem label="勘料照片" prop="checkPhoto">
        <ElInput
          v-model="formData.checkPhoto"

          placeholder="勘料照片"/>
      </ElFormItem>
      <ElFormItem label="勘料员备注" prop="checkerMemo">
        <ElInput
          v-model="formData.checkerMemo"

          placeholder="勘料员备注"/>
      </ElFormItem>
      <ElFormItem label="司机 Id" prop="driverId">
        <ElInput
          v-model="formData.driverId"

          placeholder="司机 Id"/>
      </ElFormItem>
      <ElFormItem label="司机所属客户 Id；cst_customer.id" prop="driverCustomerId">
        <ElInput
          v-model="formData.driverCustomerId"

          placeholder="司机所属客户 Id；cst_customer.id"/>
      </ElFormItem>
      <ElFormItem label="司机所属用户 Id" prop="driverUserId">
        <ElInput
          v-model="formData.driverUserId"

          placeholder="司机所属用户 Id"/>
      </ElFormItem>
      <ElFormItem label="归属客户 Id；cst_customer.id" prop="truckCustomerId">
        <ElInput
          v-model="formData.truckCustomerId"

          placeholder="归属客户 Id；cst_customer.id"/>
      </ElFormItem>
      <ElFormItem label="归属企业" prop="truckOrgId">
        <ElInput
          v-model="formData.truckOrgId"

          placeholder="归属企业"/>
      </ElFormItem>
      <ElFormItem label="车辆 Id" prop="truckId">
        <ElInput
          v-model="formData.truckId"

          placeholder="车辆 Id"/>
      </ElFormItem>
      <ElFormItem label="车牌号" prop="truckLicensePlate">
        <ElInput
          v-model="formData.truckLicensePlate"

          placeholder="车牌号"/>
      </ElFormItem>
      <ElFormItem label="历史皮重" prop="historyTareWeight">
        <ElInput
          v-model="formData.historyTareWeight"

          placeholder="历史皮重"/>
      </ElFormItem>
      <ElFormItem label="毛重；单位：千克" prop="roughWeight">
        <ElInput
          v-model="formData.roughWeight"

          placeholder="毛重；单位：千克"/>
      </ElFormItem>
      <ElFormItem label="皮重；单位：千克" prop="tareWeight">
        <ElInput
          v-model="formData.tareWeight"

          placeholder="皮重；单位：千克"/>
      </ElFormItem>
      <ElFormItem label="净重；单位：千克" prop="settleWeight">
        <ElInput
          v-model="formData.settleWeight"

          placeholder="净重；单位：千克"/>
      </ElFormItem>
      <ElFormItem label="运距；单位：米" prop="transDistance">
        <ElInput
          v-model="formData.transDistance"

          placeholder="运距；单位：米"/>
      </ElFormItem>
      <ElFormItem label="进场车头照片" prop="inFrontPhoto">
        <ElInput
          v-model="formData.inFrontPhoto"

          placeholder="进场车头照片"/>
      </ElFormItem>
      <ElFormItem label="进场车斗照片" prop="inBodyPhoto">
        <ElInput
          v-model="formData.inBodyPhoto"

          placeholder="进场车斗照片"/>
      </ElFormItem>
      <ElFormItem label="出场车头照片" prop="outFrontPhoto">
        <ElInput
          v-model="formData.outFrontPhoto"

          placeholder="出场车头照片"/>
      </ElFormItem>
      <ElFormItem label="出场车斗照片" prop="outBodyPhoto">
        <ElInput
          v-model="formData.outBodyPhoto"

          placeholder="出场车斗照片"/>
      </ElFormItem>
      <ElFormItem label="进场时间" prop="inTime">
        <ElInput
          v-model="formData.inTime"

          placeholder="进场时间"/>
      </ElFormItem>
      <ElFormItem label="出场时间" prop="outTime">
        <ElInput
          v-model="formData.outTime"

          placeholder="出场时间"/>
      </ElFormItem>
      <ElFormItem label="支付状态，MianFei-->免费、WeiZhiFu-->未支付、YiZhiFu-->已支付、YiTuiKuan-->已退款" prop="paymentStatus">
        <ElInput
          v-model="formData.paymentStatus"

          placeholder="支付状态，MianFei-->免费、WeiZhiFu-->未支付、YiZhiFu-->已支付、YiTuiKuan-->已退款"/>
      </ElFormItem>
      <ElFormItem label="支付时间" prop="payTime">
        <ElInput
          v-model="formData.payTime"

          placeholder="支付时间"/>
      </ElFormItem>
      <ElFormItem label="退款时间" prop="refundTime">
        <ElInput
          v-model="formData.refundTime"

          placeholder="退款时间"/>
      </ElFormItem>
      <ElFormItem label="总金额；单位：元" prop="totalMoney">
        <ElInput
          v-model="formData.totalMoney"

          placeholder="总金额；单位：元"/>
      </ElFormItem>
      <ElFormItem label="优惠金额；单位：元，有正负" prop="discountMoney">
        <ElInput
          v-model="formData.discountMoney"

          placeholder="优惠金额；单位：元，有正负"/>
      </ElFormItem>
      <ElFormItem label="手动修正金额；单位：元，有正负" prop="reviseMoney">
        <ElInput
          v-model="formData.reviseMoney"

          placeholder="手动修正金额；单位：元，有正负"/>
      </ElFormItem>
      <ElFormItem label="结算金额；单位：元" prop="settleMoney">
        <ElInput
          v-model="formData.settleMoney"

          placeholder="结算金额；单位：元"/>
      </ElFormItem>
      <ElFormItem label="结算方式，YueJie-->月结、YuE-->余额、XianFu-->现付" prop="settlementWay">
        <ElInput
          v-model="formData.settlementWay"

          placeholder="结算方式，YueJie-->月结、YuE-->余额、XianFu-->现付"/>
      </ElFormItem>
      <ElFormItem label="付款人 Id；sys_user.id" prop="payerUserId">
        <ElInput
          v-model="formData.payerUserId"

          placeholder="付款人 Id；sys_user.id"/>
      </ElFormItem>
      <ElFormItem label="付款人客户 Id；cst_customer.id" prop="payerCustomerId">
        <ElInput
          v-model="formData.payerCustomerId"

          placeholder="付款人客户 Id；cst_customer.id"/>
      </ElFormItem>
      <ElFormItem label="付款方资金账户 Id" prop="payerMoneyAccount">
        <ElInput
          v-model="formData.payerMoneyAccount"

          placeholder="付款方资金账户 Id"/>
      </ElFormItem>
      <ElFormItem label="创建人 Id；sys_user.id" prop="creatorId">
        <ElInput
          v-model="formData.creatorId"

          placeholder="创建人 Id；sys_user.id"/>
      </ElFormItem>
      <ElFormItem label="修改人 Id； sys_user.id" prop="modifierId">
        <ElInput
          v-model="formData.modifierId"

          placeholder="修改人 Id； sys_user.id"/>
      </ElFormItem>
      <ElFormItem label="创建时间" prop="createTime">
        <ElInput
          v-model="formData.createTime"

          placeholder="创建时间"/>
      </ElFormItem>
      <ElFormItem label="修改时间" prop="modifyTime">
        <ElInput
          v-model="formData.modifyTime"

          placeholder="修改时间"/>
      </ElFormItem>
    </ElForm>
    <template #footer>
      <ElButton @click="showDialog = false">{{ status === 'view' ? '关闭' : '取消' }}</ElButton>
      <ElButton v-if="status !== 'view'" :loading="submiting" type="primary" @click="submitHandler">提交</ElButton>
    </template>
  </ElDialog>
</template>

<script lang="ts" setup>
import TransApi from '@/pages/order/trans-order/trans-api.ts'
import Strings from '@/common/utils/strings.ts'
import FormUtil from '@/common/utils/formUtil.ts'
import Utils from '@/common/utils'
import {
  ElMessage,
  type FormInstance,
  type FormRules,
} from 'element-plus'

const emits = defineEmits(['editSucc'])
const showDialog = ref(false)
const submiting = ref(false)
const status = ref<'add' | 'view' | 'modify'>('add')

const transFormIns = useTemplateRef<FormInstance>('transForm')

const formData = Utils.resetAble(reactive<TransTypes.SearchTransResult>({}))
const rules = reactive<FormRules<TransTypes.SearchTransResult>>({
id: [{ required: true, message: '请填写Id', trigger: 'blur' }],
trainNum: [{ required: true, message: '请填写车次', trigger: 'blur' }],
orderId: [{ required: true, message: '请填写订单 Id', trigger: 'blur' }],
transStatus: [{ required: true, message: '请填写运输状态；字典代码：trans_status，DaiPaiDan-->待派单、DaiJieDan-->待接单、YiJieDan-->已接单、YunShuZhong-->运输中、YiJinChang-->已进场、YiChuChang-->已出场、YiWanCheng-->已完成、YiQuXiao-->已取消', trigger: 'blur' }],
lane: [{ required: true, message: '请填写车道名称', trigger: 'blur' }],
assignmentDriverTime: [{ required: true, message: '请填写指派司机时间', trigger: 'blur' }],
driverConfirmTime: [{ required: true, message: '请填写司机确认接单时间', trigger: 'blur' }],
transTime: [{ required: true, message: '请填写开始运输时间', trigger: 'blur' }],
finishTime: [{ required: true, message: '请填写完结时间', trigger: 'blur' }],
checkerId: [{ required: true, message: '请填写看料员 Id；sys_user.id', trigger: 'blur' }],
checkStatus: [{ required: true, message: '请填写勘料状态；字典代码：check_status，Wu-->无需勘料、YiKanLiao-->已勘料、WeiKanLiao-->未勘料', trigger: 'blur' }],
checkTime: [{ required: true, message: '请填写勘料时间', trigger: 'blur' }],
checkPhoto: [{ required: true, message: '请填写勘料照片', trigger: 'blur' }],
checkerMemo: [{ required: true, message: '请填写勘料员备注', trigger: 'blur' }],
driverId: [{ required: true, message: '请填写司机 Id', trigger: 'blur' }],
driverCustomerId: [{ required: true, message: '请填写司机所属客户 Id；cst_customer.id', trigger: 'blur' }],
driverUserId: [{ required: true, message: '请填写司机所属用户 Id', trigger: 'blur' }],
truckCustomerId: [{ required: true, message: '请填写归属客户 Id；cst_customer.id', trigger: 'blur' }],
  truckOrgId: [ {required: true, message: '请填写归属企业', trigger: 'blur'} ],
truckId: [{ required: true, message: '请填写车辆 Id', trigger: 'blur' }],
truckLicensePlate: [{ required: true, message: '请填写车牌号', trigger: 'blur' }],
historyTareWeight: [{ required: true, message: '请填写历史皮重', trigger: 'blur' }],
roughWeight: [{ required: true, message: '请填写毛重；单位：千克', trigger: 'blur' }],
tareWeight: [{ required: true, message: '请填写皮重；单位：千克', trigger: 'blur' }],
settleWeight: [{ required: true, message: '请填写净重；单位：千克', trigger: 'blur' }],
transDistance: [{ required: true, message: '请填写运距；单位：米', trigger: 'blur' }],
inFrontPhoto: [{ required: true, message: '请填写进场车头照片', trigger: 'blur' }],
inBodyPhoto: [{ required: true, message: '请填写进场车斗照片', trigger: 'blur' }],
outFrontPhoto: [{ required: true, message: '请填写出场车头照片', trigger: 'blur' }],
outBodyPhoto: [{ required: true, message: '请填写出场车斗照片', trigger: 'blur' }],
inTime: [{ required: true, message: '请填写进场时间', trigger: 'blur' }],
outTime: [{ required: true, message: '请填写出场时间', trigger: 'blur' }],
paymentStatus: [{ required: true, message: '请填写支付状态，MianFei-->免费、WeiZhiFu-->未支付、YiZhiFu-->已支付、YiTuiKuan-->已退款', trigger: 'blur' }],
payTime: [{ required: true, message: '请填写支付时间', trigger: 'blur' }],
refundTime: [{ required: true, message: '请填写退款时间', trigger: 'blur' }],
totalMoney: [{ required: true, message: '请填写总金额；单位：元', trigger: 'blur' }],
discountMoney: [{ required: true, message: '请填写优惠金额；单位：元，有正负', trigger: 'blur' }],
reviseMoney: [{ required: true, message: '请填写手动修正金额；单位：元，有正负', trigger: 'blur' }],
settleMoney: [{ required: true, message: '请填写结算金额；单位：元', trigger: 'blur' }],
settlementWay: [{ required: true, message: '请填写结算方式，YueJie-->月结、YuE-->余额、XianFu-->现付', trigger: 'blur' }],
payerUserId: [{ required: true, message: '请填写付款人 Id；sys_user.id', trigger: 'blur' }],
payerCustomerId: [{ required: true, message: '请填写付款人客户 Id；cst_customer.id', trigger: 'blur' }],
payerMoneyAccount: [{ required: true, message: '请填写付款方资金账户 Id', trigger: 'blur' }],
creatorId: [{ required: true, message: '请填写创建人 Id；sys_user.id', trigger: 'blur' }],
modifierId: [{ required: true, message: '请填写修改人 Id； sys_user.id', trigger: 'blur' }],
createTime: [{ required: true, message: '请填写创建时间', trigger: 'blur' }],
modifyTime: [{ required: true, message: '请填写修改时间', trigger: 'blur' }],
deleted: [{ required: true, message: '请填写是否删除； 0-->未删除、1-->已删除', trigger: 'blur' }],
})

function dialogCloseHandler() {
  formData.$reset()
}

function submitHandler() {
  if (status.value === 'view') return
  submiting.value = true
  if (formData.id != null) {
  FormUtil.submit(transFormIns, () => TransApi.modify(formData))
      .then(() => {
        ElMessage.success('修改成功')
        emits('editSucc')
        showDialog.value = false
      })
      .finally(() => {
        submiting.value = false
      })
  } else {
  FormUtil.submit(transFormIns, () => TransApi.add(formData))
      .then(() => {
        ElMessage.success('添加成功')
        emits('editSucc')
        showDialog.value = false
      })
      .finally(() => {
        submiting.value = false
      })
  }
}

defineExpose({
  open(data: TransTypes.SearchTransResult = {}) {
    showDialog.value = true
    if (!Strings.isBlank(data.id)) {
      status.value = 'modify'
      TransApi.detail(data.id!)
        .then(res => {
          formData.$reset(res.data)
        })
    } else {
      status.value = 'add'
      formData.$reset(data)
    }
  }
})
</script>

<style lang="stylus" scoped>
.form-panel {
  padding 20px
}
</style>
