import apis from '../../config/api'
import request from '../../utils/request'

export default {
    namespaced: true,
    state: {
        list: [],
        detail: []
    },
    mutations: {

    },
    actions: {
        getTypeList({ commit }, payload) { // 查询所有资迅类型
           return  new Promise((resolve, reject) => {
               request({
                   ...apis.newsType.list,
                   params: payload,
               }).then((data) => {
                   resolve(data)
               }).catch((err) => {
                   reject(err)
               })
           })
        },
        getTypeById({ commit }, payload) { // 根据Id查询资迅类型
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.newsType.getById,
                    urlData: { id: payload.newsTypeId },
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        addType({ commit }, payload) { // 添加类型
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.newsType.add,
                    data: payload,
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        updateType({ commit }, payload) { // 修改类型
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.newsType.update,
                    urlData:{ id: payload.newsTypeId },
                    data:payload
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        deleteType({ commit }, payload) { // 删除类型
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.newsType.delete,
                    urlData: { id: payload.newsTypeId },
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        getList({ commit }, payload) { // 查询所有资迅
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.news.list,
                    params: payload,
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        getById({ commit }, payload) { // 根据Id查询资迅
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.news.getById,
                    urlData: { id: payload.newsId },
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        add({ commit }, payload) { // 添加
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.news.add,
                    data: payload,
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        update({ commit }, payload) { // 修改
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.news.update,
                    urlData:{ id: payload.newsId },
                    data:payload
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        delete({ commit }, payload) { // 删除
            return  new Promise((resolve, reject) => {
                request({
                    ...apis.news.delete,
                    urlData: { id: payload.newsId },
                }).then((data) => {
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        }
    }
}