<template>
  <el-row>
    <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
      <el-col :span="24" class="one">
        <cSearch :is_title="false" :is_search="true" :fields="fields" @search="toSearch">
          <template #status>
            <el-option v-for="i in statusList" :key="i._id" :label="i.label" :value="i.value"></el-option>
          </template>
          <template #brand>
            <el-option v-for="i in brandList" :key="i._id" :label="i.name" :value="i.name"></el-option>
          </template>
        </cSearch>
      </el-col>
      <el-col :span="24" class="two">
        <cTable :fields="fields" :opera="opera" :list="list" @query="search" :total="total" @evaluate="toEvaluate"> </cTable>
      </el-col>
    </el-col>
  </el-row>
  <cDialog :dialog="dialog" @toClose="toClose">
    <el-col :span="24" class="dialog_one" v-if="dialog.type == '1'">
      <cForm :span="24" :fields="formFields" :form="form" :rules="{}" @save="toSave" label-width="auto">
        <template #status>
          <el-option v-for="i in statusList" :key="i._id" :label="i.label" :value="i.value"></el-option>
        </template>
      </cForm>
    </el-col>
  </cDialog>
</template>

<script setup lang="ts">
import { ref, Ref, onMounted, inject } from 'vue';
// NeedChange
import { EstimateStore } from '@/stores/estimate';
import { BrandStore } from '@/stores/system/brand';
import { DictDataStore } from '@/stores/system/dictData';
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 searchOther();
  await search({ skip, limit });
  loading.value = false;
});

const loading: Ref<any> = ref(false);
// NeedChange
const store = EstimateStore();
const brandStore = BrandStore();
const dictDataStore = DictDataStore();
const $checkRes = inject('$checkRes') as Function;

// #region 字典
// NeedChange
const statusList: Ref<any> = ref([]);
const brandList: Ref<any> = ref([]);

const searchOther = async () => {
  const statusResult: IQueryResult = await dictDataStore.query({ code: 'valuation', is_use: '0' });
  if ($checkRes(statusResult)) statusList.value = statusResult.data;
  const brandResult: IQueryResult = await brandStore.query({ is_use: '0' });
  if ($checkRes(brandResult)) brandList.value = brandResult.data;
};
// #endregion

// #region 查询相关
let list: Ref<any> = ref([]);
let total: Ref<number> = ref(0);
let skip = 0;
let limit = inject('$limit') as number;
let searchForm: Ref<any> = ref({});

const search = async (e: { skip: number; limit: number }) => {
  const info = { skip: e.skip, limit: e.limit, ...searchForm.value };
  const res: IQueryResult = await store.query(info);
  if (res.errcode == '0') {
    list.value = res.data;
    total.value = res.total;
  }
};
const toSearch = (query) => {
  searchForm.value = query;
  search({ skip, limit });
};
// #endregion

// #region 表格及操作
// NeedChange
let fields: Ref<any[]> = ref([
  { label: '品牌', model: 'brand', isSearch: true, type: 'select' },
  { label: '车系', model: 'bank' },
  { label: '车型', model: 'type' },
  { label: '上牌日期', model: 'start' },
  { label: '上牌城市', model: 'city', isSearch: true },
  { label: '所在地区', model: 'place', isSearch: true },
  { label: '行驶里程', model: 'course', isSearch: true },
  { label: '联系人', model: 'contacts', isSearch: true },
  { label: '联系电话', model: 'tel', isSearch: true },
  { label: '预估金额', model: 'estimate' },
  { label: '状态', model: 'status', format: (i) => getDict(i, 'status'), isSearch: true, type: 'select' }
]);
// 操作
let opera: Ref<any[]> = ref([{ label: '估价', method: 'evaluate', display: (i) => i.status === '0' }]);

const getDict = (data, model) => {
  let list;
  switch (model) {
    case 'status':
      list = statusList.value;
      break;
    case 'brand':
      list = brandList.value;
      break;
    default:
      break;
  }
  if (!list) return;
  const res = list.find((f) => f.value == data);
  return get(res, 'label');
};
const toEvaluate = async (data) => {
  formFields.value = formFieldsForUpdate;
  form.value = { ...data };
  dialog.value.show = true;
};
// #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({ skip: 0, limit });
    toClose();
  }
};

// #endregion

// #region 表单及操作
// NeedChange
const formFields: Ref<any> = ref();
const dialog: Ref<any> = ref({ title: '数据信息', show: false, type: '1' });
const form: Ref<any> = ref({ file: [] });
const formFieldsForUpdate = [
  { label: '预估金额', model: 'estimate' },
  { label: '状态', model: 'status', type: 'select' }
];
// 关闭弹框
const toClose = () => {
  form.value = {};
  dialog.value.show = false;
};
// #endregion
</script>

<style scoped></style>
