import React from 'react';
import Taro from '@tarojs/taro';
import { View, CoverView } from '@tarojs/components';
import { CellGroup, Cell, Field, RadioGroup, Radio, Icon, Loading } from '@antmjs/vantui';
import Map from '@/components/map';
import { getTaCheckItemAnswer } from '@/services/tacheckitem';
import { getTaCheckAnswer } from '@/services/tacheckanswer';
import mapIcon from '@/assets/icons/marker.png';
import AgePicker from './AgePicker';

export default (props) => {
  const { checkItemInfo, checkType, answer, readonly, onChange, onLoadingChange } = props;

  const [showAgePicker, setShowAgePicker] = React.useState(false);
  const [count, setCount] = React.useState();

  const setLoading = (v) => {
    if (onLoadingChange) {
      onLoadingChange(v)
    }
  }

  const setFieldChange = (key, val) => {
    onChange({
      ...answer || {},
      [key]: val,
    })
  }

  React.useEffect(() => {
    getTaCheckAnswer().then((res) => {
      setCount(res.total);
    })
  })

  // // 获取答题主记录信息
  // React.useEffect(() => {
  //   if (checkItemInfo?.itemId) {
  //     setLoading(true);
  //     getTaCheckItemAnswer(checkItemInfo?.itemId).then((res) => {
  //       if (res) {
  //         onChange(res);
  //       }
  //       setLoading(false);
  //     }).catch(() => {
  //       setLoading(false);
  //     });
  //   }
  // }, [checkItemInfo?.itemId])

  // console.log('answer', answer);

  return (
    <View>
      <Map
        readOnly={readonly}
        location={answer?.location}
        onChange={e => setFieldChange('location', e)}
      />
      {/* <View style={{ position: 'fixed', zIndex: '3000', top: '50%', color: 'red', display: 'grid', width: '50px', height: '50px' }}>
        <Loading type="spinner" vertical />
      </View> */}
      <CellGroup>
        {
          checkType == 'loc' && (
            <Cell
              title="点位"
              value={checkItemInfo?.name}
            />
          )
        }
        {
          checkType == 'survey' && (
            <Field
              label="社区"
              placeholder="请填写社区名称"
              readonly={readonly}
              value={answer?.communityName}
              onChange={e => setFieldChange('communityName', e.detail)}
            />
          )
        }
        <Field
          readonly={readonly}
          label={checkType == 'loc' ? '地址' : '小区'}
          placeholder={checkType == 'loc' ? '请输入地址' : '请填写小区名称'}
          value={answer?.addr}
          onChange={e => setFieldChange('addr', e.detail)}
        />
      </CellGroup>
      {
        checkType == 'survey' && (
          <CellGroup style={{ marginTop: '20px' }}>
            <Cell title="性别">
              <View style={{ textAlign: 'right' }}>
                <RadioGroup
                  disabled={readonly}
                  direction="horizontal"
                  value={answer?.sex}
                  style={{ display: 'inline-flex' }}
                  onChange={(e) => setFieldChange('sex', e.detail)}
                >
                  <Radio name="1" checkedColor="var(--main-bg-color)">男</Radio>
                  <Radio name="2" checkedColor="red">女</Radio>
                </RadioGroup>
              </View>
            </Cell>
            <Cell
              title="年龄"
              isLink
              value={answer?.age}
              onClick={() => !readonly && setShowAgePicker(true)}
            />
            <AgePicker
              show={showAgePicker}
              onShowChange={setShowAgePicker}
              onChange={(e) => setFieldChange('age', e)}
            />
          </CellGroup>
        )
      }
      {/* <CellGroup style={{ marginTop: '20px' }}>
        <Cell>
          <View style={{ textAlign: 'center', color: '#000' }}>
            您已完成{count}份
          </View>
        </Cell>
      </CellGroup> */}

      {
        checkType == 'survey' && (
          <CellGroup style={{ marginTop: '20px' }}>
            <Cell
              title="已完成"
              value={`${checkItemInfo?.answerNum || 0} 份`}
            />
          </CellGroup>
        )
      }
    </View >
  )
}
