/*
 * @Author: WuWei
 * @Date: 2020-07-10 10:18:41
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-30 17:40:34
 * @Description:
 */
part of '../index.dart';

final Color redColor = Color(0xFFF94F48);
final Color greenColor = Color(0xFF00BC84);
final Color yellowColor = Color(0xFFFD9025);

class _TabDetailComponent extends StatefulWidget {
  _TabDetailComponent({Key? key}) : super(key: key);

  @override
  __TabDetailComponentState createState() => __TabDetailComponentState();
}

class __TabDetailComponentState extends State<_TabDetailComponent> {
  @override
  Widget build(BuildContext context) {
    RepairDetailStore _store;
    _store = Provider.of<RepairDetailStore>(context);
    return WisScrollView(
      key: _store.repairdetailKey,
      onInit: () => _store.getRepairDetail(),
      onRefresh: () => _store.getRepairDetail(),
      slivers: <Widget>[
        if (_store.repairDetail?.id != null) ...[
          _InfoCard().asSliver(),
        ],
        if (_store.repairDetail?.status == 'waitAccept') ...[
          _FormComponent().asSliver(),
        ],
        if (_store.repairDetail?.status == 'accept' ||
            _store.repairDetail?.status == 'solve' ||
            _store.repairDetail?.status == 'evaluate') ...[
          _DispatchCard().asSliver(),
        ],
        _DetailList().asSliver(),
      ],
    );
  }
}

class _InfoCard extends StatelessWidget {
  const _InfoCard({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    RepairDetailStore _store;
    _store = Provider.of<RepairDetailStore>(context);
    RepairDetailModel detail = _store.repairDetail!;
    final listViewItem = ListViewItem(
      item: detail,
      type: 'titleTag',
      picture: detail.photos,
      isList: false,
    );
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 15.pt),
      padding: EdgeInsets.only(top: 10.pt),
      child: listViewItem,
    );
  }
}

class _DispatchCard extends StatelessWidget {
  const _DispatchCard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    RepairDetailStore _store;
    _store = Provider.of<RepairDetailStore>(context);
    RepairDetailModel detail = _store.repairDetail!;
    return WisCard(
      padding: EdgeInsets.all(15.pt),
      margin: EdgeInsets.symmetric(horizontal: 15.pt),
      elevation: 0,
      child: Wisdom.column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Wisdom(
            margin: EdgeInsets.only(bottom: 15.pt),
            child: Text(
              '派单信息',
              style: TextStyle(
                fontSize: 16.pt,
                fontWeight: FontWeight.w600,
                color: Color(0xff222222),
              ),
            ),
          ),
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            margin: EdgeInsets.only(bottom: 15.pt),
            children: <Widget>[
              Wisdom.row(
                minWidth: 75.pt,
                children: <Widget>[
                  WisText(
                    detail.status == 'waitAccept' || detail.status == 'accept'
                        ? '预约时间'
                        : '受理时间',
                    color: colorScheme.minor,
                    size: 14.pt,
                  )
                ],
              ),
              if (detail.status == 'waitAccept' ||
                  detail.status == 'accept') ...[
                Wisdom(
                  onTap: () => _store.setPreTime(),
                  child: WisText(
                    '${detail.appointedTime ?? '尽快'}',
                    color: colorScheme.primary,
                    size: 14.pt,
                  ),
                ),
              ] else ...[
                WisText(
                  '${detail.createAt}',
                  color: colorScheme.minor,
                  size: 14.pt,
                )
              ],
            ],
          ),
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            margin: EdgeInsets.only(bottom: 15.pt),
            children: <Widget>[
              Wisdom.row(
                minWidth: 75.pt,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  WisText(
                    '处理人',
                    color: colorScheme.minor,
                    size: 14.pt,
                  ),
                ],
              ),
              WisText(
                '${detail.dealPerson ?? ''}',
                color: colorScheme.minor,
                size: 14.pt,
              )
            ],
          ),
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            margin: EdgeInsets.only(bottom: 15.pt),
            children: <Widget>[
              Wisdom.row(
                minWidth: 75.pt,
                children: <Widget>[
                  WisText(
                    '处理人电话',
                    color: colorScheme.minor,
                    size: 14.pt,
                  )
                ],
              ),
              Wisdom(
                onTap: () => launch('tel:${detail.dealPersonMobile ?? ''}'),
                child: WisText(
                  '${detail.dealPersonMobile ?? ''}',
                  color: colorScheme.primary,
                  size: 14.pt,
                ),
              )
            ],
          ),
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Wisdom.row(
                minWidth: 75.pt,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  WisText(
                    '抄送人',
                    color: colorScheme.minor,
                    size: 14.pt,
                  ),
                ],
              ),
              WisText(
                '${detail.mailToName ?? '--'}',
                color: colorScheme.minor,
                size: 14.pt,
              ).asFlexible(flex: 1),
            ],
          ),
          if (detail.remark != null && detail.remark != '')
            Wisdom(
              padding: EdgeInsets.symmetric(horizontal: 15.pt),
              child: Divider(
                color: Color(0xffECECEC),
                height: 20.pt,
              ),
            ),
          if (detail.remark != null && detail.remark != '')
            Wisdom(
                child: WisText(
              '${detail.remark}',
              color: Color(0xFFFF7531),
              size: 13.pt,
            ))
        ],
      ),
    );
  }
}

class _DetailList extends StatelessWidget {
  const _DetailList({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    RepairDetailStore _store;
    _store = Provider.of<RepairDetailStore>(context);
    RepairDetailModel? detail = _store.repairDetail;
    return Wisdom.column(
      padding: EdgeInsets.symmetric(horizontal: 10.pt),
      margin: EdgeInsets.only(top: 20.pt),
      children: (detail?.process != null && (detail?.process?.length ?? 0) > 0)
          ? <Widget>[
              for (var i = 0; i < detail!.process!.length; i++)
                Wisdom.stack(
                  clipBehavior: Clip.none,
                  margin: EdgeInsets.all(15.pt),
                  children: <Widget>[
                    Wisdom.column(
                      margin: EdgeInsets.only(left: 15.pt),
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: <Widget>[
                        WisText(
                          '${detail.process![i].event ?? ''}',
                          color: colorScheme.mainTitle,
                          bold: true,
                          size: 14.pt,
                        ),
                        SizedBox(height: 5.pt),
                        WisText(
                          '${detail.process![i].createAt ?? ''}',
                          color: colorScheme.grey,
                          size: 13.pt,
                          height: 1.5,
                        ),
                        SizedBox(height: 10.pt),
                      ],
                    ),
                    if (i != detail.process!.length - 1)
                      Positioned(
                        left: 0.pt,
                        top: 5.pt,
                        bottom: -35.pt,
                        child: Container(
                          width: 1,
                          color: colorScheme.divider,
                        ),
                      ),
                    Positioned(
                      left: -5.pt,
                      top: 5.pt,
                      child: Wisdom(
                        width: 10.pt,
                        height: 10.pt,
                        color:
                            (i != 0) ? Color(0xFF999999) : colorScheme.primary,
                        borderRadius: BorderRadius.circular(10.pt),
                      ),
                    ),
                  ],
                ),
            ]
          : [],
    );
  }
}

class _FormComponent extends StatelessWidget {
  const _FormComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<RepairDetailStore>(context);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final entCode = WisStorageUtil.get(key: 'storage-user-entCode');
    return WisCard(
      margin: EdgeInsets.symmetric(horizontal: 15.pt),
      elevation: 0,
      child: WForm(
        key: store.formKey,
        initialValues: store.formValue,
        onChanged: () => store.formChange(),
        child: Wisdom.column(
          color: Colors.white,
          padding: EdgeInsets.symmetric(horizontal: 15.pt),
          children: [
            WFSinglePicker(
              isRequired: true,
              title: WisText(
                '提报类型',
                size: 15.pt,
                color: colorScheme.mainTitle,
              ),
              label: 'name',
              value: 'id',
              hintText: '请选择业主提报类型',
              initialValue: store.businessTypeId,
              dataList: store.typeList,
              onChanged: (value) => store.setType(value),
            ),
            if (store.sortList != null && store.sortList!.length > 0) ...[
              WFSinglePicker(
                isRequired: false,
                title: WisText(
                  '分类',
                  size: 15.pt,
                  color: colorScheme.mainTitle,
                ),
                label: 'sortName',
                value: 'id',
                hintText: '请选择业主分类',
                initialValue: store.sortId,
                dataList: store.sortList,
                onChanged: (value) => store.setSort(value),
              ),
            ],
            WFTimePicker(
              title: WisText('预约时间'),
              type: 'datetime',
              hintText: '选择预约时间',
              initialDate: store.appointedTime,
              minuteInterval: 10,
              firstDate: DateTime(
                DateTime.now().year,
                DateTime.now().month,
                DateTime.now().day,
                DateTime.now().hour,
                DateTime.now().minute - (DateTime.now().minute % 10) + 10,
              ),
              onChanged: (v) => store.setTime(v),
            ),
            WFSinglePicker(
              isRequired: entCode != 460102 && entCode != 460101 ? false : true,
              title: WisText(
                '内部分类',
                size: 15.pt,
                color: colorScheme.mainTitle,
              ),
              label: 'name',
              value: 'id',
              hintText: '请选择员工内部分类',
              initialValue: store.internalSortId,
              dataList: store.innerSortList,
              onChanged: (value) => store.setInnerSort(value),
            ),
            WFTreePicker(
              title: WisText(
                '处理人',
                size: 15.pt,
                color: colorScheme.mainTitle,
              ),
              isRequired: true,
              dataList: store.dealPersonList,
              initialValue: store.dealPersonId ?? null,
              isMulti: false,
              label: 'name',
              value: 'id',
              child: 'child',
              firstName: 'name',
              firstValue: 'id',
              hintText: '选择处理人',
              isSearch: true,
              onChanged: (v) => store.setDealPerson(v),
            ),
            WFTreePicker(
              title: WisText(
                '抄送人',
                size: 15.pt,
                color: colorScheme.mainTitle,
              ),
              dataList: store.dealPersonList,
              initialValue: store.mailToIds ?? null,
              isMulti: true,
              label: 'name',
              value: 'id',
              child: 'child',
              firstName: 'name',
              firstValue: 'id',
              hintText: '选择抄送人',
              isSearch: true,
              onChanged: (v) => store.setMailPerson(v),
            ),
            WFormTile(
              direction: Axis.vertical,
              leading: Text('备注'),
              child: WTextFormField(
                field: 'remark',
                maxLines: 5,
                minLines: 3,
                maxLength: 200,
                decoration: InputDecoration(
                  fillColor: Colors.white,
                  hintText: '可对本次派单进行补充说明。',
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
