<template>
  <div id="index">
    <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"> </cSearch>
        </el-col>
        <el-col :span="24" class="two">
          <cButton @toAdd="toAdd()">
            <template v-slot:custom>
              <el-button type="primary" @click="toBack">返回</el-button>
            </template>
          </cButton>
        </el-col>
        <el-col :span="24" class="thr">
          <cTable :fields="fields" :opera="opera" :list="list" @query="search" :total="total" @edit="toEdit" @del="toDel"> </cTable>
        </el-col>
      </el-col>
    </el-row>
    <cDialog :dialog="dialog" @toClose="toClose">
      <template v-slot:info>
        <el-col :span="24" class="dialog_one" v-if="dialog.type == '1'">
          <cForm :span="24" :fields="formFields" :form="form" :rules="rules" @save="toSave">
            <template #file>
              <cUpload
                :model="`${'file'}`"
                :limit="6"
                listType="picture"
                url="/files/material/specs/upload"
                accept="*"
                :list="form.file"
                @change="onUpload"
              ></cUpload>
            </template>
            <template #is_use>
              <el-radio v-for="i in is_useList" :key="i._id" :label="i.value">{{ i.label }}</el-radio>
            </template>
          </cForm>
        </el-col>
      </template>
    </cDialog>
  </div>
</template>

<script setup lang="ts">
// 基础
import _ from 'lodash';
import type { FormRules } from 'element-plus';
import type { Ref } from 'vue';
import { ref, onMounted, getCurrentInstance, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
// 接口
import { SpecsStore } from '@/stores/good/spec'; //
import { DictDataStore } from '@/stores/dict/dictData';
import type { IQueryResult } from '@/util/types.util';
const dictData = DictDataStore();
const specAxios = SpecsStore();
// 路由
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance() as any;
// 加载中
const loading: Ref<any> = ref(false);
// 列表数据
let list: Ref<any> = ref([]);
let total: Ref<number> = ref(0);
let skip = 0;
let limit: number = proxy.$limit;
let fields: Ref<any[]> = ref([
  { label: '规格名称', model: 'name', isSearch: true },
  { label: '销售价格', model: 'money' },
  { label: '库存', model: 'num' },
  { label: '是否使用', model: 'is_use', format: (i: any) => getDict(i, 'is_use') }
]);
// 操作
let opera: Ref<any[]> = ref([
  { label: '修改', method: 'edit' },
  { label: '删除', method: 'del', confirm: true, type: 'danger' }
]);
// 弹框
const dialog: Ref<{ type: string; show: boolean; title: string }> = ref({ type: '1', show: false, title: '信息管理' });
let form: Ref<{ file: Array<any>; type: string }> = ref({ file: [], type: '' });
// 表单
let formFields: Ref<any[]> = ref([
  { label: '规格名称', model: 'name' },
  { label: '销售价格', model: 'money', type: 'number' },
  { label: '库存', model: 'num', type: 'number' },
  { label: '图片', model: 'file', custom: true },
  { label: '是否使用', model: 'is_use', type: 'radio' }
]);
const rules = reactive<FormRules>({
  name: [{ required: true, message: '规格名称', trigger: 'blur' }],
  money: [{ required: true, message: '销售价格', trigger: 'blur' }],
  num: [{ required: true, message: '库存', trigger: 'blur' }]
});
// 查询数据
let searchForm: Ref<any> = ref({});
// 字典表
let is_useList: Ref<any> = ref([]);
// 请求
onMounted(async () => {
  loading.value = true;
  await searchOther();
  await search({ skip, limit });
  loading.value = false;
});
const search = async (e: { skip: number; limit: number }) => {
  const { skip, limit } = e;
  const condition = _.cloneDeep(searchForm.value);
  let goods = route.query.id;
  let info = { limit: limit, skip: skip, ...condition, goods: goods };
  let res: IQueryResult = await specAxios.query(info);
  if (res.errcode == 0) {
    list.value = res.data;
    total.value = res.total;
  }
};
const toSearch = (query: any) => {
  searchForm.value = query;
  search({ skip, limit });
};
const getDict = (value: any, model: any) => {
  if (model == 'is_use') {
    if (value) {
      let data = is_useList.value.find((i: any) => i.value == value);
      if (data) return data.label;
      else return '暂无';
    }
  }
};
// 新增
const toAdd = () => {
  let type: any = route.query.type;
  form.value = { file: [], type: type };
  dialog.value = { title: '信息管理', show: true, type: '1' };
};
// 修改
const toEdit = async (data: any) => {
  form.value = data;
  dialog.value = { title: '信息管理', show: true, type: '1' };
};
// 上传图片
const onUpload = (e: { model: string; value: Array<[]> }) => {
  const { model, value } = e;
  form.value[model] = value;
};
// 提交保存
const toSave = async (data: any) => {
  let res: IQueryResult;
  data.goods = route.query.id;
  if (data._id) res = await specAxios.update(data);
  else res = await specAxios.create(data);
  if (res.errcode == 0) {
    ElMessage({ type: `success`, message: `维护信息成功` });
    toClose();
  }
};
// 删除
const toDel = async (data: any) => {
  let res: IQueryResult = await specAxios.del(data._id);
  if (res.errcode == 0) {
    ElMessage({ type: `success`, message: `刪除信息成功` });
    search({ skip, limit });
  }
};
// 弹框关闭
const toClose = () => {
  form.value = { file: [], type: '' };
  dialog.value = { title: '信息管理', show: false, type: '1' };
  search({ skip, limit });
};
// 查询其他信息
const searchOther = async () => {
  let res: IQueryResult;
  // 是否使用
  res = await dictData.query({ type: 'is_use' });
  if (res.errcode == '0') is_useList.value = res.data;
};
// 返回上一页
const toBack = () => {
  router.push({ path: '/goods/index' });
};
</script>
<style scoped lang="scss">
.main {
  .two {
    margin: 0 0 10px 0;
  }
}
</style>
