<template>
     <!-- Modal -->
     <a-modal
      v-model:visible="medinceVisible"
      fullscreen
      :title="'药品详情'"
      title-align="start"
      :auto-label-width="true"
      :footer="false"
      @cancel="handleClose"
    >
      <div class="titlebox">
        <div class="bar"></div>
        <div class="name">订单信息</div>
      </div>
      <a-form :model="modalForm" ref="modalFormRef" :auto-label-width="true">
        <a-row :gutter="24" style="margin-top: 35px">
          <a-col :span="12">
            <a-form-item field="inquiry_no" label="订单编号：">
              <span>{{ modalForm.order_product_no }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item field="order_product_status" label="处方编号：">
              <span v-if="modalForm.order_prescription">{{
                modalForm.order_prescription.prescription_code
              }}</span>
              <span v-else>暂无</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item field="user.created_at" label="下单时间：">
              <span>{{ modalForm.created_at }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item field="pay_time" label="支付时间：">
              <span v-if="parseTime(modalForm.pay_time)">{{
                parseTime(modalForm.pay_time)
              }}</span>
              <span v-else>-</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item field="amount_total" label="订单金额：">
              <span>{{ modalForm.amount_total }}元</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item field="logistics_fee" label="邮费：">
              <span>{{ modalForm.logistics_fee }}元</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item field="payment_amount_total" label="实付金额：">
              <span>{{ modalForm.payment_amount_total }}元</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item field="order_product_status" label="订单状态：">
              <span>{{
                formatProductStatus(modalForm.order_product_status)
              }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24" v-if="modalForm.cancel_reason">
          <a-col :span="24">
            <a-form-item field="department_custom_name" label="备   注 ：">
              <span>{{
                formatProductCancelReason(modalForm.cancel_reason)
              }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-divider />
        <div class="titlebox" v-if="modalForm.order_product_refund">
          <div class="bar"></div>
          <div class="name">退款信息</div>
        </div>
        <a-row
          :gutter="24"
          style="margin-top: 35px"
          v-if="modalForm.order_product_refund"
        >
          <a-col :span="12">
            <a-form-item field="inquiry_refund_no" label="退款编号：">
              <span>{{
                modalForm.order_product_refund.product_refund_no
              }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item field="escrow_trade_no" label=" 第三方订单编号：">
              <span>{{ modalForm.order_product_refund.refund_id }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24" v-if="modalForm.order_product_refund">
          <a-col :span="12">
            <a-form-item field="inquiry_refund_no" label="退款金额：">
              <span>{{ modalForm.order_product_refund.refund_total }}元</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item field="escrow_trade_no" label="退款状态：">
              <span>{{
                formatRefundStatus(
                  modalForm.order_product_refund.product_refund_status
                )
              }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24" v-if="modalForm.order_product_refund">
          <a-col :span="24">
            <a-form-item field="inquiry_refund_no" label="退款时间：">
              <span v-if="parseTime(modalForm.order_product_refund.success_time)">{{ modalForm.order_product_refund.success_time }}</span>
              <span v-else>-</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24" v-if="modalForm.order_product_refund">
          <a-col :span="24">
            <a-form-item field="inquiry_refund_no" label="退款原因：">
              <span>{{ modalForm.order_product_refund.refund_reason }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-divider v-if="modalForm.order_product_refund" />
        <div class="titlebox" v-if="modalForm.order_product_item.length > 0">
          <div class="bar"></div>
          <div class="name">药品信息</div>
        </div>
        <a-row
          :gutter="24"
          style="margin-top: 35px"
          v-if="modalForm.order_product_item.length > 0"
        >
          <a-col :span="24">
            <a-form-item field="license_cert" :hide-label="true">
              <a-list>
                <a-list-item
                  v-for="item in modalForm.order_product_item"
                  :key="item.product_item_id"
                >
                  <a-list-item-meta
                    :title="item.product_name + item.product_spec"
                    :description="item.manufacturer + '*' + item.amount"
                  >
                    <template #avatar>
                      <a-avatar shape="square">
                        <img
                          alt="avatar"
                          src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/3ee5f13fb09879ecb5185e440cef6eb9.png~tplv-uwbnlip3yd-webp.webp"
                        />
                      </a-avatar>
                    </template>
                  </a-list-item-meta>
                </a-list-item>
              </a-list>
            </a-form-item>
          </a-col>
        </a-row>
        <a-divider v-if="modalForm.order_product_item.length > 0" />
        <div class="titlebox">
          <div class="bar"></div>
          <div class="name">地址信息</div>
          <a-space
            style="
              margin-right: 8px;
              flex: 1;
              display: flex;
              justify-content: flex-end;
            "
          >
            <a-button type="primary" status="warning" @click="toggleAddress"
              >{{lookAllAddress?'隐藏完整地址信息':'查看完整地址信息'}}</a-button
            >
          </a-space>
        </div>
       <a-row :gutter="24"  style="margin-top: 35px">
            <a-col :span="12">
              <a-form-item field="inquiry_no" label="收货人姓名：">
                <span v-if="!lookAllAddress"> {{ modalForm.consignee_name_mask}}</span>
                <span v-else>{{AddressData.consignee_name}}</span>
              </a-form-item>
            </a-col> 
            <a-col :span="12">
              <a-form-item field="inquiry_no" label="收货人电话：">
                <span v-if="!lookAllAddress"> {{modalForm.consignee_tel_mask}}</span>
                <span v-else>{{AddressData.consignee_tel}}</span>
              </a-form-item>
            </a-col> 
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
              <a-form-item field="inquiry_no" label="收货人地址：">
                <span v-if="!lookAllAddress"> {{modalForm.address_mask }}</span>
                <span v-else>{{AddressData.address}}</span>
              </a-form-item>
            </a-col> 
      </a-row> 
        <a-divider />
        <div class="titlebox">
          <div class="bar"></div>
          <div class="name">物流信息</div>
        </div>
        <a-row :gutter="24" style="margin-top: 35px">
          <a-col :span="24">
            <a-form-item field="license_cert" :hide-label="true">
              <div class="timeline" v-if="modalForm.order_product_logistics">
                <a-timeline>
                  <a-timeline-item
                    :label="item.time"
                    :dotColor="formatColor(item.status)"
                    v-for="item in cur_express"
                  >
                    {{ item.context }}
                  </a-timeline-item>
                </a-timeline>
                <a-button @click="toggleExpand" class="expand">{{
                  isExpand ? '收起' : '展开'
                }}</a-button>
              </div>
              <div v-else>暂无物流信息</div>
            </a-form-item>
          </a-col>
        </a-row>

        <a-divider />
      <div class="titlebox" v-if="modalForm.order_prescription">
        <div class="bar"></div>
        <div class="name">处方信息</div>
      </div>
      <a-row
        :gutter="24"
        style="margin-top: 35px"
        v-if="modalForm.order_prescription"
      >
        <a-col :span="24">
          <a-form-item field="license_cert" :hide-label="true">
            <tableUnit
              :pagination="false"
              :columns="modalForm.chufang_columns"
              :data="modalForm.chufang_data"
            ></tableUnit>
            <!-- <prescription :data="modalForm.order_prescription"></prescription> -->
          </a-form-item>
        </a-col>
      </a-row>
      <a-divider v-if="modalForm.order_prescription" />
      <div class="titlebox" v-if="modalForm.user_doctor">
        <div class="bar"></div>
        <div class="name">医生信息</div>
        <a-space
          style="
            margin-right: 8px;
            flex: 1;
            display: flex;
            justify-content: flex-end;
          "
        >
          <a-button type="primary" @click="openDcotor"
            >查看医生完整信息</a-button
          >
        </a-space>
      </div>
      <a-row :gutter="24" style="margin-top: 35px">
        <a-col :span="24">
          <a-form-item field="license_cert" :hide-label="true">
            <div class="doctorInfo" v-if="modalForm.user_doctor">
              <a-space size="large">
                <a-image
                  width="80"
                  height="80"
                  class="headImg"
                  :src="modalForm.user_doctor.avatar"
                  v-if="modalForm.user_doctor.avatar"
                >
                </a-image>
                <a-image
                  width="80"
                  height="80"
                  class="headImg"
                  src="https://img.applets.igandanyiyuan.com/basic/file/doctor_avatar.png"
                  v-else
                >
                </a-image>
              </a-space>
              <div class="infobox">
                <div class="name">
                  {{ modalForm.user_doctor.user_name }}&nbsp;&nbsp;{{
                    formatDoctorTitle(modalForm.user_doctor.doctor_title)
                  }}
                </div>
                <div class="hospital">
                  {{
                    modalForm.user_doctor.hospital.hospital_name
                  }}&nbsp;&nbsp;{{
                    modalForm.user_doctor.department_custom_name
                  }}
                </div>
              </div>
            </div>
            <div class="doctorInfo" v-else>暂未分配到医生</div>
          </a-form-item>
        </a-col>
      </a-row>
      <a-divider v-if="modalForm.user_doctor" />
      <div class="titlebox">
        <div class="bar"></div>
        <div class="name">就诊人信息</div>
        <a-space
          style="
            margin-right: 8px;
            flex: 1;
            display: flex;
            justify-content: flex-end;
          "
        >
          <a-button type="primary" status="warning" @click="openPatient"
            >查看就诊人完整信息</a-button
          >
        </a-space>
      </div>
      <a-row :gutter="24"  style="margin-top: 35px" v-if="modalForm.order_inquiry_case">
        <a-col :span="24">
          <a-form-item  label="就诊人姓名：">
            <span>{{modalForm.order_inquiry_case.name }}&nbsp;&nbsp;（{{modalForm.order_inquiry_case.sex == 1 ? '男' : '女'}}&nbsp;&nbsp;{{ modalForm.order_inquiry_case.age }}岁）</span>
          </a-form-item>
        </a-col>
      </a-row>
       <a-row :gutter="24" v-if="modalForm.order_inquiry_case">
        <a-col :span="24">
              <a-form-item field="inquiry_no" label="病情描述：">
                <span>{{modalForm.order_inquiry_case.disease_desc}}</span>
              </a-form-item>
            </a-col> 
      </a-row>
      <a-row :gutter="24" v-if="modalForm.order_inquiry_case">
        <a-col :span="24">
              <a-form-item field="inquiry_no" label="确诊疾病：">
                <span>{{modalForm.order_inquiry_case.disease_class_name}}&nbsp;&nbsp;{{modalForm.user_doctor.department_custom_name}}</span>
              </a-form-item>
            </a-col> 
      </a-row> 
      <a-divider />
      <div class="titlebox">
        <div class="bar"></div>
        <div class="name">处方上报状态</div>
      </div>
      <a-row :gutter="24"  style="margin-top: 35px">
        <a-col :span="24">
              <a-form-item field="inquiry_no" label="上报状态：">
                <span> {{ fromatReportStatus(modalForm.report_pre_status) }}</span>
              </a-form-item>
            </a-col> 
      </a-row>
      <a-row :gutter="24" >
        <a-col :span="24">
              <a-form-item field="inquiry_no" label="上报时间：">
                <span v-if="parseTime(modalForm.report_pre_time)"> {{modalForm.report_pre_time}}</span>
                <span v-else>-</span>
              </a-form-item>
            </a-col> 
      </a-row>
      <a-row :gutter="24" v-if="modalForm.report_pre_fail_reason">
        <a-col :span="24" >
              <a-form-item field="inquiry_no" label="上报失败原因：">
                <span> {{ modalForm.report_pre_fail_reason}}</span>
              </a-form-item>
            </a-col> 
      </a-row>
      <a-divider />
      </a-form>

      <div class="titlebox">
        <div class="bar"></div>
        <div class="name">操作</div>
      </div>
      <a-row :gutter="24" style="margin-top: 35px">
        <a-col :span="24">
          <a-form-item field="" label="" no-style>
            <a-space
              style="margin-right: 8px"
              v-if="modalForm.report_pre_status == 2 && modalForm.refund_status!=2 &&  modalForm.refund_status!=3"
            >
              <a-button
                type="primary"
                v-has="'admin:sysProductList:report'"
                @click="
                  () => {
                    isVisible = true;
                    order_product_id = modalForm.order_product_id;
                  }
                "
                >上报处方平台</a-button
              >
            </a-space>
            <!-- 订单状态=2退款状态=4，5支付状态=2上报状态!=1 -->
            <a-space
              style="margin-right: 8px"
              v-if="
                (modalForm.order_product_status == 2 && modalForm.pay_status==2
                 && (modalForm.refund_status==4 || modalForm.refund_status==5 || modalForm.refund_status==0) &&
                modalForm.report_pre_status != 1 ) 
              "
            >
              <a-button
                type="primary"
                v-has="'admin:sysProductList:cancel'"
                status="danger"
                @click="
                  () => {
                    okVisible = true;
                    cur_product_id = modalForm.order_product_id;
                  }
                "
                >取消药品订单</a-button
              >
            </a-space>
          </a-form-item>
        </a-col>
      </a-row>
    </a-modal>
     <!-- 医生详情弹框 -->
     <doctorModal
      :doctorVisible="doctorVisible"
      :doctor_id="doctor_id"
      @doctorVisibleChange="
        () => {
          doctorVisible = false;
          doctor_id = '';
        }
      "
    ></doctorModal>
    <!-- 患者详情弹窗 -->
    <patientModal
      :patientVisible="patientVisible"
      :data="modalForm.patientData"
      @patientVisibleChange="
        () => {
          patientVisible = false;
        }
      "
    ></patientModal>
    <confirmModal
      :okVisible="okVisible"
      :api="cancelProduct"
      :dealType="'order_inquiry_id'"
      :id="cur_product_id"
      @closeChange="closeChange"
    ></confirmModal>
    <isOkModal
      :isVisible="isVisible"
      :api="reportPrePscription"
      :title="'确定上报处方吗？'"
      :dealType="'order_product_id'"
      :id="order_product_id"
      @closeChangeOk="closeChangeOk"
    ></isOkModal>
</template>
<script setup>
import {reactive,ref,getCurrentInstance,watch,toRefs} from 'vue';
import { parseTime } from '@/utils/parseTime';
import {
  formatDoctorTitle,
  fromatReportStatus,
  formatPrescriptionStatus,
  formatPharmacistStatus,
  formatProductCancelReason,
} from '@/utils/format';
import {lookAddress,cancelProduct,reportPrePscription} from '@/api/order/list';
const IMG_URL = import.meta.env.VITE_IMG_URL;
const emits = defineEmits(['medinceVisibleChange']);
const AddressData=reactive({});
const getAddress= async (order_product_id)=>{
  const{data,code}= await lookAddress(order_product_id);
 if(code==200){
   Object.assign(AddressData,data)
 }
}
const props = defineProps({
  // 是否显示
  medinceVisible: {
    type: Boolean,
    default: false,
  },
  modalTitle:{
    type: String,
    default:'就诊人详情'
  },
  modalForm: {
    type: Object,
    default:{
      order_product_refund: {},
    order_product_item: [],
    order_product_logistics: null,
    user_doctor: null,
    express:[],
    patientData:{},
    order_prescription: null,
   }
  },
});
const cur_express=ref([]);

const isExpand = ref(false);
const lookAllAddress=ref(false);

const doctorVisible = ref(false);
const doctor_id = ref('');

const patientVisible = ref(false);

const okVisible = ref(false);
const cur_product_id = ref('');

const isVisible = ref(false);
const order_product_id= ref('');
const {medinceVisible,modalForm} = toRefs(props);
watch(()=>props.modalForm,(newValue,oldValue)=>{
  cur_express.value = props.modalForm.express.length > 1 && props.modalForm.express.slice(0, 1);
  console.log(props.modalForm);
  if(props.modalForm.order_product_id){
    getAddress(props.modalForm.order_product_id)
  }
},{immediate:true,deep:true})
const toggleExpand = () => {
  isExpand.value = !isExpand.value;
  if (isExpand.value) {
    cur_express.value =props.modalForm.express.slice(0, -1);
  } else {
    cur_express.value = props.modalForm.express.slice(0, 1);
  }
};
const toggleAddress=()=>{
  lookAllAddress.value=!lookAllAddress.value;
}

const openPatient = () => {
  //Object.assign(patientData, modalForm.order_inquiry_case);
  patientVisible.value = true;
};

const openDcotor=()=>{
  doctor_id.value=props.modalForm.user_doctor.doctor_id;
 doctorVisible.value=true
}

//关闭确认框
const closeChange = (data) => {
  cur_product_id.value = '';
  okVisible.value = false;
};
//关闭上传处方平台弹框
const closeChangeOk = (data) => {
  order_product_id.value = '';
  isVisible.value = false;

};

const handleClose=()=>{
  emits('medinceVisibleChange', (medinceVisible.value = false));
}
const formatProductStatus = (val) => {
  //订单状态（<!-- 1:待支付 2:待发货 3:已发货 4:已签收 5:已取消 -->
  let data = {
    1: '待支付',
    2: '待发货',
    3: '已发货',
    4: '已签收',
    5: '已取消',
  };
  if (val) {
    return data[val];
  } else {
    return '';
  }
};
const formatRefundStatus = (val) => {
  //问诊订单退款状态（0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常）
  let data = {
    0: '无退款',
    1: '申请退款',
    2: '退款中',
    3: '退款成功',
    4: '拒绝退款',
    5: '退款关闭',
    6: '退款异常',
  };
  if (val || val == 0) {
    return data[val];
  } else {
    return '';
  }
};
const formatColor = (value) => {
  if (value == '签收') {
    return '#00B42A';
  } else {
    return 'var(--color-fill-4)';
  }
};
</script>
<style scoped>
.headImg {
    margin-right: 20px;
    border-radius: 50%;
    width: 80px;
    height: 80px;
}
.doctorInfo {
    display: flex;
    align-items: center;
}
.infobox {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
</style>