<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="professional_service_icon">
            <el-popover placement="bottom-start" :teleported="false" :width="540" :visible="showChooseIcon"
              trigger="click">
              <template #reference>
                <el-input v-model="formData.professional_service_icon" placeholder="点击选择图标" @click="showSelectIcon"
                  readonly>
                  <template #prefix>
                    <svg-icon v-if="formData.professional_service_icon_name"
                      :name="formData.professional_service_icon_name" />
                    <el-icon v-else>
                      <search />
                    </el-icon>
                  </template>
                </el-input>
              </template>
              <icon-select ref="iconSelectRef" @selected="selected" />
            </el-popover>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="语言类型" prop="i18n_lang_type">
            <el-select v-model="formData.i18n_lang_type_guid" clearable placeholder="请选择">
              <el-option v-for="item in i18n_lang_type" :key="item.i18n_lang_type_guid"
                :label="item.i18n_lang_type_code" :value="item.i18n_lang_type_guid"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="标题" prop="professional_service_title">
            <el-input v-model='formData.professional_service_title' type="text" placeholder='请输入标题'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="描述" prop="professional_service_describe">
            <el-input v-model='formData.professional_service_describe' type="textarea" :rows="5"
              placeholder='请输入描述'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="跳转链接" prop="professional_service_link">
            <el-input v-model='formData.professional_service_link' type="text" placeholder='请输入跳转链接'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="professional_service_sort">
            <el-input-number v-model='formData.professional_service_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 { editProfessionalService } from "~/service/professional_service";
import { useLoginStore } from "~/store";
import { getI18nLangTypeList } from '~/service/i18n_lang_type';

// --业务参数
const showChooseIcon = ref(false)
const iconSelectRef = ref(null)



// --业务方法

/** 展示下拉图标 */
function showSelectIcon() {
  iconSelectRef.value.reset()
  showChooseIcon.value = true
}
/** 选择图标 */
function selected(name, code) {
  console.log(name + code, 'asdasd');
  formData.value.professional_service_icon_name = name
  formData.value.professional_service_icon = code
  showChooseIcon.value = false
}

const i18n_lang_type = ref();
async function geti18n_lang_typeList() {
  getI18nLangTypeList({ limit: 9999 }).then((res) => {
    if (res.code == 0) {
      i18n_lang_type.value = res.data
    }
  })
}



// --基础参数
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: "ProfessionalService"
})


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


});

// 打开弹窗时执行
const openDialog = async () => {
  await geti18n_lang_typeList()
};

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

const rules = reactive({
  professional_service_icon: [
    {
      required: true,
      message: '图标不能为空'
    }
  ],
  professional_service_title: [
    {
      required: true,
      message: '标题不能为空'
    }
  ],
  professional_service_link: [
    {
      required: true,
      message: '跳转链接不能为空'
    }
  ],
  professional_service_describe: [
    {
      required: true,
      message: '描述不能为空'
    }
  ],
  professional_service_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 editProfessionalService(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>
