<template>
  <el-row>
    <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
      <el-col :span="24" class="one">
        <cForm :span="24" :fields="formFields" :form="form" :rules="{}" @save="toSave" label-width="auto">
          <template #mode>
            <el-radio v-for="i in statusList" :key="i._id" :label="i.value">{{ i.label }}</el-radio>
          </template>
          <template #logo>
            <cUpload model="logo" :list="form.logo" :limit="1" url="/files/usedCar/config/upload" listType="picture-card" @change="onUpload"></cUpload>
          </template>
        </cForm>
      </el-col>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { ref, Ref, onMounted, inject } from 'vue';
// NeedChange
import { ConfigStore } from '@/stores/system/config';
import type { IQueryResult } from '@/util/types.util';
import { cloneDeep, get } from 'lodash';
import baseStore from '@/stores/counter';
const user = ref(baseStore.state.user);
onMounted(async () => {
  loading.value = true;
  await search();
  loading.value = false;
});

const loading: Ref<any> = ref(false);
// NeedChange
const store = ConfigStore();
const $checkRes = inject('$checkRes') as Function;

// #region 字典
// NeedChange
const statusList: Ref<any> = ref([
  { label: '平台销售', value: '0' },
  { label: '多店铺销售', value: '1' }
]);

// #endregion
const search = async () => {
  let res: IQueryResult = await store.query();
  if (res.errcode == '0') {
    form.value = res.data;
  }
};
// #endregion

// #region 常规接口
const toSave = async () => {
  const data = cloneDeep(form.value);
  let res: IQueryResult;
  if (get(data, '_id')) res = await store.update(data);
  else res = await store.create(data);
  if ($checkRes(res, true)) {
    search();
  }
};

// #endregion

// #region 表单及操作
// NeedChange
const formFields: Ref<any> = ref([
  { label: '销售模式', model: 'mode', type: 'radio' },
  { label: '联系人', model: 'contacts' },
  { label: '联系电话', model: 'tel' },
  { label: 'logo', model: 'logo', custom: true },
  { label: '底部文案', model: 'bottom_title' }
]);
const form: Ref<any> = ref({ logo: [] });
// #endregion
const onUpload = (e: { model: string; value: Array<[]> }) => {
  console.log(e);
  const { model, value } = e;
  form.value[model] = value;
};
</script>

<style scoped></style>
