<!--
 * @Author: 快乐橙 1760016317@qq.com
 * @Date: 2025-04-20 19:12:10
 * @LastEditors: 快乐橙 1760016317@qq.com
 * @LastEditTime: 2025-04-20 23:04:01
 * @FilePath: \tmerclub-multishop\src\views\modules\product\recycle\order-page\review-dialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <el-dialog
      v-model="visible"
      title="详情"
      width="900"
      @close="resetFormData"
    >
      <div class="container">
        <el-descriptions
          :column="2"
          border
        >
          <el-descriptions-item label="商品名称">
            {{ orderInfo.productName || '无' }}
          </el-descriptions-item>
          <el-descriptions-item label="商品图片">
            <el-image
              v-if="orderInfo.productImages"
              :src="orderInfo.productImageUrl"
              :preview-src-list="[orderInfo.productImageUrl]"
              style="width: 100px; height: 100px"
            />
            <span v-else>无</span>
          </el-descriptions-item>
          <el-descriptions-item label="品牌">
            {{ orderInfo.brandName || '无' }}
          </el-descriptions-item>
          <el-descriptions-item label="类型">
            {{ orderInfo.typeName || '无' }}
          </el-descriptions-item>
          <el-descriptions-item label="用户手机号">
            {{ orderInfo.userPhone || '无' }}
          </el-descriptions-item>
          <el-descriptions-item label="收货物流单号">
            {{ orderInfo.sendNumber || '无' }}
          </el-descriptions-item>
          <el-descriptions-item label="退货物流单号">
            {{ orderInfo.returnTrackingNumber || '无' }}
          </el-descriptions-item>
          <el-descriptions-item label="订单类型">
            {{ orderInfo.cueOrderType === 1 ? '回收' : orderInfo.cueOrderType === 2 ?
              '置换' : '寄售' }}
          </el-descriptions-item>
          <el-descriptions-item
            label="缺陷"
            :span="2"
          >
            <div style="display: flex; flex-wrap: wrap; align-items: center; gap: 10px;">
              <div
                v-for="(item, index) in orderInfo.flawList"
                :key="index"
              >
                {{ item.flawName }} ({{ item.isFlaw === 1 ? '是' : '否' }})
              </div>
              <span v-if="!orderInfo.flawList || orderInfo.flawList.length === 0">无</span>
            </div>
          </el-descriptions-item>
          <el-descriptions-item
            label="缺陷图片"
            :span="2"
          >
            <div style="display: flex; flex-wrap: wrap; align-items: center; gap: 10px;">
              <el-image
                v-for="(image, index) in imageList.slice(0, 8)"
                :key="index"
                :src="image"
                :alt="image"
                :preview-src-list="imageList"
                style="width: 100px; height: 100px"
              />

              <span v-if="!imageList || imageList.length === 0">无</span>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button
            v-if="orderInfo.orderStatus === 10"
            type="primary"
            @click="formVisible2 = true"
          >审核</el-button>
          <el-button
            v-if="orderInfo.orderStatus === 14 || orderInfo.orderStatus == 13"
            type="primary"
            @click="formVisible = true"
          >评估</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
      v-model="formVisible"
      title="评估表单"
      width="600"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item
          label="实际金额:"
          prop="actualAmount"
        >
          <el-input
            v-model="formData.actualAmount"
            type="number"
          />
        </el-form-item>
        <el-form-item
          label="代金券金额:"
          prop="voucherAmount"
        >
          <el-input
            v-model="formData.voucherAmount"
            type="number"
          />
        </el-form-item>
        <el-form-item
          label="佣金:"
          prop="brokerage"
        >
          <el-input
            v-model="formData.brokerage"
            type="number"
          />
        </el-form-item>
        <el-form-item
          label="服务费:"
          prop="serviceCharge"
        >
          <el-input
            v-model="formData.serviceCharge"
            type="number"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="updateEvaluateStatus"
          >
            提交
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
      v-model="formVisible2"
      title="预估"
      role="rules2"
      width="600"
    >
      <el-form
        ref="formRef2"
        :model="formData2"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item
          label="预估金额:"
          prop="estimatedAmount"
        >
          <el-input
            v-model="formData2.estimatedAmount"
            type="number"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading2"
            type="primary"
            @click="updateStatus(11)"
          >
            提交
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script setup>

import { getRecycleOrderDetail, updateRecycleOrderStatus, updateRecycleOrderEvaluateStatus } from '@/api/product/recycle'
const visible = ref(false)
const formVisible = ref(false)
const formVisible2 = ref(false)
const formRef = ref(null)
const formRef2 = ref(null)
const orderInfo = ref({})
const imageList = ref([])
const loading = ref(false)
const loading2 = ref(false)
const formData = ref({
  actualAmount: 0,
  voucherAmount: 0,
  brokerage: 0,
  serviceCharge: 0
})
const formData2 = ref({
  estimatedAmount: 0
})
const rules = {
  actualAmount: [
    { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的数字格式', trigger: 'blur' }
  ],
  voucherAmount: [
    { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的数字格式', trigger: 'blur' }
  ],
  brokerage: [
    { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的数字格式', trigger: 'blur' }
  ],
  serviceCharge: [
    { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的数字格式', trigger: 'blur' }
  ]
}
const rules2 = {
  estimatedAmount: [
    { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的数字格式', trigger: 'blur' }
  ]

}
const init = (orderId) => {
  console.log('init')
  visible.value = true
  getRecycleOrderDetail({
    orderId
  }).then(res => {
    console.log(res)
    orderInfo.value = {
      ...res,
      productImageUrl: res.productImages ? import.meta.env.VITE_APP_RESOURCES_URL + '/' + res.productImages : null
    }
    imageList.value = res.flawImgUrl.split(',').map(v => {
      return import.meta.env.VITE_APP_RESOURCES_URL + '/' + v
    })
    console.log(orderInfo.value.flawList)
  })
}
const updateStatus = (status) => {
  loading2.value = true
  formRef2.value.validate((valid) => {
    if (valid) {
      updateRecycleOrderStatus({
        orderId: orderInfo.value.orderId,
        orderStatus: status,
        userId: orderInfo.value.userId,
        mallOrderId: orderInfo.value.mallOrderId,
        estimatedAmount: formData2.value.estimatedAmount
      }).then(res => {
        visible.value = false
        formVisible2.value = false
        ElMessage.success('操作成功')
      }).finally(() => {
        loading2.value = false
      })
    } else {
      loading2.value = false
      return false
    }
  })
}
// {
//     "orderId": 22,
//     "orderStatus": 5,
//     "userId":5001,
//     "mallOrderId":2504204005002001,
//     "actualAmount": 100,
//     "voucherAmount": 150,
//     "brokerage": null,
//     "serviceCharge":null,
//     "cueOrderType":1
// }
const resetFormData = () => {
  formData.value = {
    actualAmount: 0,
    voucherAmount: 0,
    brokerage: 0,
    serviceCharge: 0
  }
  formData2.value.estimatedAmount = 0
}

const updateEvaluateStatus = () => {
  loading.value = true
  formRef.value.validate((valid) => {
    if (valid) {
      updateRecycleOrderEvaluateStatus({
        orderId: orderInfo.value.orderId,
        orderStatus: 15,
        userId: orderInfo.value.userId,
        mallOrderId: orderInfo.value.mallOrderId,
        cueOrderType: orderInfo.value.cueOrderType,
        actualAmount: formData.value.actualAmount,
        voucherAmount: formData.value.voucherAmount,
        brokerage: formData.value.brokerage,
        serviceCharge: formData.value.serviceCharge
      }).then(res => {
        visible.value = false
        formVisible.value = false
        ElMessage.success('操作成功')
      }).finally(() => {
        loading.value = false
      })
    } else {
      loading.value = false
      return false
    }
  })
}
defineExpose({
  init
})
</script>
<style scoped>
.image-row {
  width: 100%;
  margin-top: 20px;
  display: flex;
  /* flex-wrap: wrap; */
  gap: 10px;
  align-items: flex-start;
}

.product-image {
  width: calc((100% - 70px) / 8);
  height: 70px;
  object-fit: cover;
  margin-top: 0;
  margin-right: 10px;
}

.container {
  width: 100%;
  height: 800px;
  overflow-y: auto;
}

.label {
  width: 120px;
  margin-bottom: 10px;
}
</style>
