import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Icon, Select, Tabs, Radio, DatePicker,message } from 'antd';
import { FormattedMessage } from 'umi-plugin-react/locale';
import styles from '../style/GoodsList.less';
import moment from 'moment';
import router from 'umi/router';
import BuildSelect from '../../components/SelectButton/BuildSelect'
import SelectCity from '../../components/SelectButton/CitySelect'
import XForm, { FieldTypes } from '../../components/XForm';
import Wangedit from '../../components/Wangedit/Wangedit'
import request from '../../utils/request'

const { MonthPicker, RangePicker, WeekPicker } = DatePicker;
/**
 *
 *
 * @param {*} props
 * @returns
 */
 const Edit = (props) => {
  const policyId = props.location.query.policyId
  const [ policyData, setPolicyData ] = useState({})
  if(policyId){
    useEffect(() => {
      getPolicyData(policyId);
    },[])

  // 查询列表
  const getPolicyData = (policyId) => {
    request({
        url: '/api/admin/taPolicy/' + policyId,
        method: 'GET',
    }).then((data) => {
        console.log(data)
        setPolicyData(data)
    })
  }
  }

  const cancelPage = () =>{
    router.push({
      pathname: '/system/housingPolicy',
    });
  }
    const fields = [
      {
        label: '城市',
        name: 'cityId',
        render: <SelectCity />,
        value: policyData.cityId,
      },
      {
        label: '政策图片',
        name: 'policyImg',
        type: FieldTypes.ImageUploader,
        value: policyData.policyImg,
      },
      {
        label: '政策标题',
        name: 'title',
        type: FieldTypes.Text,
        value: policyData.title,
      },
      {
        label: '政策详情',
        name: 'body',
        render: <Wangedit />,
        value: policyData.body,
      },
      {
        label: '状态',
        name: 'publishStatus',
        type: FieldTypes.Select,
        dict: [{label:"未发布",value:0},{label:"已发布",value:1}],
        value: policyData.publishStatus != null ? policyData.publishStatus : 1,
      },
    ]
  
    const handleSubmit = val => { 
      console.log('submit data --->', val)
      if(policyId){
        val.policyId = policyId
        request({
          url: '/api/admin/taPolicy/'+policyId,
          method: 'PUT',
          data: val,
        }).then((data) => {
          message.info("保存成功")
          cancelPage()
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      }else{
        request({
          url: '/api/admin/taPolicy',
          method: 'POST',
          data: val,
        }).then((data) => {
          message.info("保存成功")
          cancelPage()
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      }
    }
     

  return (
    <XForm onSubmit={handleSubmit} onCancel={cancelPage} fields={fields}></XForm>
  );
 }



export default Edit
