<template>
  <div id="goods">
    <template v-if="view === 'list'">
      <search-1 :form="searchInfo" @onSubmit="search" @querySearch="querySearch" @toReset="toClose" :shopList="shopList"> </search-1>
      <data-btn :fields="btnList" @add="toAdd"></data-btn>
      <data-table
        ref="dataTable"
        :fields="fields"
        :opera="opera"
        :data="list"
        :total="total"
        :limit="limit"
        @query="search"
        @edit="toEdit"
        @puton="toPuton"
        @lower="toLower"
        @delete="toDelete"
        @spec="toSpec"
        @copy="toCopy"
      ></data-table>
    </template>
    <template v-else>
      <el-row>
        <el-col :span="24">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
        <el-col :span="24">
          <data-form :fields="infoFields" :rules="rules" v-model="form" labelWidth="150px" @save="toSave">
            <template #tags="{ item }">
              <el-cascader v-model="form[item.model]" :options="tagsList" :props="props" clearable filterable :show-all-levels="false"></el-cascader>
            </template>
            <template #act_tags>
              <el-option v-for="i in act_tagsList" :key="i.value" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #status>
              <el-option v-for="i in goodsStatusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #brief>
              <editor v-model="form.brief" url="/files/point/goods/upload" />
            </template>
            <template #shop>
              <el-select
                v-model="form.shop"
                filterable
                remote
                reserve-keyword
                placeholder="请输入商铺名称"
                :remote-method="searchShop"
                :loading="loading"
                @change="selectShop"
              >
                <el-option v-for="item in shopList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
              </el-select>
            </template>
          </data-form>
        </el-col>
      </el-row>
    </template>
  </div>
</template>

<script>
const _ = require('lodash');
import methodsUtil from '@/util/opera';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: goods } = createNamespacedHelpers('goods');
const { mapActions: goodsTags } = createNamespacedHelpers('goodsTags');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
const { mapActions: actTags } = createNamespacedHelpers('actTags');
const { mapActions: shop } = createNamespacedHelpers('shop');

export default {
  name: 'index',
  props: {},
  components: { editor: () => import('@/components/editor.vue'), search1: () => import('./parts/search-1.vue') },
  data: function () {
    return {
      view: 'list',
      fields: [
        { label: '排序', model: 'sort' },
        { label: '商品名称', model: 'name' },
        { label: '店铺名称', model: 'shop.name' },
        { label: '商品分类', model: 'tags', format: (i) => this.getTags(i) },
        { label: '活动标签', model: 'act_tags', format: (i) => this.getAct_tags(i) },
        { label: '商品状态', model: 'status', format: (i) => this.getStatus(i) },
      ],
      opera: [
        { label: '修改', method: 'edit' },
        { label: '上架', method: 'puton', display: (i) => i.status == '0' },
        { label: '下架', method: 'lower', display: (i) => i.status == '1' },
        { label: '库存管理', method: 'spec' },
        { label: '复制', method: 'copy' },
        { label: '删除', method: 'delete', confirm: true, type: 'danger' },
      ],
      btnList: [{ label: '添加', method: 'add' }],
      searchInfo: {},
      list: [],
      total: 0,
      limit: 10,
      // info部分
      infoFields: [
        { label: '商铺名称', model: 'shop', custom: true },
        { label: '商品名称', model: 'name' },
        { label: '商品分类', model: 'tags', custom: true },
        { label: '活动标签', model: 'act_tags', type: 'selectMany' },
        { label: '简短简介', model: 'shot_brief', maxLength: 50 },
        { label: '预计发货时间', model: 'send_time' },
        { label: '商品状态', model: 'status', type: 'select' },
        { label: '商品来源', model: 'source' },
        { label: '网址', model: 'url' },
        { label: '排序', model: 'sort', type: 'number' },
        { label: '商品图片', model: 'file', type: 'upload', url: '/files/point/goods/upload' },
        { label: '商品介绍', model: 'brief', custom: true },
      ],

      rules: {
        shop: [{ required: true, message: '请选择商铺名称', trigger: 'change' }],
      },
      form: {},
      // 商品分类
      tagsList: [],
      props: { multiple: true, label: 'label', value: 'code' },
      // 活动标签
      act_tagsList: [],

      goodsStatusList: [],
      // 商铺列表
      shopList: [],
      // 店铺远程搜索加载
      loading: false,
    };
  },
  created() {
    this.searchOthers();
    this.search();
  },
  methods: {
    ...dictData({ getDict: 'query' }),
    ...actTags({ actQuery: 'query' }),
    ...goodsTags(['tree']),
    ...shop({ shopQuery: 'query', shopFetch: 'fetch' }),
    ...goods(['copy', 'query', 'delete', 'fetch', 'update', 'create']),
    ...methodsUtil,
    // 查询
    async search({ skip = 0, limit = this.limit, ...others } = {}) {
      let query = { skip, limit, ...others };
      if (Object.keys(this.searchInfo).length > 0) query = { ...query, ...this.searchInfo };
      const res = await this.query(query);
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 去编辑
    async toEdit({ data }) {
      const res = await this.fetch(data._id);
      if (this.$checkRes(res)) {
        if (data.shop) {
          let res = await this.shopFetch(data.shop._id);
          if (this.$checkRes(res)) {
            this.shopList.push(res.data);
          }
        }
        this.$set(this, `form`, res.data);
        this.view = 'info';
      } else {
        this.$message.error('未找到指定数据');
      }
    },
    // 多选
    handleSelect(data) {
      this.$emit('handleSelect');
    },
    // 店铺名称远程查询
    async querySearch(value) {
      this.loading = true;
      let res = await this.shopQuery({ name: value });
      if (this.$checkRes(res)) {
        this.$set(this, 'shopList', res.data);
      }
      this.loading = false;
    },
    selectShop(data) {},
    async searchShop(value) {
      let res = await this.shopQuery({ name: value });
      if (this.$checkRes(res)) {
        this.$set(this, 'shopList', res.data);
      }
    },
    toClose() {
      this.searchInfo = {};
      this.search();
    },
    // 添加自定义
    initAddData() {
      const obj = {
        status: '1',
      };
      this.$set(this, 'form', obj);
    },
    // 复制
    async toCopy({ data }) {
      this.$confirm('是否确认复制该商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        let res;
        res = await this.copy(data.id);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `复制成功` });
          this.toBack();
          this.search();
        }
      });
    },
    // 查询其他信息
    async searchOthers() {
      let res = await this.tree();
      if (this.$checkRes(res)) this.$set(this, `tagsList`, res.data);
      res = await this.getDict({ code: 'goods_status' });
      if (this.$checkRes(res)) this.$set(this, `goodsStatusList`, res.data);
      res = await this.actQuery();
      if (this.$checkRes(res)) this.$set(this, `act_tagsList`, res.data);
    },
    // 商品状态
    getStatus(data) {
      const res = this.goodsStatusList.find((f) => f.value === data);
      if (res) return res.label;
      return '';
    },
    // 活动标签
    getAct_tags(data) {
      const arr = [];
      for (const val of data) {
        const r = this.act_tagsList.find((f) => f.value === val);
        if (r) arr.push(r.label);
      }
      return arr.join(';');
    },
    // 商品分类
    getTags(data) {
      let list = this.tagsList;
      const getChildren = (list) =>
        list.map((i) => {
          if (i.children) return getChildren(i.children);
          return i;
        });
      list = _.flattenDeep(getChildren(list));
      const arr = [];
      for (const ts of data) {
        const last = _.last(ts);
        const r = list.find((f) => f.code === last);
        if (r) arr.push(r.label);
      }
      return arr.join(';');
    },
    // 保存
    async toSave({ data }) {
      let res;
      if (data.id) res = await this.update(data);
      else res = await this.create(data);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `维护信息成功` });
        this.toBack();
        this.search();
      }
    },
    // 执行返回
    toBack() {
      this.form = {};
      this.view = 'list';
    },
    // 上架
    async toPuton({ data }) {
      this.$confirm('是否确认上架该商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        data.status = '1';
        let res;
        if (data.id) res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `上架成功` });
        }
        this.search();
      });
    },
    // 下架
    async toLower({ data }) {
      this.$confirm('是否确认下架该商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        data.status = '0';
        let res;
        if (data._id) res = await this.update(data);
        if (this.$checkRes(res)) {
          console.log(res.data);
          this.$message({ type: `success`, message: `下架成功` });
        }
        this.search();
      });
    },
    // 库存管理
    toSpec({ data }) {
      this.$router.push({ path: `/selfShop/spec/${data._id}` });
    },
  },
  computed: {
    ...mapState(['user']),
  },
};
</script>

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