<template>
  <el-dialog v-model="props.modelValue" title="编辑合作伙伴" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row>


        <el-col :span='12'>
          <el-form-item :label-width='labelWidth' label='图片' prop='partners_img'>
            <UploadImage ref='uploadRef' v-model='formData.partners_img' :data=uoloadData :limit='1' :fileSize='5'
              :drag='true' :isShowTip='true' />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="描述" prop="partners_des">
            <el-input v-model='formData.partners_des' type="textarea" :rows="5" placeholder='请输入描述'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="partners_sort">
            <el-input-number v-model='formData.partners_sort' controls-position='right' :min='1'></el-input-number>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="isBtnLod">保存</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
        <el-button @click="closeDialog()">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { editPartners } from "~/service/partners";
import { useLoginStore } from "~/store";

// --业务参数



// --业务方法





// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};
const isBtnLod = ref(false);
const formRef = ref();
const labelWidth = 100;
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const formData = ref({
  ...props.data,
});
const uoloadData = ref({
  dirName: "Partners"
})


// --基础方法
watch(props, (v) => {
  formData.value = v.data;


});

// 打开弹窗时执行
const openDialog = () => {

};

const closeDialog = () => {
  props.done();
  emits("update:modelValue", false);
};

const rules = reactive({
  partners_img: [
    {
      required: true,
      message: '图片不能为空'
    }
  ],
  // partners_des: [
  //   {
  //     required: true,
  //     message: '描述不能为空'
  //   }
  // ],
  partners_sort: [
    {
      required: true,
      message: '排序不能为空'
    }
  ],

});

const handleEditClick = async (formEl) => {
  console.log(formData.value);
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    isBtnLod.value = true;


    const { code } = await editPartners(formData.value);
    if (code == 0) {
      closeDialog();
      props.done();
    }
    isBtnLod.value = false;
  });
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style lang="less" scoped></style>
