<!--
 * @Descripttion: (审核售后订单)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-10-17)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-10-17)
-->
<template>
  <el-dialog v-model="props.modelValue" title="审核售后订单" width="600px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row :gutter="20">
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="退款金额" prop="">
            ￥ {{ formData.refundMoney }}
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="审核状态" prop="isAgree">
            <div class="radio-box">
              <el-radio-group style="display: flex;" v-model="formData.isAgree" @change="handleSelect()">
                <el-radio :label=true>同意</el-radio>
                <el-radio :label=false>拒绝</el-radio>
              </el-radio-group>
              <!-- <div class="input-intro">同意后将退回付款金额并关闭订单</div> -->
            </div>
          </el-form-item>
        </el-col>

        <!-- 同意审核，商家选择退款地址 -->
        <el-col :lg="24" v-if="formData.isAgree">
          <el-form-item :label-width="labelWidth" label="退货地址" prop="">
            <el-select v-model="formData.shopAddressGuid" placeholder="请选择商家退货地址" clearable>
              <el-option v-for="item in  dataList " :key="item.shopAddressGuid" :label="item.addressName + '--' + item.shopAddressDetailedAddress"
                :value="item.shopAddressGuid"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <!-- 同意拒绝，商家说明原因 -->
        <el-col :lg="24" v-if="formData.isAgree == false">
          <el-form-item :label-width="labelWidth" label="拒绝说明" prop="">
            <el-input :rows="5" type="textarea" v-model="formData.merchantRefuseDesc" placeholder="请输入拒绝说明" />
          </el-form-item>
        </el-col>

      </el-row>


    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="loadingStatus">确认</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { reactive, ref, watch } from "vue";
import { auditOrderRefund } from '@/api/business/OrderManage/OrderRefunds/orderRefund.js'
import { shopAddressList } from '@/api/business/ShopManager/ShopAddresss/shopAddress.js'


// 打开弹窗时回调
const openDialog = async () => {
}

const formData = ref({
  ...props.data,
});
watch(props, async (v) => {
  formData.value = v.data;

});

// 业务参数
const dataList = ref([])
const params = ref({
  pageSize: 10000,
  shopGuid: formData.value.shopGuid,
  shopAddressType: 2
})
console.log(params.value,'参数');

// -业务方法
function getList() {
  shopAddressList(params.value).then((res) => {
    if (res.code == 200) {
      dataList.value = res.data.result;
      console.log(dataList.value, '地址列表');
    }
  });
}

function handleSelect() {
  if(formData.value.isAgree){
    params.value.shopGuid = formData.value.shopGuid
    getList()
    console.log(formData.value.shopAddressGuid);
  }
}


// -基础参数
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});

const loadingStatus = ref(false)
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const imgData = ref({
  fileDir: "Order"
})

// 验证
const rules = reactive({
  isAgree: [{ required: true, message: "审核状态不能为空", trigger: "blur" }],
});

// -基础方法
// 提交
const handleEditClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }

    loadingStatus.value = true

    const { code, data } = await auditOrderRefund(formData.value);
    if (code == 200) {
      modal.msgSuccess(data)
      props.done();
      closeDialog();
      loadingStatus.value = false
    }
  });
}

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
}
const closeDialog = () => {
  emits("update:modelValue", false);
};

</script>

<style>
.input-intro {
  color: #8c8c8c;
  font-size: 12px;
}

.radio-box {
  display: flex;
  flex-direction: column;
}
</style>