<!--
 * @Descripttion: (小程序客户/tb_customer 编辑弹窗)
 * @version: (1.0)
 * @Author: (黎文豪)
 * @Date: (2023-06-05)
 * @LastEditors: (黎文豪)
 * @LastEditTime: (2023-06-05)
-->
<template>
  <el-dialog v-model="props.modelValue" title="修改小程序客户信息" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row :gutter="20">




        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="openid" prop="customerXcxOpenid">
            <el-input v-model="formData.customerXcxOpenid" placeholder="请输入openid" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="昵称" prop="customerNickname">
            <el-input v-model="formData.customerNickname" placeholder="请输入昵称" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="手机号" prop="customerMobilePhoneNumber">
            <el-input v-model="formData.customerMobilePhoneNumber" placeholder="请输入手机号" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="头像" prop="customerAvatar">
            <UploadImage ref="uploadRef" v-model="formData.customerAvatar" :data=imgData :limit="1" :fileSize="5"
              :drag="true" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="性别" prop="customerGender">
            <el-select v-model="formData.customerGender" placeholder="请选择性别">
              <el-option v-for="item in  sys_user_sex " :key="item.dictValue" :label="item.dictLabel"
                :value="parseInt(item.dictValue)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="可用余额" prop="customerAvailableBalance">
            <el-input-number v-model.number="formData.customerAvailableBalance" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="可用积分" prop="customerAvailablePoints">
            <el-input-number v-model.number="formData.customerAvailablePoints" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="总支付金额" prop="customerTotalPaymentAmount">
            <el-input-number v-model.number="formData.customerTotalPaymentAmount" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="实际消费金额" prop="customerActualConsumptionAmount">
            <el-input-number v-model.number="formData.customerActualConsumptionAmount" controls-position="right"
              :min="0" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="最后登录时间" prop="customerLastLoginTime">
            <el-date-picker v-model="formData.customerLastLoginTime" type="datetime" :teleported="false"
              placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
        </el-col>






      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)">编辑</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 { addOrUpdateCustomer } from "@/api/business/Custom/Customers/customer.js";


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

}

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

});

// 业务参数
// 性别字典选项列表
const sys_user_sex = ref([]);

// -业务方法
//字典获取
async function getsys_user_sex() {
  await proxy.getDicts('sys_user_sex').then((res) => {
    sys_user_sex.value = res.data
  })
}



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

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

// 验证
const rules = reactive({
  customerGuid: [{ required: true, message: "不能为空", trigger: "blur", type: "number" }],
  customerXcxOpenid: [{ required: true, message: "openid不能为空", trigger: "blur" }],
  customerNickname: [{ required: true, message: "昵称不能为空", trigger: "blur" }],
  customerMobilePhoneNumber: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
  customerAvatar: [{ required: true, message: "头像不能为空", trigger: "blur" }],
  customerGender: [{ required: true, message: "性别不能为空", trigger: "change", type: "number" }],
  customerAvailableBalance: [{ required: true, message: "可用余额不能为空", trigger: "blur" }],
  customerAvailablePoints: [{ required: true, message: "可用积分不能为空", trigger: "blur" }],
  customerTotalPaymentAmount: [{ required: true, message: "总支付金额不能为空", trigger: "blur" }],
  customerActualConsumptionAmount: [{ required: true, message: "实际消费金额不能为空", trigger: "blur" }],
  customerLastLoginTime: [{ required: true, message: "最后登录时间不能为空", trigger: "blur" }],
});

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


    const { code } = await addOrUpdateCustomer(formData.value);
    if (code == 200) {
      modal.msgSuccess('修改成功')
      closeDialog();
    }
  });
}

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

</script>
