import React, {Component} from 'react'
import './index.css'
import {
Picker,
NavBar,
Accordion,
List,
InputItem,
ImagePicker,
Button,
ActivityIndicator,
Stepper,
Modal
} from 'antd-mobile'
import {Switch, Row, Col, Icon} from 'antd'
import {withRouter} from 'react-router-dom'
import {
create_product,
update_product,
category_by_props,
productbyprops,
update_category,
delete_category,
create_category
} from "../../../../utils/gql"
import {Query, Mutation} from "react-apollo"
import gql from "graphql-tag"
import {idGen} from "../../../../utils/func"
import moment from 'moment'
import {storeFile} from "../../../../configs/url"
import axios from 'axios'
import classNames from 'classnames'
const Item = List.Item
const categoryFilterRefetch = {
"status": "1",
"limit": 7,
"sort_by": {"order": "asc"}
}
const categoryFilter = {
"sort_by": {"order": "asc"}
}
class Goods extends Component {
constructor(props) {
super(props)
this.state = {
accordionKey: undefined
}
}
render() {
let {accordionKey} = this.state
return (
}
onLeftClick={() => {
this.props.history.go(-2)
}}
>商品管理
{accordionKey? '折叠单项以展开更多分类':'请选择需要打开的分类'}
{
this.setState({
accordionKey: key[0]
})
}}>
)
}
}
class AddGoods extends Component {
constructor(props) {
super(props)
let state = {
files: [],
imgDatas: [],
}
if (props.good === undefined) {
this.state = {
...state,
name: '',
price: 0,
intro: '',
stock: 20,
category: '',
category_id: '',
newGood: true
}
} else {
// console.log(props.good)
let {name, price, intro, stock, id} = props.good
this.state = {
...state,
id,
name,
price,
intro,
stock,
category: props.good.category_id.name,
category_id: [props.good.category_id.id],
newGood: false
}
}
}
onChange = (id) => (files, operationType) => {
let imgDatas = []
files.forEach((file, index) => {
let base64Cont = files[index].url.split(',')[1]
let imgType = files[index].file.type.split('/')[1]
let imgNewName = `good_id_${id}.${imgType}`
const imgData = {
'file-name': `e-commerce/images/${imgNewName}`,
'bucket': 'case',
'cont': base64Cont,
'public': true,
'format': 'base64'
}
imgDatas.push(imgData)
})
this.setState({
imgDatas,
files
})
}
uploadImg = () => {
let {imgDatas} = this.state
return imgDatas.map((imgData) => (
axios({
url: storeFile,
method: 'post',
data: imgData
})
))
}
render() {
let {files, imgDatas, name, intro, stock, price, category_id, newGood} = this.state
let id = newGood ? idGen('goods') : this.state.id
return (
{
this.setState({name: e})
}} value={name} placeholder="请输入名称">名称
{
({loading, error, data}) => {
if (loading) {
return (
)
}
if (error) {
return 'error!'
}
let categoryList = data.categorybyprops.map(category => {
category.value = category.id
category.label = category.text
return category
})
return (
{
this.setState({category_id: v})
}}
>
选择种类
)
}
}
{
this.setState({intro: e})
}} value={intro} placeholder="请输入简介">简介
{
this.setState({price: e})
}} value={price} placeholder="请输入价格">价格
- {
this.setState({stock: e})
}} value={stock} style={{width: '100%', minWidth: '100px'}} showNumber size="small"/>}>库存
商品图片
console.log(index, fs)}
selectable={true}
multiple={false}
/>
{
newGood ?
{(createproduct, {loading, error}) => {
if (loading)
return (
)
if (error)
return 'error'
let varObj = {
id,
unit: '1件',
status: '1',
recommend: 0,
category_id: category_id[0],
name,
stock,
intro,
price,
createdAt: moment().format('YYYY-MM-DD HH:mm:ss'),
updatedAt: ''
}
return (
)
}}
:
{(updateproduct, {loading, error}) => {
if (loading)
return (
)
if (error)
return 'error'
let varObj = {
id,
unit: '1件',
status: '1',
recommend: 0,
category_id: category_id[0],
name,
stock,
intro,
price,
updatedAt: moment().format('YYYY-MM-DD HH:mm:ss')
}
return (
)
}}
}
)
}
}
class AllGoods extends Component {
constructor(props) {
super(props)
this.state = {
modal: false,
product: {}
}
}
controlModal = (bool) => () => {
this.setState({
modal: bool
})
if (!bool) {
this.setState({
product: {}
})
}
}
render() {
let {modal, product} = this.state
return (
{
({loading, error, data}) => {
if (loading) {
return (
)
}
if (error) {
return 'error!'
}
let products = data.productbyprops
return (
{
products.map(product => {
return (
{product.name}
{(updateproduct, {loading, error}) => {
if (loading)
return (
)
if (error)
return 'error'
let {id, recommend} = product
let variables = {
id,
recommend: Number(!recommend),
updatedAt: moment().format('YYYY-MM-DD HH:mm:ss')
}
return (
{
updateproduct({variables})
}}/>
)
}}
{
this.setState({modal: true, product})
}}/>
)
})
}
X
)
}
}
)
}
}
class AllCategory extends Component {
constructor(props) {
super(props)
this.state = {}
}
render() {
return (
{
({loading, error, data}) => {
if (loading) {
return (
)
}
if (error) {
return 'error!'
}
let categoryList = data.categorybyprops
return (
{
categoryList.map(category => {
return (
-
}>
{category.text}
)
})
}
)
}
}
)
}
}
class AllCategorySwitch extends Component {
constructor(props) {
super(props)
this.state = {
checked: props.category.status === '1'
}
}
render() {
let {category} = this.props
let {checked} = this.state
return (
{(updatecategory, {loading, error}) => {
if (loading)
return (
)
if (error)
return 'error'
let obj = {
id: category.id,
updatedAt: moment().format('YYYY-MM-DD HH:mm:ss')
}
return (
{
this.setState({checked: bool})
updatecategory({variables: {...obj, status: bool ? '1' : '0'}})
}}/>
)
}}
)
}
}
class AllCategoryButton extends Component {
constructor(props) {
super(props)
this.state = {}
}
render() {
let {category} = this.props
return (
{(deletecategory, {loading, error}) => {
if (loading)
return (
)
if (error)
return 'error'
return (
)
}}
)
}
}
class AllCategoryInput extends Component {
constructor(props) {
super(props)
this.state = {
newCategory: ''
}
}
render() {
let {order} = this.props
let {newCategory} = this.state
return (
{(createcategory, {loading, error}) => {
if (loading)
return (
)
if (error)
return 'error'
let obj = {
id: idGen('category'),
name: newCategory,
img: '',
order,
status: '1',
createdAt: moment().format('YYYY-MM-DD HH:mm:ss'),
updatedAt: ''
}
return (
{
this.setState({newCategory: e})
}}
value={newCategory}
placeholder="请输入分类名称"
extra={
}>
新的分类
)
}}
)
}
}
export default withRouter(Goods)