<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :async="true"
      width="60vw"
      @close="handleClose"
      :bottom-btn="false"
    >
      <el-descriptions
        class="margin-top"
        :title="popupTitle"
        :column="3"
        border
      >
        <el-descriptions-item label="商户名称">
          {{ formData.merchant?.mer_name }}
        </el-descriptions-item>
        <el-descriptions-item label="联系人">
          {{ dataJSON.company_nickname }}
        </el-descriptions-item>
        <el-descriptions-item label="联系电话">
          {{ formData.merchant?.service_phone }}
        </el-descriptions-item>
        <el-descriptions-item label="公司名称" :span="2">
          {{ dataJSON.company_name }}
        </el-descriptions-item>
        <el-descriptions-item label="营业执照">
          <material-picker v-model="dataJSON.images" disabled />
        </el-descriptions-item>
        <el-descriptions-item label="公司地址" :span="2">
          {{ dataJSON.company_address }}
        </el-descriptions-item>
        <el-descriptions-item label="社会代码">
          {{ dataJSON.social_credit_code }}
        </el-descriptions-item>
        <el-descriptions-item label="更新时间">
          {{ dataJSON.update_time }}
        </el-descriptions-item>
      </el-descriptions>
      <el-form :model="auditInfo">
        <el-form-item label="审核状态" prop="status">
          <el-radio-group v-model="auditInfo.status" class="ml-4">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="0">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="mark">
          <el-input v-model="auditInfo.mark" type="textarea" />
        </el-form-item>
      </el-form>
      <div style="width: 100%; display: flex; justify-content: flex-end">
        <el-button @click="onAudit" type="primary">确认</el-button>
      </div>
    </popup>
  </div>
</template>

<script lang="ts" setup name="brandEdit">
import Popup from "@/components/popup/index.vue";
import { apiMerchantStatusUpdate } from "@/api/merchant";
import type { PropType } from "vue";
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({}),
  },
});
const emit = defineEmits(["success", "close"]);
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");

// 弹窗标题
const popupTitle = computed(() => {
  return "商户详情";
});

// 表单数据
const formData = reactive({
  financial_type: "",
  mer_id: "",
  category_id: "",
  type_id: "",
  mer_name: "",
  service_user: "",
  credit_buy: "",
  settle_cycle: "",
  interest_rate: "",
  province_id: "510000",
  city_id: "",
  city_name: "",
  area_id: "",
  area_name: "",
  street_id: "",
  street_name: "",
  village_id: "",
  village_name: "",
  mer_address: "",
  mer_avatar: "",
  merchant: {},
  apply_id: "",
  mark: "",
  sort: "",
  status: "",
  commission_rate: "",
  commission_switch: "",
  long: "",
  lat: "",
  service_phone: "",
  mer_money: "",
  financial_bank: "",
  financial_wechat: "",
  financial_alipay: "",
});

// 表单验证
const formRules = reactive<any>({
  name: [
    {
      required: true,
      message: "请输入品牌名称",
      trigger: ["blur"],
    },
  ],
});

const dataJSON = ref({});
// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
  dataJSON.value = JSON.parse(data.merchant?.data_json || "{}");
};

const auditInfo = reactive({
  status: 1,
  mark: "",
});
// 审核
const onAudit = () => {
  if (auditInfo.status == 0 && auditInfo.mark == "")
    return ElMessage.error("请输入拒绝原因");
  apiMerchantStatusUpdate({
    mer_id: formData.merchant?.mer_id,
    status: auditInfo.status,
    apply_id: formData.apply_id,
    service_phone: formData.merchant?.service_phone,
    mark: auditInfo.mark,
  }).then((res) => {
    emit('success');
    popupRef.value?.close();
  });
};

// 提交按钮
// const handleSubmit = async () => {
//     await formRef.value?.validate()
//     const data = { ...formData, }
//     mode.value == 'edit'
//         ? await apiBrandEdit(data)
//         : await apiBrandAdd(data)
//     popupRef.value?.close()
//     emit('success')
// }

//打开弹窗
const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

// 关闭回调
const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
});
</script>
