import { sync } from '@/api/sync';
import importFile from '@/components/import';
import FormDetail from '@/components/form-detail';
import Form from '@/components/form';

// 搜索、重置
export const searchOpt = {
  data() {
    return {
      condition: {}
    };
  },
  methods: {
    /**
     * 查询
     */
    receiveSearch(value) {
      this.condition = value;
      this.handleSearch();
    },
    handleSearch() {
      this.$nextTick(() => {
        this.$refs.table.handleSearch();
      });
    }
  }
};

// 同步
export const syncOpt = {
  methods: {
    handleSync(url) {
      sync(url).then(res => {
        this.$message.success('同步中，若数据量大，可能会存在延迟，请稍后自行刷新！');
        // this.handleSearch();
      });
    }
  }
};

// 导入
export const importOpt = {
    methods: {
  /**
   * @description: 导入
   * @param { 接口地址 } action
   * @param { 下载模板 } temp
   * @return {*}
   */
  handleImport(action, temp, title = '导入') {
    const vm = this;
    const layer = this.$dgLayer({
      title,
      content: importFile,
      props: {
        temp,
        action
      },
      on: {
        success(params) {
          vm.handleSearch();
          layer.close(layer.dialogIndex);
        }
      },
      cancel: function(index) {
        // 关闭对应弹窗的ID
        layer.close(index);
        return false;
      },
      area: ['550px', '500px']
    });
  }
}
};

// 详情
export const detailOpt = {
  methods: {
    async handleViewDetail(id, { content = FormDetail, config, labelWidth, area, title = '详情' }) {
      const detailInfo = await this.getDetail(id);
      this.$dgLayer({
        title,
        content,
        props: {
          detailInfo,
          config,
          labelWidth
        },
        area
      });
    }
  }
};

// 新增or编辑
export const addEditOpt = {
  methods: {
    /**
     * @description: 新增or编辑
     * @param { 标题 } title
     * @param { 类型，新增还是编辑 } type
     * @param { 详情数据 } detailInfo
     * @param { 弹出的表单 } content
     * @param { 表单配置 } config
     * @param { 表单文字长度 } labelWidth
     * @param { 弹框的宽高 } area
     * @return {*}
     */

    handleOpenEditForm(
      title,
      type,
      { content = Form, config, labelWidth = '120px', area = ['600px', '500px'], detailInfo = {} }
    ) {
      const vm = this;
      const layer = this.$dgLayer({
        title,
        content,
        props: {
          detailInfo,
          type,
          config,
          labelWidth
        },
        on: {
          success(form, type) {
            // 此处处理编辑和新增逻辑
            vm.saveForm(form, type, layer);
          }
        },
        area
      });
    }
  }
};
