import React,{Component} from 'react';
import {Button, Cascader, message, Popconfirm} from "antd";
import DataTable from "@/components/common/DataTable";
import {getGameState, getprovince, getAddressSelect} from '@/utils/tools';
import AddProject from './components/addProject';
import {ProFormSelect} from "@ant-design/pro-form";
import {getAllCname,deleteProject} from './services/API';
import UpadateProject from './components/upadateProject';

class Project extends Component{
  constructor(props) {
    super(props);
    this.state={
      columns: [
        {
          title: "项目名称",
          dataIndex: "bname",
          key: "bname",
          searchBool: true,
        },
        {
          title: "业务品类",
          dataIndex: "cname",
          key: "cid",
          searchBool: true,
          renderFormItem:()=>{
            return (
              <ProFormSelect
                label=''
                request={this.getAllCname}/>
            )
          }
        },
        {
          title: "业务地区",
          dataIndex: "country",
          key: "country",
          render: (text,record) => {
            let isCountry = "";
            if (text === '1') {
              isCountry = "全国";
            } else {
              if(!isNaN(parseInt(record.province))){
                isCountry += getprovince(parseInt(record.province));
              }
              if(!isNaN(parseInt(record.city))){
                isCountry += '-'+getprovince(parseInt(record.city));
              }
              if(!isNaN(parseInt(record.district))){
                isCountry += '-'+getprovince(parseInt(record.district));
              }
            }
            return (
              isCountry
            );
          }
        },
        {
          title: "市场价",
          dataIndex: "price",
          key: "price",
        },
        {
          title: "最低折扣",
          dataIndex: "offset",
          key: "offset",
        },
        {
          title: "会员价",
          dataIndex: "memberPrice",
          key: "memberPrice",
        },
        {
          title: "活动价",
          dataIndex: "activityPrice",
          key: "activityPrice",
        },
        {
          title: "活动生效标识",
          dataIndex: "activityFlag",
          key: "activityFlag",
          searchBool: true,
          valueType: 'select',
          valueEnum: {
            '0': {
              text: '有效',
            },
            '1': {
              text: '无效',
            },
          },
          render: (text,record) => {
            return getGameState(record.activityFlag);
          },
        },
        {
          title: "业务状态",
          dataIndex: "status",
          key: "status",
          searchBool: true,
          valueType: 'select',
          valueEnum: {
            '0': {
              text: '正常',
              status: 'Success'
            },
            '1': {
              text: '停用',
              status: 'Error'
            },
          },
        },
        {
          title: "项目分类",
          dataIndex: "type",
          key: "type",
          searchBool: true,
          valueType: 'select',
          valueEnum: {
            0: {
              text: '通用',
            },
            1: {
              text: '专利',
            },
            2: {
              text: '软著',
            },
            3: {
              text: '审计',
            },
            4: {
              text: '双软',
            },
            5: {
              text: '高新',
            },
            6: {
              text: '商标',
            },
          },
          render: (text, record) => {
            if (record.type === 0) {
              return "通用";
            } else if (record.type === 1) {
              return "专利";
            } else if (record.type === 2) {
              return "软著";
            } else if (record.type === 3) {
              return "审计";
            }else if(record.type === 4) {
              return "双软"
            }else if(record.type === 5) {
              return "高新"
            }else if(record.type === 6) {
              return "商标"
            }
          },
        },
        {
          title: '省-市-区',
          key: 'address',
          hideInTable: true,
          dataIndex: 'address',
          searchBool: true,
          search: {
            transform: (v)=>{
              return {
                province: v[0] || '',
                city: v[1] || '',
                district: v[2] || '',
              }
            }
          },
          renderFormItem: (item, props) => {
            let addressSelect = getAddressSelect()
            addressSelect.unshift({
              label: '全国',
              value: 0,
            })
            return (
              <Cascader
                changeOnSelect
                options={addressSelect}
                placeholder='请选择省-市-区'/>
            );
          },
        },{
          title: '操作',
          dataIndex: 'id',
          key: 'id',
          render: (text,record,key,action) => { return (
            <Popconfirm
              title="是否删除?"
              onConfirm={(e)=>{e.stopPropagation();this.deleteProject(text,action)}}
              onCancel={(e)=>{e.stopPropagation();}}
              okText="确认"
              cancelText="取消"
              placement="topLeft">
              <Button type='primary' danger onClick={(e)=>e.stopPropagation()}>删除</Button>
            </Popconfirm>
          ) }
        }
      ],
      visible: false,
      projectInfor: {},
    }
  }

  deleteById=async (id,action)=>{
    message.loading({content:'删除中...',key:'deleteById'})
    const msg = await deleteById(id);
    if(msg.error.length === 0){
      message.success({content:'删除成功',key:'deleteById',duration:2.5});
      action.reload();
    }else{
      message.error({content:msg.error[0].message,key:'deleteById',duration:2.5});
    }
  }

  deleteProject= async (id,action)=>{
    message.loading({content:'删除中...',key:'deleteProject'});
    const msg = await deleteProject(id);
    if(msg.error.length === 0){
      message.success({content:'删除成功',key:'deleteProject',duration:2.5});
      action.reload();
    }else{
      message.error({content:msg.error[0].message,key:'deleteProject',duration:2.5})
    }
  }

  getAllCname=async ()=>{
    const msg = await getAllCname();
    let theArr = [];
    if(msg.error.length === 0){
      msg.data.forEach((item) => {
        theArr.push({
          label: item.cname,
          value: item.id,
        });
      });
    }else{
      message.error(msg.error[0].message)
    }
    return theArr;
  }

  render() {
    const ref = React.createRef();
    return (
      <>
        <DataTable
          ref={ref}
          headerTitle='业务项目管理'
          url='/api/admin/ProjectSize/listProject'
          method='post'
          rowKey='id'
          scroll={{x:1100}}
          columns={this.state.columns}
          search={{
            filterType: 'query',
            labelWidth: 'auto',
            defaultCollapsed: false,
          }}
          onRow={(record) => ({
            onClick: (e) => {
              e.stopPropagation();
              this.setState({
                visible: true,
                projectInfor: record,
              })
            }
          })}
          toolBarRender={[
            <AddProject formRef={ref}  key='addProject'/>
          ]}
        />
        {this.state.visible && <UpadateProject
          formRef={ref}
          visible={this.state.visible}
          projectInfor={this.state.projectInfor}
          onCancel={()=>{
            this.setState({
              visible: false,
              projectInfor: {}
            })
          }}
        />}
      </>
    )
  }
}

export default Project;
