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

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

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

  // 获取答题主记录信息
  React.useEffect(() => {
    if (checkItemInfo?.itemId) {
      setLoading(true);
      getTaCheckItemAnswer(checkItemInfo?.itemId).then((res) => {
        if (res) {
          onChange(res);
        }
        setLoading(false);
      }).catch(() => {
        setLoading(false);
      });
    }
  }, [checkItemInfo?.itemId])
  
  return (
    <View>
      <Map
        readOnly={readonly}
        location={answer?.location}
        onChange={(e) => setFieldChange('location', e)}
      />
      <CellGroup>
        <Cell
          title="点位"
          value={checkItemInfo?.name}
        />
        <Field
          readonly={readonly}
          placeholder="请输入地址"
          value={answer?.addr}
          leftIcon={mapIcon}
          onChange={e => setFieldChange('addr', e.detail)}
        />
      </CellGroup>
    </View>
  )
}
