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 apis from '../../services/apis';
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({ ...apis.system.taPolicy, urlData: {id: policyId} }).then((data) => {
        console.log(data)
        setPolicyData(data)
    })
  }
  }

  const cancelPage = () =>{
    router.push({
      pathname: '/system/housingPolicy',
    });
  }
    const fields = [
      {
        label: '城市',
        name: 'cityId',
        render: <SelectCity />,
        value: policyData.cityId,
        rules: [
          {
            required: true,
            message: '请选择城市',
          },
        ],
      },
      {
        label: '购房须知封面图',
        name: 'policyImg',
        type: FieldTypes.ImageUploader,
        value: policyData.policyImg,
        help: '建议图片尺寸：220*176px，比例5:4,格式：jpg，用于购房须知列表',
        rules: [
          {
            required: true,
            message: '请选择购房须知封面图',
          },
        ],
      },
      {
        label: '购房须知标题',
        name: 'title',
        type: FieldTypes.Text,
        value: policyData.title,
        rules: [
          {
            required: true,
            message: '请输入购房须知标题',
          },
        ],
      },
      {
        label: '购房须知详情',
        name: 'body',
        render: <Wangedit />,
        value: policyData.body,
        rules: [
          {
            required: true,
            message: '请输入购房须知详情',
          },
        ],
      },
      {
        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)
      console.log(val.body, 'val.body')
      if (val.body == '<p><br></p>'){
        message.info("购房须知详情必填")
        return
      }
      
      if(policyId){
        val.policyId = policyId
        request({ ...apis.system.updateTaPolicy, urlData: {id: policyId},data: val, }).then((data) => {
          message.info("保存成功")
          cancelPage()
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      }else{
        request({ ...apis.system.addTaPolicy,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
